Completed
Push — master ( c93e0a...f87b82 )
by Mario
09:30 queued 03:59
created

ValueObject::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Marek\Toggable\API\Toggl\Values;
4
5
use Marek\Toggable\API\Exception\PropertyNotFoundException;
6
use RuntimeException;
7
8
/**
9
 * Class ValueObject
10
 * @package Marek\Toggable\API\Toggl\Values
11
 */
12
abstract class ValueObject
13
{
14
    /**
15
     * Construct object optionally with a set of properties.
16
     *
17
     * Readonly properties values must be set using $properties as they are not writable anymore
18
     * after object has been created.
19
     *
20
     * @param array $properties
21
     *
22
     * @throws \Marek\Toggable\API\Exception\PropertyNotFoundException
23
     */
24 325
    public function __construct(array $properties = array())
25
    {
26 325
        foreach ($properties as $property => $value) {
27 279
            if (property_exists($this, $property)) {
28 278
                $this->$property = $value;
29
            } else {
30 279
                throw new PropertyNotFoundException($property, get_class($this));
31
            }
32
        }
33 325
    }
34
35
36
    /**
37
     * @param $property
38
     *
39
     * @return mixed
40
     *
41
     * @throws \Marek\Toggable\API\Exception\PropertyNotFoundException
42
     */
43 2
    public function __get($property)
44
    {
45 2
        if (property_exists($this, $property)) {
46 1
            return $this->$property;
47
        }
48 1
        throw new PropertyNotFoundException($property, get_class($this));
49
    }
50
51
    /**
52
     * Magic isset function handling isset() to non public properties.
53
     *
54
     * Returns true for all (public/)protected/private properties.
55
     *
56
     * @ignore This method is for internal use
57
     *
58
     * @param string $property Name of the property
59
     *
60
     * @return bool
61
     */
62 1
    public function __isset($property)
63
    {
64 1
        return property_exists($this, $property);
65
    }
66
}
67