Completed
Push — master ( 4bdd65...a99c6e )
by Askupa
01:24
created

Component_composite::set_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Amarkal\UI;
4
5
/**
6
 * Implements a Composite UI component.
7
 */
8
class Component_composite
9
extends AbstractComponent
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
10
implements ValueComponentInterface, 
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
11
           DisableableComponentInterface,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
12
           FilterableComponentInterface,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
13
           ValidatableComponentInterface
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
14
{
15
    /**
16
     * The list of child components.
17
     * 
18
     * @var UI\AbstractComponent[] 
19
     */
20
    private $components;
21
    
22
    public $component_type = 'composite';
23
    
24
    /**
25
     * The __set magic method is overridden here to apply value changes to 
26
     * child components.
27
     */
28
    public function __set( $name, $value )
29
    {
30
        parent::__set($name, $value);
31
        
32
        if( 'value' === $name )
33
        {
34
            $this->set_value($value);
35
        }
36
    }
37
    
38
    /**
39
     * Apply the value to each of the child components.
40
     * 
41
     * @param array $value
42
     */
43
    public function set_value( array $value )
44
    {
45
        foreach($value as $n => $v)
46
        {
47
            $component = $this->get_component($n);
48
            $component->value = $v;
49
        }
50
    }
51
    
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function default_model() 
56
    {
57
        return array(
58
            'name'          => '',
59
            'parent_name'   => '',
60
            'id'            => '',
61
            'disabled'      => false,
62
            'template'      => null,
63
            'components'    => array(),
64
            'filter'        => array($this, 'filter'),
65
            'validation'    => array($this, 'validation'),
66
        );
67
    }
68
    
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function required_arguments()
73
    {
74
        return array('name','components','template');
75
    }
76
    
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function get_template_path() 
81
    {
82
        return __DIR__.'/template.phtml';
83
    }
84
    
85
    /**
86
     * Parse the template by converting the name tokens into UI components.
87
     * 
88
     * @return string The parsed template
89
     */
90
    public function parse_template()
91
    {
92
        return preg_replace_callback('/\{\{([a-zA-Z\d-_]+)\}\}/', function($a){
93
            $component = $this->get_component($a[1]);
94
            return $component->render();
95
        }, $this->model['template']);
96
    }
97
    
98
    /**
99
     * If this is the root composite component, this will return the component's 
100
     * name. If this is a child composite component, this will return the
101
     * component's name as a key in the parent's array, i.e 'parent_name[child_name]'
102
     * 
103
     * @return string
104
     */
105
    public function get_name()
106
    {
107
        if('' !== $this->parent_name)
0 ignored issues
show
Documentation introduced by
The property parent_name does not exist on object<Amarkal\UI\Component_composite>. 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...
108
        {
109
            return "{$this->parent_name}[{$this->name}]";
0 ignored issues
show
Documentation introduced by
The property parent_name does not exist on object<Amarkal\UI\Component_composite>. 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...
Documentation introduced by
The property name does not exist on object<Amarkal\UI\Component_composite>. 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...
110
        }
111
        return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Amarkal\UI\Component_composite>. 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...
112
    }
113
114
    public function filter($v)
115
    {
116 View Code Duplication
        foreach($this->components as $component)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
        {
118
            if($component instanceof FilterableComponentInterface &&
119
               \is_callable($component->filter))
120
            {
121
                $n = $component->name;
122
                $v[$n] = $component->filter($v[$n]);
123
            }
124
        }
125
        
126
        return $v;
127
    }
128
129
    public function validation($v,&$e)
130
    {
131 View Code Duplication
        foreach($this->components as $component)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
        {
133
            if($component instanceof ValidatableComponentInterface &&
134
               \is_callable($component->validation))
135
            {
136
                $n = $component->name;
137
                if(!$component->validation($v[$n],$e))
138
                {
139
                    return false;
140
                }
141
            }
142
        }
143
        return true;
144
    }
145
    
146
    /**
147
     * Instantiate child UI components when created.
148
     */
149
    protected function on_created()
150
    {
151
        foreach( $this->model['components'] as $args )
152
        {
153
            $this->components[$args['name']] = $this->create_component($args);
154
        }
155
    }
156
    
157
    /**
158
     * 
159
     * @param type $args
160
     * @return type
161
     */
162
    private function create_component( $args )
163
    {
164
        $type = $args['type'];
165
        
166
        if('composite' === $type)
167
        {
168
            $args['parent_name'] = $this->get_name();
169
        }
170
        
171
        $c = \Amarkal\UI\ComponentFactory::create( $type, $args );
172
        
173
        // Apply the composite name template
174
        $c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template);
175
        
176
        return $c;
177
    }
178
    
179
    /**
180
     * Get a child component by name.
181
     * 
182
     * @param string $name
183
     * @return UI\AbstractComponent
184
     * @throws \RuntimeException If there's no child component corresponding to the given name
185
     */
186 View Code Duplication
    private function get_component( $name )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        if(!array_key_exists($name, $this->components))
189
        {
190
            throw new \RuntimeException("Composite sub-component not found with name $name");
191
        }
192
        return $this->components[$name];
193
    }
194
}