Accessor::factory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
17
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
21
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
 * POSSIBILITY OF SUCH DAMAGE.
23
 *
24
 * PHP Version 5.3
25
 * 
26
 * @category  Database
27
 * @package   Fwk\Db
28
 * @author    Julien Ballestracci <[email protected]>
29
 * @copyright 2011-2012 Julien Ballestracci <[email protected]>
30
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
31
 * @link      http://www.phpfwk.com
32
 */
33
namespace Fwk\Db;
34
35
use Fwk\Db\RelationInterface;
36
37
/**
38
 * This class is a simple accessor for values of objects
39
 *
40
 * If getters/setters are found, we use them first.
41
 * If not, we try to directly get/set the value (\ArrayAccess or \stdClass)
42
 *
43
 * @category Utilities
44
 * @package  Fwk\Db
45
 * @author   Julien Ballestracci <[email protected]>
46
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
47
 * @link     http://www.phpfwk.com
48
 */
49
class Accessor
50
{
51
    /**
52
     * The object to access
53
     *
54
     * @var mixed
55
     */
56
    protected $object;
57
58
    /**
59
     * Reflector for the object
60
     *
61
     * @var \ReflectionObject
62
     */
63
    protected $reflector;
64
65
    /**
66
     * Override properties visibility ?
67
     *
68
     * @var boolean
69
     */
70
    protected $force = false;
71
72
    /**
73
     * Constructor
74
     *
75
     * @param mixed $object The object we want to access
76
     * 
77
     * @throws \InvalidArgumentException if $object is not an object
78
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
79
     */
80
    public function __construct($object)
81
    {
82
        if (!is_object($object)) {
83
            throw new \InvalidArgumentException("Argument is not an object");
84
        }
85
        
86
        $this->object   = $object;
87
    }
88
89
    /**
90
     * Returns all relations from an entity
91
     *
92
     * @return array<RelationInterface> Found relations
93
     */
94
    public function getRelations()
95
    {
96
        $values     = $this->toArray();
97
        $final      = array();
98
99
        foreach ($values as $key => $value) {
100
            if ($value instanceof RelationInterface) {
101
                $final[$key]   = $value;
102
            }
103
        }
104
105
        return $final;
106
    }
107
108
    /**
109
     * Static toArray() modifier to handle objects and relations.
110
     * {@see Accessor::toArray()}
111
     * 
112
     * @param mixed $value Actual value 
113
     * 
114
     * @return mixed Filtered value
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer|double|string|null|boolean|resource|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
115
     */
116
    public function everythingAsArrayModifier($value)
117
    {
118
        if ($value instanceof RelationInterface) {
119
            /**
120
             * @todo Boucle infinie!!!  $value->hasChanged()
121
             */
122
            $value  = sprintf(
123
                'relation:%s-%s', 
124
                (string)\microtime(), 
125
                (string)$value->isFetched()
126
            );
127
        }
128
129
        if (is_object($value)) {
130
            $accessor   = self::factory($value);
131
            $value      = $accessor->toArray(array($this, __METHOD__));
132
        }
133
134
        if (is_array($value)) {
135
            foreach ($value as $key => $val) {
136
                $value[$key] = $val;
137
            }
138
        }
139
140
        return $value;
141
    }
142
143
    /**
144
     * Try to retrieve a value from the object
145
     *
146
     * @param string $key Propertie's name
147
     * 
148
     * @return mixed Actual value if reached or false 
149
     */
150
    public function get($key)
151
    {
152
        $obj        = $this->object;
153
        $getter     = "get". ucfirst($key);
154
155
        if (\method_exists($obj, $getter) && \is_callable(array($obj, $getter))) {
156
            return $obj->{$getter}();
157
        } elseif ($obj instanceof \stdClass && isset($obj->{$key})) {
158
            return  $obj->{$key};
159
        } elseif ($obj instanceof \ArrayAccess && $obj->offsetExists($key)) {
160
            return  $obj->offsetGet($key);
161
        } else {
162
            $reflector  = $this->getReflector();
163
            try {
164
                $prop   = $reflector->getProperty($key);
165
                if (($prop->isPrivate() || $prop->isProtected()) && $this->force) {
166
                    $prop->setAccessible(true);
167
                }
168
169
                return $prop->getValue($obj);
170
            } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
171
            }
172
        }
173
174
        return false;
175
    }
