Component_composite::get_component()   A
last analyzed

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 & name 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
        if( 'name' === $name )
38
        {
39
            $this->set_name($value);
40
        }
41
    }
42
    
43
    /**
44
     * Apply the value to each of the child components.
45
     * 
46
     * @param array $value
47
     * @return void
48
     */
49
    public function set_value( array $value )
50
    {
51
        foreach($value as $n => $v)
52
        {
53
            $component = $this->get_component($n);
54
            $component->value = $v;
55
        }
56
    }
57
58
    /**
59
     * Apply the new name to each of the child components.
60
     *
61
     * @param string $name
62
     * @return void
63
     */
64
    public function set_name( $name )
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        foreach($this->components as $c)
67
        {
68
            $c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template);
69
        }
70
    }
71
    
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function default_model() 
76
    {
77
        return array(
78
            'name'          => '',
79
            'parent_name'   => '',
80
            'id'            => '',
81
            'disabled'      => false,
82
            'template'      => null,
83
            'components'    => array(),
84
            'default'       => array(),
85
            'filter'        => array($this, 'filter'),
86
            'validation'    => array($this, 'validation'),
87
            'show'          => null
88
        );
89
    }
90
    
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function required_arguments()
95
    {
96
        return array('name','components','template');
97
    }
98
    
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function get_template_path() 
103
    {
104
        return __DIR__.'/template.phtml';
105
    }
106
    
107
    /**
108
     * Parse the template by converting the name tokens into UI components.
109
     * 
110
     * @return string The parsed template
111
     */
112
    public function parse_template()
113
    {
114
        $self = $this;
115
        return preg_replace_callback('/\{\{([a-zA-Z\d-_]+)\}\}/', function($a) use($self) {
116
            $component = $self->get_component($a[1]);
117
            return $component->render();
118
        }, $this->model['template']);
119
    }
120
    
121
    /**
122
     * If this is the root composite component, this will return the component's 
123
     * name. If this is a child composite component, this will return the
124
     * component's name as a key in the parent's array, i.e 'parent_name[child_name]'
125
     * 
126
     * @return string
127
     */
128
    public function get_name()
129
    {
130
        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...
131
        {
132
            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...
133
        }
134
        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...
135
    }
136
137
    public function filter($v)
138
    {
139 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...
140
        {
141
            if($component instanceof FilterableComponentInterface &&
142
               \is_callable($component->filter))
143
            {
144
                $n = $component->name;
145
                $v[$n] = $component->filter($v[$n]);
146
            }
147
        }
148
        
149
        return $v;
150
    }
151
152
    public function validation($v,&$e)
153
    {
154 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...
155
        {
156
            if($component instanceof ValidatableComponentInterface &&
157
               \is_callable($component->validation))
158
            {
159
                $n = $component->name;
160
                if(!$component->validation($v[$n],$e))
161
                {
162
                    return false;
163
                }
164
            }
165
        }
166
        return true;
167
    }
168
169
    /**
170
     * Get a child component by name.
171
     * 
172
     * @param string $name
173
     * @return UI\AbstractComponent
174
     * @throws \RuntimeException If there's no child component corresponding to the given name
175
     */
176
    public function get_component( $name )
177
    {
178
        if(!array_key_exists($name, $this->components))
179
        {
180
            throw new \RuntimeException("Composite sub-component not found with name $name");
181
        }
182
        return $this->components[$name];
183
    }
184
    
185
    /**
186
     * Instantiate child UI components when created.
187
     */
188
    protected function on_created()
189
    {
190
        foreach( $this->model['components'] as $args )
191
        {
192
            $this->components[$args['name']] = $this->create_component($args);
193
        }
194
    }
195
    
196
    /**
197
     * 
198
     * @param type $args
199
     * @return type
200
     */
201
    private function create_component( $args )
202
    {
203
        $type = $args['type'];
204
        
205
        if('composite' === $type)
206
        {
207
            $args['parent_name'] = $this->get_name();
208
        }
209
        
210
        $c = \Amarkal\UI\ComponentFactory::create( $type, $args );
211
        
212
        // Apply the composite name template
213
        $c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template);
214
        
215
        return $c;
216
    }
217
}