@@ -15,7 +15,9 @@ |
||
15 | 15 | /** |
16 | 16 | * Prevent loading the library more than once |
17 | 17 | */ |
18 | -if( defined( 'AMARKAL_UI' ) ) return; |
|
18 | +if( defined( 'AMARKAL_UI' ) ) { |
|
19 | + return; |
|
20 | +} |
|
19 | 21 | define( 'AMARKAL_UI', true ); |
20 | 22 | |
21 | 23 | /** |
@@ -10,25 +10,25 @@ discard block |
||
10 | 10 | * @link https://github.com/askupasoftware/amarkal-ui |
11 | 11 | * @copyright 2017 Askupa Software |
12 | 12 | */ |
13 | -defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
|
13 | +defined('ABSPATH') or die('No script kiddies please!'); |
|
14 | 14 | |
15 | 15 | /** |
16 | 16 | * Prevent loading the library more than once |
17 | 17 | */ |
18 | -if( defined( 'AMARKAL_UI' ) ) return; |
|
19 | -define( 'AMARKAL_UI', true ); |
|
18 | +if (defined('AMARKAL_UI')) return; |
|
19 | +define('AMARKAL_UI', true); |
|
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Load required classes if not using composer |
23 | 23 | */ |
24 | -if( !class_exists('Composer\\Autoload\\ClassLoader') ) |
|
24 | +if (!class_exists('Composer\\Autoload\\ClassLoader')) |
|
25 | 25 | { |
26 | 26 | require_once 'Renderer.php'; |
27 | 27 | require_once 'AbstractController.php'; |
28 | 28 | require_once 'AbstractComponent.php'; |
29 | 29 | } |
30 | 30 | |
31 | -if(!function_exists('amarkal_ui_render')) |
|
31 | +if (!function_exists('amarkal_ui_render')) |
|
32 | 32 | { |
33 | 33 | /** |
34 | 34 | * Render a UI component. |
@@ -38,14 +38,14 @@ discard block |
||
38 | 38 | * @param array $props The component's properties |
39 | 39 | * @return string The rendered HTML |
40 | 40 | */ |
41 | - function amarkal_ui_render( $type, array $props = array() ) |
|
41 | + function amarkal_ui_render($type, array $props = array()) |
|
42 | 42 | { |
43 | 43 | $renderer = Amarkal\UI\Renderer::get_instance(); |
44 | - return $renderer->render_component( $type, $props ); |
|
44 | + return $renderer->render_component($type, $props); |
|
45 | 45 | } |
46 | 46 | } |
47 | 47 | |
48 | -if(!function_exists('amarkal_ui_register_component')) |
|
48 | +if (!function_exists('amarkal_ui_register_component')) |
|
49 | 49 | { |
50 | 50 | /** |
51 | 51 | * Register a custom UI component. The registered component's class should |
@@ -55,9 +55,9 @@ discard block |
||
55 | 55 | * to one of the core component's type, it will override the core component. |
56 | 56 | * @param string $class_name The component's class name. |
57 | 57 | */ |
58 | - function amarkal_ui_register_component( $type, $class_name ) |
|
58 | + function amarkal_ui_register_component($type, $class_name) |
|
59 | 59 | { |
60 | 60 | $renderer = Amarkal\UI\Renderer::get_instance(); |
61 | - $renderer->register_component( $type, $class_name ); |
|
61 | + $renderer->register_component($type, $class_name); |
|
62 | 62 | } |
63 | 63 | } |
64 | 64 | \ No newline at end of file |
@@ -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() |
@@ -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 | /** |
@@ -18,16 +18,16 @@ discard block |
||
18 | 18 | */ |
19 | 19 | public static function get_instance() |
20 | 20 | { |
21 | - if( null === static::$instance ) |
|
21 | + if (null === static::$instance) |
|
22 | 22 | { |
23 | 23 | static::$instance = new static(); |
24 | 24 | } |
25 | 25 | return static::$instance; |
26 | 26 | } |
27 | 27 | |
28 | - public function render_component( $type, array $props = array() ) |
|
28 | + public function render_component($type, array $props = array()) |
|
29 | 29 | { |
30 | - $component = $this->create_component( $type, $props ); |
|
30 | + $component = $this->create_component($type, $props); |
|
31 | 31 | return $component->render(); |
32 | 32 | } |
33 | 33 | |
@@ -42,9 +42,9 @@ discard block |
||
42 | 42 | * @param string $class_name |
43 | 43 | * @throws \RuntimeException |
44 | 44 | */ |
45 | - public function register_component( $type, $class_name ) |
|
45 | + public function register_component($type, $class_name) |
|
46 | 46 | { |
47 | - if( !in_array($type, $this->registered_components) ) |
|
47 | + if (!in_array($type, $this->registered_components)) |
|
48 | 48 | { |
49 | 49 | $this->registered_components[$type] = $class_name; |
50 | 50 | } |
@@ -57,12 +57,12 @@ discard block |
||
57 | 57 | * @param type $props |
58 | 58 | * @return type |
59 | 59 | */ |
60 | - private function create_component( $type, $props ) |
|
60 | + private function create_component($type, $props) |
|
61 | 61 | { |
62 | 62 | try { |
63 | - $component = $this->create_registered_component ($type, $props ); |
|
63 | + $component = $this->create_registered_component($type, $props); |
|
64 | 64 | } catch (\RuntimeException $ex) { |
65 | - $component = $this->create_core_component ($type, $props ); |
|
65 | + $component = $this->create_core_component($type, $props); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | return $component; |
@@ -74,15 +74,15 @@ discard block |
||
74 | 74 | * @param type $props |
75 | 75 | * @throws \RuntimeException |
76 | 76 | */ |
77 | - private function create_core_component( $type, $props ) |
|
77 | + private function create_core_component($type, $props) |
|
78 | 78 | { |
79 | 79 | $file_name = __DIR__."/components/$type/controller.php"; |
80 | 80 | $class_name = 'Amarkal\\UI\\Component_'.$type; |
81 | 81 | |
82 | 82 | // Load one of the core components |
83 | - if(!class_exists($class_name)) |
|
83 | + if (!class_exists($class_name)) |
|
84 | 84 | { |
85 | - if( file_exists( $file_name ) ) |
|
85 | + if (file_exists($file_name)) |
|
86 | 86 | { |
87 | 87 | require_once $file_name; |
88 | 88 | } |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | * @param type $props |
102 | 102 | * @throws \RuntimeException |
103 | 103 | */ |
104 | - private function create_registered_component( $type, $props ) |
|
104 | + private function create_registered_component($type, $props) |
|
105 | 105 | { |
106 | - if(in_array($type, $this->registered_components)) |
|
106 | + if (in_array($type, $this->registered_components)) |
|
107 | 107 | { |
108 | 108 | $class_name = $this->registered_components[$type]; |
109 | 109 | return new $class_name($props); |
@@ -47,8 +47,9 @@ discard block |
||
47 | 47 | if( !in_array($type, $this->registered_components) ) |
48 | 48 | { |
49 | 49 | $this->registered_components[$type] = $class_name; |
50 | + } else { |
|
51 | + throw new \RuntimeException("A component of type '$type' has already been registered."); |
|
50 | 52 | } |
51 | - else throw new \RuntimeException("A component of type '$type' has already been registered."); |
|
52 | 53 | } |
53 | 54 | |
54 | 55 | /** |
@@ -85,8 +86,7 @@ discard block |
||
85 | 86 | if( file_exists( $file_name ) ) |
86 | 87 | { |
87 | 88 | require_once $file_name; |
88 | - } |
|
89 | - else |
|
89 | + } else |
|
90 | 90 | { |
91 | 91 | throw new \RuntimeException("A component of type '$type' does not exist."); |
92 | 92 | } |
@@ -15,9 +15,9 @@ discard block |
||
15 | 15 | * |
16 | 16 | * @param array $model |
17 | 17 | */ |
18 | - public function __construct( array $model = array() ) |
|
18 | + public function __construct(array $model = array()) |
|
19 | 19 | { |
20 | - $this->set_model( $model ); |
|
20 | + $this->set_model($model); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | /** |
@@ -27,9 +27,9 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @return mixed the argument's value. |
29 | 29 | */ |
30 | - public function __get( $name ) |
|
30 | + public function __get($name) |
|
31 | 31 | { |
32 | - if( isset( $this->model[$name] ) ) |
|
32 | + if (isset($this->model[$name])) |
|
33 | 33 | { |
34 | 34 | return $this->model[$name]; |
35 | 35 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * |
44 | 44 | * @return mixed the settings' argument value. |
45 | 45 | */ |
46 | - public function __set( $name, $value ) |
|
46 | + public function __set($name, $value) |
|
47 | 47 | { |
48 | 48 | $this->model[$name] = $value; |
49 | 49 | } |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | * |
64 | 64 | * @return array |
65 | 65 | */ |
66 | - public function set_model( $model ) |
|
66 | + public function set_model($model) |
|
67 | 67 | { |
68 | 68 | $this->model = $model; |
69 | 69 | } |
@@ -81,22 +81,22 @@ discard block |
||
81 | 81 | * @return string The rendered template. |
82 | 82 | * @throws TemplateNotFoundException Thrown if the template file cannot be found. |
83 | 83 | */ |
84 | - public function render( $echo = false ){ |
|
84 | + public function render($echo = false) { |
|
85 | 85 | |
86 | 86 | $rendered_html = ''; |
87 | 87 | |
88 | - if( file_exists( $this->get_template_path() ) ) |
|
88 | + if (file_exists($this->get_template_path())) |
|
89 | 89 | { |
90 | 90 | ob_start(); |
91 | - include( $this->get_template_path() ); |
|
91 | + include($this->get_template_path()); |
|
92 | 92 | $rendered_html = ob_get_clean(); |
93 | 93 | } |
94 | 94 | else |
95 | 95 | { |
96 | - throw new \RuntimeException( "Error: cannot render HTML, template file not found at " . $this->get_template_path() ); |
|
96 | + throw new \RuntimeException("Error: cannot render HTML, template file not found at ".$this->get_template_path()); |
|
97 | 97 | } |
98 | 98 | |
99 | - if( !$echo ) |
|
99 | + if (!$echo) |
|
100 | 100 | { |
101 | 101 | return $rendered_html; |
102 | 102 | } |
@@ -90,8 +90,7 @@ |
||
90 | 90 | ob_start(); |
91 | 91 | include( $this->get_template_path() ); |
92 | 92 | $rendered_html = ob_get_clean(); |
93 | - } |
|
94 | - else |
|
93 | + } else |
|
95 | 94 | { |
96 | 95 | throw new \RuntimeException( "Error: cannot render HTML, template file not found at " . $this->get_template_path() ); |
97 | 96 | } |