amarkal /
amarkal-ui
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Amarkal\UI; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Defines an abstract UI component |
||
| 7 | */ |
||
| 8 | abstract class AbstractComponent |
||
| 9 | extends Template |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 10 | { |
||
| 11 | /** |
||
| 12 | * This template is used to generate the name for this component. the token |
||
| 13 | * {{name}} will be replaced by the component's name during runtime. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | public $name_template = '{{name}}'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * This template is used to generate the name for this component when it is |
||
| 21 | * used as a child component in a composite component. The token |
||
| 22 | * {{parent_name}} will be replaced with the name of the parent (composite) |
||
| 23 | * component. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $composite_name_template = '{{parent_name}}[{{name}}]'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor |
||
| 31 | * |
||
| 32 | * @param array $model |
||
| 33 | */ |
||
| 34 | public function __construct( array $model = array() ) |
||
| 35 | { |
||
| 36 | parent::__construct($model); |
||
| 37 | $this->on_created(); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The default model to use when none is provided to the constructor. |
||
| 42 | * This method should be overriden by child class to define the default |
||
| 43 | * model. |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public function default_model() |
||
| 48 | { |
||
| 49 | return array(); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The list of required model arguments. |
||
| 54 | * This method should be overriden by child class to specify required model |
||
| 55 | * arguments. |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function required_arguments() |
||
| 60 | { |
||
| 61 | return array(); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Set the model data for this component. |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function set_model( $model ) |
||
| 70 | { |
||
| 71 | // Check that the required arguments are provided. |
||
| 72 | foreach( $this->required_arguments() as $key ) |
||
| 73 | { |
||
| 74 | if ( !isset($model[$key]) ) |
||
| 75 | { |
||
| 76 | throw new \RuntimeException('The required argument "'.$key.'" was not provided for '.get_called_class()); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | // Assign the name of the component as the id if no id was specified |
||
| 81 | if( !isset($model['id']) ) |
||
| 82 | { |
||
| 83 | $model['id'] = $model['name']; |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->model = array_merge( $this->default_model(), $model ); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the name for this component by parsing the name template. |
||
| 91 | * |
||
| 92 | * @return type |
||
| 93 | */ |
||
| 94 | public function get_name() |
||
| 95 | { |
||
| 96 | return \str_replace('{{name}}', $this->name, $this->name_template); |
||
|
0 ignored issues
–
show
The property
name does not exist on object<Amarkal\UI\AbstractComponent>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Generate common UI component wrapper attributes |
||
| 101 | */ |
||
| 102 | public function component_attributes() |
||
| 103 | { |
||
| 104 | return sprintf( |
||
| 105 | 'class="amarkal-ui-component amarkal-ui-component-%s" amarkal-component-name="%s"', |
||
| 106 | $this->component_type, |
||
|
0 ignored issues
–
show
The property
component_type does not exist on object<Amarkal\UI\AbstractComponent>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 107 | $this->name |
||
|
0 ignored issues
–
show
The property
name does not exist on object<Amarkal\UI\AbstractComponent>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Enqueue component's script and render it. |
||
| 113 | * |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function render( $echo = false ) |
||
| 117 | { |
||
| 118 | $this->enqueue_scripts(); |
||
| 119 | return parent::render($echo); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Enqueue styles/scripts required for this element. |
||
| 124 | */ |
||
| 125 | public function enqueue_scripts() |
||
| 126 | { |
||
| 127 | \wp_enqueue_script('amarkal-ui'); |
||
| 128 | \wp_enqueue_style('amarkal-ui'); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * A hook that is called once the component has been created. |
||
| 133 | */ |
||
| 134 | protected function on_created() {} |
||
| 135 | } |