Property::accessor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Selector;
12
13
use Symfony\Component\PropertyAccess\PropertyAccess;
14
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
15
16
/**
17
 * Property Selector Class.
18
 *
19
 * @author Karel Osorio Ramírez <[email protected]>
20
 */
21
class Property extends Field
22
{
23
    /**
24
     * @var PropertyAccessorInterface
25
     */
26
    private static $accessor = null;
27
28
    /**
29
     * @return \Symfony\Component\PropertyAccess\PropertyAccessorInterface
30
     */
31
    protected static function accessor()
32
    {
33
        if (self::$accessor === null) {
34
            self::$accessor = PropertyAccess::createPropertyAccessor();
35
        }
36
37
        return self::$accessor;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function apply($value)
44
    {
45
        if (!\is_object($value)) {
46
            throw new \RuntimeException('Trying to get property of non-object');
47
        }
48
49
        if (!\property_exists($value, $this->name)) {
50
            throw new \RuntimeException(\sprintf('Undefined property %s::%s', \get_class($value), $this->name));
51
        }
52
53
        return self::accessor()->getValue($value, $this->name);
54
    }
55
}
56