Completed
Push — master ( 0576d5...c4f381 )
by Askupa
01:58
created

AbstractComponent::enqueue_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
The extends keyword must be on the same line as the class name
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
Documentation introduced by
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 _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?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
    public function component_attributes()
100
    {
101
        return sprintf(
102
            'class="amarkal-ui-component amarkal-ui-component-%s" amarkal-component-name="%s"',
103
            $this->component_type,
0 ignored issues
show
Documentation introduced by
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 _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?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...
104
            $this->name
0 ignored issues
show
Documentation introduced by
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 _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?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...
105
        );
106
    }
107
    
108
    public function render( $echo = false )
109
    {
110
        $this->enqueue_scripts();
111
        return parent::render($echo);
112
    }
113
    
114
    public function enqueue_scripts()
115
    {
116
        \wp_enqueue_script('amarkal-ui');
117
        \wp_enqueue_style('amarkal-ui');
118
    }
119
    
120
    /**
121
     * A hook that is called once the component has been created.
122
     */
123
    protected function on_created() {}
124
}