Passed
Pull Request — master (#42)
by
unknown
02:32 queued 42s
created

Schema::toArray()   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

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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