@@ -21,7 +21,7 @@ |
||
| 21 | 21 | |
| 22 | 22 | public function required_arguments() |
| 23 | 23 | { |
| 24 | - return array('name','data'); |
|
| 24 | + return array('name', 'data'); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | public function get_template_path() |
@@ -8,7 +8,7 @@ |
||
| 8 | 8 | class Component_select |
| 9 | 9 | extends AbstractComponent |
| 10 | 10 | implements ValueComponentInterface, |
| 11 | - DisableableComponentInterface |
|
| 11 | + DisableableComponentInterface |
|
| 12 | 12 | { |
| 13 | 13 | public function default_model() |
| 14 | 14 | { |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | * @param array $model |
| 15 | 15 | */ |
| 16 | - public function __construct( array $model = array() ) |
|
| 16 | + public function __construct(array $model = array()) |
|
| 17 | 17 | { |
| 18 | 18 | parent::__construct($model); |
| 19 | 19 | $this->on_created(); |
@@ -48,24 +48,24 @@ discard block |
||
| 48 | 48 | * |
| 49 | 49 | * @return array |
| 50 | 50 | */ |
| 51 | - public function set_model( $model ) |
|
| 51 | + public function set_model($model) |
|
| 52 | 52 | { |
| 53 | 53 | // Check that the required arguments are provided. |
| 54 | - foreach( $this->required_arguments() as $key ) |
|
| 54 | + foreach ($this->required_arguments() as $key) |
|
| 55 | 55 | { |
| 56 | - if ( !isset($model[$key]) ) |
|
| 56 | + if (!isset($model[$key])) |
|
| 57 | 57 | { |
| 58 | 58 | throw new \RuntimeException('The required argument "'.$key.'" was not provided for '.get_called_class()); |
| 59 | 59 | } |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // Assign the name of the component as the id if no id was specified |
| 63 | - if( !isset($model['id']) ) |
|
| 63 | + if (!isset($model['id'])) |
|
| 64 | 64 | { |
| 65 | 65 | $model['id'] = $model['name']; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - $this->model = array_merge( $this->default_model(), $model ); |
|
| 68 | + $this->model = array_merge($this->default_model(), $model); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
@@ -17,7 +17,9 @@ |
||
| 17 | 17 | /** |
| 18 | 18 | * Prevent loading the library more than once |
| 19 | 19 | */ |
| 20 | -if( defined( 'AMARKAL_UI' ) ) return false; |
|
| 20 | +if( defined( 'AMARKAL_UI' ) ) { |
|
| 21 | + return false; |
|
| 22 | +} |
|
| 21 | 23 | define( 'AMARKAL_UI', true ); |
| 22 | 24 | |
| 23 | 25 | if(!function_exists('amarkal_ui_render')) |
@@ -12,15 +12,15 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | // Prevent direct file access |
| 15 | -defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
|
| 15 | +defined('ABSPATH') or die('No script kiddies please!'); |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Prevent loading the library more than once |
| 19 | 19 | */ |
| 20 | -if( defined( 'AMARKAL_UI' ) ) return false; |
|
| 21 | -define( 'AMARKAL_UI', true ); |
|
| 20 | +if (defined('AMARKAL_UI')) return false; |
|
| 21 | +define('AMARKAL_UI', true); |
|
| 22 | 22 | |
| 23 | -if(!function_exists('amarkal_ui_render')) |
|
| 23 | +if (!function_exists('amarkal_ui_render')) |
|
| 24 | 24 | { |
| 25 | 25 | /** |
| 26 | 26 | * Render a UI component. |
@@ -30,14 +30,14 @@ discard block |
||
| 30 | 30 | * @param array $props The component's properties |
| 31 | 31 | * @return string The rendered HTML |
| 32 | 32 | */ |
| 33 | - function amarkal_ui_render( $type, array $props = array() ) |
|
| 33 | + function amarkal_ui_render($type, array $props = array()) |
|
| 34 | 34 | { |
| 35 | - $component = Amarkal\UI\ComponentFactory::create( $type, $props ); |
|
| 35 | + $component = Amarkal\UI\ComponentFactory::create($type, $props); |
|
| 36 | 36 | return $component->render(); |
| 37 | 37 | } |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | -if(!function_exists('amarkal_ui_register_component')) |
|
| 40 | +if (!function_exists('amarkal_ui_register_component')) |
|
| 41 | 41 | { |
| 42 | 42 | /** |
| 43 | 43 | * Register a custom UI component. The registered component's class should |
@@ -47,8 +47,8 @@ discard block |
||
| 47 | 47 | * to one of the core component's type, it will override the core component. |
| 48 | 48 | * @param string $class_name The component's class name. |
| 49 | 49 | */ |
| 50 | - function amarkal_ui_register_component( $type, $class_name ) |
|
| 50 | + function amarkal_ui_register_component($type, $class_name) |
|
| 51 | 51 | { |
| 52 | - Amarkal\UI\ComponentFactory::register( $type, $class_name ); |
|
| 52 | + Amarkal\UI\ComponentFactory::register($type, $class_name); |
|
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | \ No newline at end of file |
@@ -6,7 +6,7 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | // Prevent direct file access |
| 9 | -defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
|
| 9 | +defined('ABSPATH') or die('No script kiddies please!'); |
|
| 10 | 10 | |
| 11 | 11 | // Load module functions |
| 12 | 12 | require_once 'functions.php'; |
| 13 | 13 | \ No newline at end of file |
@@ -6,13 +6,13 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | // Prevent direct file access |
| 9 | -defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
|
| 9 | +defined('ABSPATH') or die('No script kiddies please!'); |
|
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Load module functions. If this amarkal module has not been loaded, |
| 13 | 13 | * functions.php will not return false. |
| 14 | 14 | */ |
| 15 | -if(false !== (require_once 'functions.php')) |
|
| 15 | +if (false !== (require_once 'functions.php')) |
|
| 16 | 16 | { |
| 17 | 17 | // Load required classes if not using composer |
| 18 | 18 | require_once 'Renderer.php'; |
@@ -52,8 +52,8 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | 54 | * |
| 55 | - * @param type $type |
|
| 56 | - * @param type $props |
|
| 55 | + * @param string $type |
|
| 56 | + * @param type string |
|
| 57 | 57 | * @throws \RuntimeException |
| 58 | 58 | */ |
| 59 | 59 | private static function create_core_component( $type, $props ) |
@@ -79,8 +79,8 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | /** |
| 81 | 81 | * |
| 82 | - * @param type $type |
|
| 83 | - * @param type $props |
|
| 82 | + * @param string $type |
|
| 83 | + * @param type string |
|
| 84 | 84 | * @throws \RuntimeException |
| 85 | 85 | */ |
| 86 | 86 | private static function create_registered_component( $type, $props ) |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * @param array $props |
| 21 | 21 | * @return AbstractComponent |
| 22 | 22 | */ |
| 23 | - public static function create( $type, array $props ) |
|
| 23 | + public static function create($type, array $props) |
|
| 24 | 24 | { |
| 25 | 25 | try { |
| 26 | 26 | $component = self::create_registered_component($type, $props); |
@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | * @param string $class_name |
| 42 | 42 | * @throws \RuntimeException |
| 43 | 43 | */ |
| 44 | - public static function register( $type, $class_name ) |
|
| 44 | + public static function register($type, $class_name) |
|
| 45 | 45 | { |
| 46 | - if( !in_array($type, self::$registered_components) ) |
|
| 46 | + if (!in_array($type, self::$registered_components)) |
|
| 47 | 47 | { |
| 48 | 48 | self::$registered_components[$type] = $class_name; |
| 49 | 49 | } |
@@ -56,15 +56,15 @@ discard block |
||
| 56 | 56 | * @param type $props |
| 57 | 57 | * @throws \RuntimeException |
| 58 | 58 | */ |
| 59 | - private static function create_core_component( $type, $props ) |
|
| 59 | + private static function create_core_component($type, $props) |
|
| 60 | 60 | { |
| 61 | 61 | $file_name = __DIR__."/components/$type/controller.php"; |
| 62 | 62 | $class_name = 'Amarkal\\UI\\Component_'.$type; |
| 63 | 63 | |
| 64 | 64 | // Load one of the core components |
| 65 | - if(!class_exists($class_name)) |
|
| 65 | + if (!class_exists($class_name)) |
|
| 66 | 66 | { |
| 67 | - if( file_exists( $file_name ) ) |
|
| 67 | + if (file_exists($file_name)) |
|
| 68 | 68 | { |
| 69 | 69 | require_once $file_name; |
| 70 | 70 | } |
@@ -83,9 +83,9 @@ discard block |
||
| 83 | 83 | * @param type $props |
| 84 | 84 | * @throws \RuntimeException |
| 85 | 85 | */ |
| 86 | - private static function create_registered_component( $type, $props ) |
|
| 86 | + private static function create_registered_component($type, $props) |
|
| 87 | 87 | { |
| 88 | - if(in_array($type, self::$registered_components)) |
|
| 88 | + if (in_array($type, self::$registered_components)) |
|
| 89 | 89 | { |
| 90 | 90 | $class_name = self::$registered_components[$type]; |
| 91 | 91 | return new $class_name($props); |
@@ -46,8 +46,9 @@ discard block |
||
| 46 | 46 | if( !in_array($type, self::$registered_components) ) |
| 47 | 47 | { |
| 48 | 48 | self::$registered_components[$type] = $class_name; |
| 49 | + } else { |
|
| 50 | + throw new \RuntimeException("A component of type '$type' has already been registered."); |
|
| 49 | 51 | } |
| 50 | - else throw new \RuntimeException("A component of type '$type' has already been registered."); |
|
| 51 | 52 | } |
| 52 | 53 | |
| 53 | 54 | /** |
@@ -67,8 +68,7 @@ discard block |
||
| 67 | 68 | if( file_exists( $file_name ) ) |
| 68 | 69 | { |
| 69 | 70 | require_once $file_name; |
| 70 | - } |
|
| 71 | - else |
|
| 71 | + } else |
|
| 72 | 72 | { |
| 73 | 73 | throw new \RuntimeException("A component of type '$type' does not exist."); |
| 74 | 74 | } |
@@ -8,9 +8,9 @@ |
||
| 8 | 8 | class Component_text |
| 9 | 9 | extends AbstractComponent |
| 10 | 10 | implements ValueComponentInterface, |
| 11 | - DisableableComponentInterface, |
|
| 12 | - FilterableComponentInterface, |
|
| 13 | - ValidatableComponentInterface |
|
| 11 | + DisableableComponentInterface, |
|
| 12 | + FilterableComponentInterface, |
|
| 13 | + ValidatableComponentInterface |
|
| 14 | 14 | { |
| 15 | 15 | public function default_model() |
| 16 | 16 | { |
@@ -202,7 +202,9 @@ |
||
| 202 | 202 | */ |
| 203 | 203 | private function validate( $component ) |
| 204 | 204 | { |
| 205 | - if( !($component instanceof ValidatableComponentInterface) ) return; |
|
| 205 | + if( !($component instanceof ValidatableComponentInterface) ) { |
|
| 206 | + return; |
|
| 207 | + } |
|
| 206 | 208 | |
| 207 | 209 | $name = $component->name; |
| 208 | 210 | $validate = $component->validation; |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | * Get a component by its name. |
| 138 | 138 | * |
| 139 | 139 | * @param string $name |
| 140 | - * @return UI\AbstractComponent |
|
| 140 | + * @return AbstractComponent |
|
| 141 | 141 | */ |
| 142 | 142 | public function get_component( $name ) |
| 143 | 143 | { |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | /** |
| 148 | 148 | * Get all components. |
| 149 | 149 | * |
| 150 | - * @return array |
|
| 150 | + * @return AbstractComponent[] |
|
| 151 | 151 | */ |
| 152 | 152 | public function get_components() |
| 153 | 153 | { |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | /** |
| 182 | 182 | * Check if the given component is disabled. |
| 183 | 183 | * |
| 184 | - * @param UI\AbstractComponent $component |
|
| 184 | + * @param ValueComponentInterface $component |
|
| 185 | 185 | * @return boolean |
| 186 | 186 | */ |
| 187 | 187 | private function is_disabled( $component ) |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | /** |
| 198 | 198 | * Filter the component's value using its filter function (if applicable) |
| 199 | 199 | * |
| 200 | - * @param UI\AbstractComponent $component |
|
| 200 | + * @param ValueComponentInterface $component |
|
| 201 | 201 | */ |
| 202 | 202 | private function filter( $component ) |
| 203 | 203 | { |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | * If the value is invalid, the old value is used, and an error message is |
| 220 | 220 | * saved into the errors array as component_name => error_message. |
| 221 | 221 | * |
| 222 | - * @param UI\AbstractComponent $component The component to validate. |
|
| 222 | + * @param ValueComponentInterface $component The component to validate. |
|
| 223 | 223 | */ |
| 224 | 224 | private function validate( $component ) |
| 225 | 225 | { |
@@ -59,12 +59,12 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @param array $components An array of arrays of component arguments |
| 61 | 61 | */ |
| 62 | - public function __construct( array $components = array() ) |
|
| 62 | + public function __construct(array $components = array()) |
|
| 63 | 63 | { |
| 64 | - foreach( $components as $args ) |
|
| 64 | + foreach ($components as $args) |
|
| 65 | 65 | { |
| 66 | 66 | $name = $args['name']; |
| 67 | - if(array_key_exists($name, $components)) |
|
| 67 | + if (array_key_exists($name, $components)) |
|
| 68 | 68 | { |
| 69 | 69 | throw new \RuntimeException("A component with the name <b>$name</b> has already been created"); |
| 70 | 70 | } |
@@ -89,18 +89,18 @@ discard block |
||
| 89 | 89 | * |
| 90 | 90 | * @return array The updated values array. |
| 91 | 91 | */ |
| 92 | - public function update( array $new_instance, array $old_instance = array() ) |
|
| 92 | + public function update(array $new_instance, array $old_instance = array()) |
|
| 93 | 93 | { |
| 94 | - $this->old_instance = array_merge($this->get_defaults(),$old_instance); |
|
| 95 | - $this->new_instance = array_merge($this->old_instance,$new_instance); |
|
| 94 | + $this->old_instance = array_merge($this->get_defaults(), $old_instance); |
|
| 95 | + $this->new_instance = array_merge($this->old_instance, $new_instance); |
|
| 96 | 96 | $this->final_instance = $this->new_instance; |
| 97 | 97 | |
| 98 | - foreach ( $this->components as $component ) |
|
| 98 | + foreach ($this->components as $component) |
|
| 99 | 99 | { |
| 100 | 100 | // Update individual fields, as well as the composite parent field. |
| 101 | - if ( $component instanceof ValueComponentInterface ) |
|
| 101 | + if ($component instanceof ValueComponentInterface) |
|
| 102 | 102 | { |
| 103 | - $this->update_component( $component ); |
|
| 103 | + $this->update_component($component); |
|
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | */ |
| 115 | 115 | public function reset() |
| 116 | 116 | { |
| 117 | - foreach( $this->components as $c ) |
|
| 117 | + foreach ($this->components as $c) |
|
| 118 | 118 | { |
| 119 | 119 | $c->value = $c->default; |
| 120 | 120 | $this->final_instance[$c->name] = $c->default; |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | * @param string $name |
| 140 | 140 | * @return UI\AbstractComponent |
| 141 | 141 | */ |
| 142 | - public function get_component( $name ) |
|
| 142 | + public function get_component($name) |
|
| 143 | 143 | { |
| 144 | 144 | return $this->components[$name]; |
| 145 | 145 | } |
@@ -161,21 +161,21 @@ discard block |
||
| 161 | 161 | * |
| 162 | 162 | * @param ValueComponentInterface $component The component to validate. |
| 163 | 163 | */ |
| 164 | - private function update_component( ValueComponentInterface $component ) |
|
| 164 | + private function update_component(ValueComponentInterface $component) |
|
| 165 | 165 | { |
| 166 | 166 | $component->value = $this->final_instance[$component->name]; |
| 167 | 167 | |
| 168 | 168 | // Skip if this field is disabled |
| 169 | - if( $this->is_disabled($component) ) |
|
| 169 | + if ($this->is_disabled($component)) |
|
| 170 | 170 | { |
| 171 | 171 | return; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | // Apply user-defined filter |
| 175 | - $this->filter( $component ); |
|
| 175 | + $this->filter($component); |
|
| 176 | 176 | |
| 177 | 177 | // Validate value |
| 178 | - $this->validate( $component ); |
|
| 178 | + $this->validate($component); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /** |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | * @param UI\AbstractComponent $component |
| 185 | 185 | * @return boolean |
| 186 | 186 | */ |
| 187 | - private function is_disabled( $component ) |
|
| 187 | + private function is_disabled($component) |
|
| 188 | 188 | { |
| 189 | 189 | return |
| 190 | 190 | $component instanceof DisableableComponentInterface && |
@@ -199,13 +199,13 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @param UI\AbstractComponent $component |
| 201 | 201 | */ |
| 202 | - private function filter( $component ) |
|
| 202 | + private function filter($component) |
|
| 203 | 203 | { |
| 204 | - if( $component instanceof FilterableComponentInterface ) |
|
| 204 | + if ($component instanceof FilterableComponentInterface) |
|
| 205 | 205 | { |
| 206 | 206 | $filter = $component->filter; |
| 207 | 207 | |
| 208 | - if( is_callable( $filter ) ) |
|
| 208 | + if (is_callable($filter)) |
|
| 209 | 209 | { |
| 210 | 210 | $component->value = \call_user_func_array($filter, array($this->final_instance[$component->name])); |
| 211 | 211 | $this->final_instance[$component->name] = $component->value; |
@@ -221,22 +221,22 @@ discard block |
||
| 221 | 221 | * |
| 222 | 222 | * @param UI\AbstractComponent $component The component to validate. |
| 223 | 223 | */ |
| 224 | - private function validate( $component ) |
|
| 224 | + private function validate($component) |
|
| 225 | 225 | { |
| 226 | - if( !($component instanceof ValidatableComponentInterface) ) return; |
|
| 226 | + if (!($component instanceof ValidatableComponentInterface)) return; |
|
| 227 | 227 | |
| 228 | 228 | $name = $component->name; |
| 229 | 229 | $validate = $component->validation; |
| 230 | 230 | |
| 231 | 231 | $component->validity = $component::VALID; |
| 232 | 232 | |
| 233 | - if(is_callable($validate)) |
|
| 233 | + if (is_callable($validate)) |
|
| 234 | 234 | { |
| 235 | 235 | $error = ''; |
| 236 | 236 | $valid = \call_user_func_array($validate, array($this->final_instance[$name], &$error)); |
| 237 | 237 | |
| 238 | 238 | // Invalid input, use old instance or default value |
| 239 | - if ( true !== $valid ) |
|
| 239 | + if (true !== $valid) |
|
| 240 | 240 | { |
| 241 | 241 | $this->errors[$name] = $error; |
| 242 | 242 | $component->value = $this->old_instance[$name]; |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | { |
| 256 | 256 | $defaults = array(); |
| 257 | 257 | |
| 258 | - foreach( $this->components as $component ) |
|
| 258 | + foreach ($this->components as $component) |
|
| 259 | 259 | { |
| 260 | 260 | $defaults[$component->name] = $component->default; |
| 261 | 261 | } |
@@ -8,9 +8,9 @@ |
||
| 8 | 8 | class Component_textarea |
| 9 | 9 | extends AbstractComponent |
| 10 | 10 | implements ValueComponentInterface, |
| 11 | - DisableableComponentInterface, |
|
| 12 | - FilterableComponentInterface, |
|
| 13 | - ValidatableComponentInterface |
|
| 11 | + DisableableComponentInterface, |
|
| 12 | + FilterableComponentInterface, |
|
| 13 | + ValidatableComponentInterface |
|
| 14 | 14 | { |
| 15 | 15 | public function default_model() |
| 16 | 16 | { |