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 AbstractController |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Constructor |
||
| 13 | * |
||
| 14 | * @param array $model |
||
| 15 | */ |
||
| 16 | public function __construct( array $model = array() ) |
||
| 17 | { |
||
| 18 | parent::__construct($model); |
||
| 19 | $this->on_created(); |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The default model to use when none is provided to the constructor. |
||
| 24 | * This method should be overriden by child class to define the default |
||
| 25 | * model. |
||
| 26 | * |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function default_model() |
||
| 30 | { |
||
| 31 | return array(); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The list of required model arguments. |
||
| 36 | * This method should be overriden by child class to specify required model |
||
| 37 | * arguments. |
||
| 38 | * |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | public function required_arguments() |
||
| 42 | { |
||
| 43 | return array(); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set the model data for this component. |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public function set_model( $model ) |
||
| 52 | { |
||
| 53 | // Check that the required arguments are provided. |
||
| 54 | foreach( $this->required_arguments() as $key ) |
||
| 55 | { |
||
| 56 | if ( !isset($model[$key]) ) |
||
| 57 | { |
||
| 58 | throw new \RuntimeException('The required argument "'.$key.'" was not provided for '.get_called_class()); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | // Assign the name of the component as the id if no id was specified |
||
| 63 | if( !isset($model['id']) ) |
||
| 64 | { |
||
| 65 | $model['id'] = $model['name']; |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->model = array_merge( $this->default_model(), $model ); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * A hook that is called once the component has been created. |
||
| 73 | */ |
||
| 74 | protected function on_created() {} |
||
| 75 | } |