Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components #116

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

[Unreleased]
## [Unreleased]

### Added

- ACF Codifier Components
+ Components are reusable field sets.
+ Components can be transformed into any type of groupable field with component factories.
* `Geniem\ACF\Factory\Block`: A factory for creating ACF Gutenberg blocks with Codifier components.
+ Component rendering functionality implementing the `Geniem\ACF\Interfaces\Renderer` interface.
* `Geniem\ACF\Renderer\PHP`: Uses PHP files as templates.
* `Geniem\ACF\Renderer\Dust`: Uses Dust.js template files.

## [1.41.2]

Expand Down Expand Up @@ -228,6 +238,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Added the ability to use Allow null setting with the Radio field.
>>>>>>> master

## [1.20.0] - 2019-10-10

Expand Down
242 changes: 242 additions & 0 deletions src/Component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<?php
/**
* Codifier Component abstraction.
*/

namespace Geniem\ACF;

use Geniem\ACF\Field\Common\Groupable,
Geniem\ACF\Interfaces\Component as ComponentInterface;
use Geniem\ACF\Interfaces\Renderer;

/**
* Class Component
*
* This class is an abstraction of a ACF Codifier component.
* A component is a reusable set of fields.
*
* @package Geniem\ACF
*/
abstract class Component implements ComponentInterface {

/**
* Import the groupable functionalities.
*/
use Groupable;

/**
* Component slug or name, internal
*
* @var string
*/
protected $name = '';

/**
* Component title, shows to the user
*
* @var string
*/
protected $title = '';

/**
* The block category
*
* @var string
*/
protected $category = '';

/**
* The block icon. Can hold both the string or array representation.
*
* @var string|array
*/
protected $icon = '';

/**
* The block keywords.
*
* @var array
*/
protected $keywords = [];

/**
* Component description, shows to the user
*
* @var string
*/
protected $description = '';

/**
* Component fields.
*
* @var array
*/
protected $fields = [];

/**
* Array of post types to restrict this block type to.
*
* @var array
*/
protected $post_types = [];

/**
* Display mode for the block.
*
* Options: auto, preview or edit.
*
* @var string
*/
protected $mode = 'preview';

/**
* The default block alignment.
*
* Options: left, center, right, wide or full.
*
* @var string
*/
protected $align = '';

/**
* An array of features for the block to support.
*
* @see https://wordpress.org/gutenberg/handbook/block-api/
*
* @var array
*/
protected $supports = [];

/**
* The renderer for this component.
*
* @var Renderer
*/
protected $renderer;

/**
* Getter for the name.
*
* @return string
*/
public function get_name() : string {

return $this->name;
}

/**
* Getter for the title.
*
* @return string
*/
public function get_title() : string {

return $this->title;
}

/**
* Getter for the description.
*
* @return string
*/
public function get_description() : string {

return $this->description;
}

/**
* Constructor method
*
* @param array|null $args Arguments to give to the block on creation.
*/
public function __construct( ?array $args = null ) {}

/**
* Set the renderer for the component.
*
* @param Renderer $renderer The renderer.
*/
public function set_renderer( Renderer $renderer ) {
$this->renderer = $renderer;
}

/**
* Getter for the component renderer.
*
* @return Renderer
* @throws Exception An exception is thrown if the renderer is not set
* and this method is called.
*/
public function get_renderer(): Renderer {

if ( empty( $this->renderer ) ) {
// The extending class must implement this method
// if the renderer is not set.
throw new Exception( 'You must implement get_renderer()' );
}
return $this->renderer;
}

/**
* Getter for the category.
*
* @return string
*/
public function get_category() : string {
return $this->category;
}

/**
* Getter for the icon.
*
* @return string
*/
public function get_icon() : string {
return $this->icon;
}

/**
* Getter for the keywords.
*
* @return array
*/
public function get_keywords() : array {
return $this->keywords;
}

/**
* Getter for the post types.
*
* @return array
*/
public function get_post_types() : array {
return $this->post_types;
}

/**
* Getter for the display mode.
*
* @return string
*/
public function get_mode() : string {
return $this->mode;
}

/**
* Getter for the default block alignment.
*
* @return string
*/
public function get_align() : string {
return $this->align;
}

/**
* Getter for the supported features of the block.
*
* @return array
*/
public function get_supports() : array {
return $this->supports;
}
}
Loading