Completed
Push — master ( 103643...7ff92a )
by Peter
03:35
created

PayloadTrait::getProperties()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 0
crap 3
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Payload;
12
13
use GpsLab\Component\Payload\Exception\UndefinedPropertyException;
14
15
trait PayloadTrait
16
{
17
    /**
18
     * @var array
19
     */
20
    private $payload = [];
21
22
    /**
23
     * @var array
24
     */
25
    private $properties = [];
26
27
    /**
28
     * @return array
29
     */
30 3
    final public function payload()
31
    {
32 3
        return $this->payload;
33
    }
34
35
    /**
36
     * @param array $payload
37
     */
38 9
    final protected function setPayload(array $payload)
39
    {
40 9
        foreach ($payload as $name => $value) {
41 9
            if (!in_array($name, $this->getProperties())) {
42 6
                throw UndefinedPropertyException::propertyOfClass($name, $this);
43
            }
44
45 3
            $this->$name = $value;
46 3
        }
47
48 3
        $this->payload = $payload;
49 3
    }
50
51
    /**
52
     * @return array
53
     */
54 9
    private function getProperties()
55
    {
56 9
        if (!$this->properties) {
57 9
            $ref = new \ReflectionClass($this);
58 9
            $properties = $ref->getProperties(\ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PUBLIC);
59 9
            foreach ($properties as $property) {
60 9
                $this->properties[] = $property->getName();
61 9
            }
62 9
        }
63
64 9
        return $this->properties;
65
    }
66
}
67