Completed
Push — master ( 7a4b78...188c2e )
by Simone
02:26
created

MagicResource::properties()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 4
nop 0
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources;
13
14
use RuntimeException;
15
use Sensorario\Resources\Validators\ResourcesValidator;
16
17
abstract class MagicResource
18
{
19
    protected $properties = [];
20
21
    public function __call($functionName, $arguments)
22
    {
23
        $propertyName = strtolower($functionName);
24
25
        if ($this->hasProperty($propertyName)) {
26
            return $this->get($propertyName);
27
        }
28
29
        if (isset($this->defaults()[$propertyName])) {
30
            return $this->defaults()[$propertyName];
31
        }
32
33
        throw new RuntimeException(
34
            'Method `' . get_class($this)
35
            . '::' . $functionName 
36
            . '()` is not yet implemented'
37
        );
38
    }
39
40
    protected function __construct(
41
        array $properties,
42
        ResourcesValidator $validator
43
    ) {
44
        $this->properties = $properties;
45
46
        foreach ($properties as $k => $v) {
47
            if ('object' === gettype($v) && !isset($this->rules()[$k])) {
0 ignored issues
show
Documentation Bug introduced by
The method rules does not exist on object<Sensorario\Resources\MagicResource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
48
                throw new RuntimeException(
49
                    'When property `' . $k . '` is an object class, must be defined in Resources::rules()'
50
                );
51
            }
52
        }
53
54
        foreach ($properties as $name => $value) {
55
            if ('object' !== gettype($value)) {
56
                $properties[$name] = utf8_encode(
57
                    $value
58
                );
59
            }
60
        }
61
62
        $validator->validate($this);
1 ignored issue
show
Compatibility introduced by
$this of type object<Sensorario\Resources\MagicResource> is not a sub-type of object<Sensorario\Resources\Resource>. It seems like you assume a child class of the class Sensorario\Resources\MagicResource to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
63
    }
64
65
    public static function __callStatic($methodName, array $args)
66
    {
67
        $methodWhiteList = [
68
            'box',
69
            'allowedValues'
70
        ];
71
72
        $isMethodAllowed = in_array(
73
            $methodName,
74
            $methodWhiteList
75
        );
76
77
        if ($isMethodAllowed) {
78
            return new static(
79
                isset($args[0])
80
                ? $args[0]
81
                : [],
82
                new ResourcesValidator()
83
            );
84
        }
85
86
        throw new RuntimeException(
87
            'Invalid factory method `' . $methodName . '`'
88
        );
89
    }
90
91
    final public function hasProperty($propertyName)
92
    {
93
        return isset(
94
            $this->properties[$propertyName]
95
        );
96
    }
97
98
    final public function get($propertyName)
99
    {
100
        if ('' == $propertyName) {
101
            throw new RuntimeException(
102
                'Oops! Property name requested is empty string!!'
103
            );
104
        }
105
106
        if ($this->hasNotProperty($propertyName)) {
107
            if (isset($this->defaults()[$propertyName])) {
108
                return $this->defaults()[$propertyName];
109
            }
110
111
            throw new RuntimeException(
112
                'No value nor method `'
113
                . $propertyName
114
                . '` found in this resource'
115
            );
116
        }
117
118
        return $this->properties[$propertyName];
119
    }
120
121
    public static function defaults()
122
    {
123
        return [];
124
    }
125
126
    final public function hasNotProperty($propertyName)
127
    {
128
        return !$this->hasProperty($propertyName);
129
    }
130
131
    final public function hasProperties(array $properties)
132
    {
133
        foreach ($properties as $property) {
134
            if ($this->hasNotProperty($property)) {
135
                return false;
136
            }
137
        }
138
139
        return true;
140
    }
141
142
    final public function properties()
143
    {
144
        $properties = $this->properties;
145
146
        foreach ($properties as $k => $v) {
147
            if ('object' === gettype($v)) {
148
                if ($this->rules()[$k]['object'] === '\\Sensorario\\Resources\\Resource') {
0 ignored issues
show
Documentation Bug introduced by
The method rules does not exist on object<Sensorario\Resources\MagicResource>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
149
                    $properties[$k] = $v->properties();
150
                }
151
            }
152
        }
153
154
        return $properties;
155
    }
156
}
157