Passed
Pull Request — develop (#18)
by Csaba
02:46
created

Schema::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
namespace Fathomminds\Rest;
3
4
use Fathomminds\Rest\Contracts\ISchema;
5
use Fathomminds\Rest\Schema\TypeValidators\ValidatorFactory;
6
use Fathomminds\Rest\Exceptions\RestException;
7
8
abstract class Schema implements ISchema
9
{
10 55
    public function __construct($object = null)
11
    {
12 55
        if ($object === null) {
13 53
            return;
14
        }
15 18
        if (gettype($object) !== 'object') {
16 1
            throw new RestException('Schema constructor expects object or null as parameter', [
17 1
                'parameter' => $object,
18
            ]);
19
        }
20 17
        foreach (get_object_vars($object) as $name => $value) {
21 15
            $this->{$name} = $value;
22
        }
23 17
    }
24
25 2
    public function __get($name)
26
    {
27 2
        if (!isset($this->{$name})) {
28 1
            throw new RestException(
29 1
                'Trying to access undefined property ' . $name,
30 1
                []
31
            );
32
        }
33 1
        return $this->{$name};
34
    }
35
36
    abstract public function schema();
37
}
38