Passed
Branch master (2bdc71)
by Csaba
02:38
created

Schema::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4
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 74
    public function __construct($object = null)
11
    {
12 74
        if ($object === null) {
13 72
            return;
14
        }
15 19
        if (gettype($object) !== 'object') {
16 1
            throw new RestException('Schema constructor expects object or null as parameter', [
17 1
                'parameter' => $object,
18
            ]);
19
        }
20 18
        foreach (get_object_vars($object) as $name => $value) {
21 16
            $this->{$name} = $value;
22
        }
23 18
    }
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 4
    public function toArray()
39
    {
40 4
        return json_decode(json_encode($this), true);
41
    }
42
}
43