Object   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 6
A __call() 0 9 2
A triggerError() 0 4 1
A importPropertiesFrom() 0 10 4
1
<?php
2
3
namespace ObjectLiteral;
4
5
/**
6
 * Object.
7
 *
8
 * @author Jose Celano
9
 */
10
class Object extends \stdClass
11
{
12
    public function __construct($value = null)
13
    {
14
        if (is_null($value) || $value == '') {
15
            return;
16
        }
17
18
        if (is_array($value)) {
19
            $object = (object)$value;
20
            $this->importPropertiesFrom($object);
21
            return;
22
        }
23
24
        if (is_string($value)) {
25
            $object = json_decode($value);
26
            if (!is_null($object)) {
27
                $this->importPropertiesFrom($object);
28
                return;
29
            }
30
        }
31
32
        throw new \InvalidArgumentException('Object can not be build from value ' . var_export($value, true));
33
    }
34
35
    public function __call($name, $args)
36
    {
37
        if (!is_callable($this->$name)) {
38
            $this->triggerError("Not callable property: $name", E_ERROR);
39
        }
40
41
        array_unshift($args, $this);
42
        return call_user_func_array($this->$name, $args);
43
    }
44
45
    protected function triggerError($errorMsg, $errorType = E_USER_NOTICE)
46
    {
47
        $this->triggerError($errorMsg, $errorType);
48
    }
49
50
    private function importPropertiesFrom($object)
51
    {
52
        foreach (get_object_vars($object) as $key => $value) {
53
            if (is_scalar($value) || is_callable($value)) {
54
                $this->$key = $value;
55
                continue;
56
            }
57
            $this->$key = new Object($value);
58
        }
59
    }
60
}
61