176
177
    /**
178
     * Try to set a value
179
     *
180
     * @param string $key   Propertie's name
181
     * @param mixed  $value Desired value
182
     * 
183
     * @return boolean true if successful
184
     */
185
    public function set($key, $value)
0 ignored issues
show
Coding Style introduced by
function set() does not seem to conform to the naming convention (^(?:is|has|should|may|su...ster|unregister|exists)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
186
    {
187
        $obj        = $this->object;
188
        $setter     = "set". ucfirst($key);
189
190
        if (\method_exists($obj, $setter) && \is_callable(array($obj, $setter))) {
191
            $obj->{$setter}($value);
192
            return true;
193
        }
194
195
        if ($obj instanceof \stdClass) {
196
            $obj->{$key}    = $value;
197
            return true;
198
        }
199
200
        if ($obj instanceof \ArrayAccess) {
201
            $obj->offsetSet($key, $value);
202
            return true;
203
        }
204
205
        $reflector  = $this->getReflector();
206
        try {
207
            $prop   = $reflector->getProperty($key);
208
            if (($prop->isPrivate() || $prop->isProtected()) && $this->force) {
209
                $prop->setAccessible(true);
210
            }
211
212
            if ($prop->isPublic() || $this->force === true) {
213
                $prop->setValue($obj, $value);
214
                return true;
215
            }
216
        } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
217
        }
218
219
        return false;
220
    }
221
222
    /**
223
     * Set multiple values at once
224
     *
225
     * @param array $values Array of keys->values to be set
226
     *
227
     * @return void
228
     */
229
    public function setValues(array $values)
230
    {
231
        foreach ($values as $key => $value) {
232
            $this->set($key, $value);
233
        }
234
    }
235
236
    /**
237
     * Gets a reflector for the object
238
     *
239
     * @return \ReflectionObject
240
     */
241
    public function getReflector()
242
    {
243
        if (!isset($this->reflector)) {
244
            $this->reflector = new \ReflectionObject($this->object);
245
        }
246
247
        return $this->reflector;
248
    }
249
250
    /**
251
     * Make an array of keys->values (eventually filtered by $modifier) from
252
     * object's properties.
253
     * 
254
     * @param mixed $modifier Filtering callable
255
     * 
256
     * @return array The resulting array
257
     */
258
    public function toArray($modifier = null)
259
    {
260
        $reflector  = $this->getReflector();
261
        $final      = array();
262
263
        foreach ($reflector->getProperties() as $property) {
264
            $value = $this->get($property->getName());
265
266
            if (\is_callable($modifier)) {
267
                $value  = \call_user_func_array($modifier, array($value));
268
            }
269
270
            $final[$property->getName()] = $value;
271
        }
272
273
        return $final;
274
    }
275
276
    /**
277
     * Produces a unique hash code based on values
278
     *
279
     * @param string $algo Desired algorythm
280
     * 
281
     * @return string
282
     */
283
    public function hashCode($algo  = 'crc32')
284
    {
285
        $old    = $this->force;
286
        $this->overrideVisibility(true);
287
        $array  = $this->toArray(array($this, 'everythingAsArrayModifier'));
288
        \ksort($array);
289
        $str    = \get_class($this->object);
290
291
        foreach ($array as $value) {
292
            $str .= (is_array($value) ? json_encode($value) : $value);
293
        }
294
295
        $this->overrideVisibility($old);
296
297
        return \hash($algo, $str);
298
    }
299
300
    /**
301
     * Factory utility
302
     * 
303
     * @param mixed $object The object we want to access
304
     *
305
     * @return Accessor
306
     */
307
    public static function factory($object)
308
    {
309
        return new static($object);
310
    }
311
312
    /**
313
     * Should we force properties visibility ?
314
     * 
315
     * @param boolean $bool yes or no
316
     * 
317
     * @return void
318
     */
319
    public function overrideVisibility($bool)
320
    {
321
        $this->force = (bool) $bool;
322
    }
323
}