Completed
Pull Request — master (#2)
by
unknown
04:25
created

Base::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Iris\Transfer;
4
5
abstract class Base implements \ArrayAccess
6
{
7
8
    /**
9
     * Base constructor - you may use an array to initialize the Transfer
10
     */
11 58
    public function __construct(array $data = array())
12
    {
13 58
        if (!empty($data)) {
14 35
            $ref = new \ReflectionObject($this);
15 35
            foreach ($ref->getProperties() as $property) {
16
                try {
17 35
                    $value = $this->getValue($data, $property->getName());
18 35
                    $property->setAccessible(true);
19 35
                    $property->setValue($this, $value);
20 35
                } catch (\OutOfBoundsException $e) {
21
                    // does nothing when we don't have a certain property in the $data array
22
                }
23 35
            }
24 35
        }
25 58
    }
26
27
    /**
28
     * Getter and Setters
29
     */
30 37
    public function __call($name, array $args)
31
    {
32 37
        if (in_array(substr($name, 0, 3), array('get', 'set'))) {
33 36
            $property = lcfirst(substr($name, 3));
34 36
            $ref = new \ReflectionObject($this);
35 36
            if ($ref->hasProperty($property)) {
36 35
                $property = $ref->getProperty($property);
37 35
                $property->setAccessible(true);
38 35
                switch (substr($name, 0, 3)) {
39 35
                    case 'get':
40 35
                        return $property->getValue($this);
41 2
                    case 'set':
42 2
                    default:
43 2
                        $property->setValue($this, $args[0]);
44 2
                        return $this;
45
                }
46
            }
47 1
            throw new \InvalidArgumentException(sprintf('Property %s does not exists', $property));
48
        }
49 1
        throw new \InvalidArgumentException('Method does not exists');
50
    }
51
52
    /**
53
     * Export transfer values as an array
54
     * @return array
55
     */
56 7
    public function toArray()
57
    {
58 7
        $ref = new \ReflectionObject($this);
59 7
        $export = array();
60
61 7
        foreach ($ref->getProperties() as $property) {
62 7
            $property->setAccessible(true);
63 7
            $name = strtolower(preg_replace('@([A-Z])@', '_\1', $property->getName()));
64 7
            $export[$name] = $property->getValue($this);
65
66 7
            if ($export[$name] instanceof self) {
67 1
                $export[$name] = $export[$name]->toArray();
68 1
            }
69
70 7
            if ($export[$name] instanceof \DateTime) {
71
                $export[$name] = $export[$name]->format('Y-m-d');
72
            }
73 7
        }
74
75 7
        return $export;
76
    }
77
78
    /**
79
     * Export transfer values as an array in studly caps format.
80
     * @return array
81
     */
82
    public function toStudlyCapsArray()
83
    {
84
        $export = $this->toArray();
85
86
        $result = array();
87
        foreach ($export as $key => $value) {
88
            $result[ucfirst($this->toCamelCase($key))] = $value;
89
        }
90
91
        return $result;
92
    }
93
94
    /**
95
     * toArray with unset null fields
96
     * @return array
97
     */
98 6
    public function toCleanArray()
99
    {
100 6
        $export = $this->toArray();
101
102 6
        $cleanExport = array();
103 6
        foreach ($export as $key => $value) {
104 6
            if ($value) {
105 5
                $cleanExport[$key] = $value;
106 5
            }
107 6
        }
108
109 6
        return $cleanExport;
110
    }
111
112
    /**
113
     * Extract transfer $property value from $data
114
     */
115 35
    private function getValue(array $data, $property)
116
    {
117 35
        if (isset($data[$property])) {
118 18
            return $data[$property];
119
        }
120
121 35
        $property = strtolower(preg_replace('@([A-Z])@', '_\1', $property));
122 35
        if (isset($data[$property])) {
123 32
            return $data[$property];
124
        }
125
126 35
        throw new \OutOfBoundsException('');
127
    }
128
129 1
    private function toCamelCase($name)
130
    {
131 1
        return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $name))));
132
    }
133
134 1
    public function offsetGet($offset)
135
    {
136 1
        $property = $this->toCamelCase($offset);
137 1
        return call_user_func(array($this, 'get' . ucfirst($property)));
138
    }
139
140 1
    public function offsetSet($offset, $value)
141
    {
142 1
        $property = $this->toCamelCase($offset);
143 1
        return call_user_func(array($this, 'set' . ucfirst($property)), $value);
144
    }
145
146 1
    public function offsetExists($offset)
147
    {
148 1
        $property = $this->toCamelCase($offset);
149 1
        return (new \ReflectionObject($this))->hasProperty($property);
150
    }
151
152 1
    public function offsetUnset($offset)
153
    {
154 1
        $this->offsetSet($offset, null);
155 1
    }
156
}
157