Completed
Push — master ( 288216...e3ed33 )
by
unknown
09:26
created

Base::toStudlyCapsArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
crap 2
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 62
    public function __construct(array $data = array())
12
    {
13 62
        if (!empty($data)) {
14 38
            $ref = new \ReflectionObject($this);
15 38
            foreach ($ref->getProperties() as $property) {
16
                try {
17 38
                    $value = $this->getValue($data, $property->getName());
18 38
                    $property->setAccessible(true);
19 38
                    $property->setValue($this, $value);
20 38
                } catch (\OutOfBoundsException $e) {
21
                    // does nothing when we don't have a certain property in the $data array
22
                }
23 38
            }
24 38
        }
25 62
    }
26
27
    /**
28
     * Getter and Setters
29
     */
30 39
    public function __call($name, array $args)
31
    {
32 39
        if (in_array(substr($name, 0, 3), array('get', 'set'))) {
33 38
            $property = lcfirst(substr($name, 3));
34 38
            $ref = new \ReflectionObject($this);
35 38
            if ($ref->hasProperty($property)) {
36 37
                $property = $ref->getProperty($property);
37 37
                $property->setAccessible(true);
38 37
                switch (substr($name, 0, 3)) {
39 37
                    case 'get':
40 37
                        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 9
    public function toArray()
57
    {
58 9
        $ref = new \ReflectionObject($this);
59 9
        $export = array();
60
61 9
        foreach ($ref->getProperties() as $property) {
62 9
            $property->setAccessible(true);
63 9
            $name = strtolower(preg_replace('@([A-Z])@', '_\1', $property->getName()));
64 9
            $export[$name] = $property->getValue($this);
65
66 9
            if ($export[$name] instanceof self) {
67 1
                $export[$name] = $export[$name]->toArray();
68 1
            }
69
70 9
            if ($export[$name] instanceof \DateTime) {
71 1
                $export[$name] = $export[$name]->format('Y-m-d');
72 1
            }
73 9
        }
74
75 9
        return $export;
76
    }
77
78
    /**
79
     * Export transfer values as an array in studly caps format.
80
     * @return array
81
     */
82 1
    public function toStudlyCapsArray()
83
    {
84 1
        $export = $this->toArray();
85
86 1
        $result = array();
87 1
        foreach ($export as $key => $value) {
88 1
            $result[ucfirst($this->toCamelCase($key))] = $value;
89 1
        }
90
91 1
        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 38
    private function getValue(array $data, $property)
116
    {
117 38
        if (isset($data[$property])) {
118 20
            return $data[$property];
119
        }
120
121 38
        $property = strtolower(preg_replace('@([A-Z])@', '_\1', $property));
122 38
        if (isset($data[$property])) {
123 35
            return $data[$property];
124
        }
125
126 38
        throw new \OutOfBoundsException('');
127
    }
128
129 2
    private function toCamelCase($name)
130
    {
131 2
        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