AllReadableTrait::getPropertyAccessor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Traits;
16
17
use Spaark\CompositeUtils\Service\RawPropertyAccessor;
18
19
/**
20
 * Classes with this trait will allow all of their properties to be read
21
 * externally, but will not allow writing
22
 */
23
trait AllReadableTrait
24
{
25
    /**
26
     * @var RawPropertyAccessor
27
     */
28
    protected $accessor;
29
30
    /**
31
     * Returns the RawPropertyAccessor for this object, or constructs
32
     * one on the fly if one does not yet exist
33
     *
34
     * @return RawPropertyAccessor
35
     */
36 63
    protected function getPropertyAccessor()
37
    {
38 63
        if (!$this->accessor)
39
        {
40 53
            $this->accessor = new RawPropertyAccessor($this);
41
        }
42
43 63
        return $this->accessor;
44
    }
45
46
    /**
47
     * Gets the value of a property using the RawPropertyAccessor
48
     *
49
     * @param string $property The property to get
50
     * @return mixed The property value
51
     */
52 63
    public function __get($property)
53
    {
54 63
        return $this->getPropertyAccessor()->getRawValue($property);
55
    }
56
}
57