Completed
Pull Request — master (#92)
by Tom
13:35 queued 12:09
created

LdJson::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Spatie\SchemaOrg\Decoder;
4
5
use InvalidArgumentException;
6
use Spatie\SchemaOrg\BaseType;
7
use Spatie\SchemaOrg\Exceptions\InvalidType;
8
9 View Code Duplication
class LdJson
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    public static function fromJson(string $json): BaseType
12
    {
13
        $data = json_decode($json, true);
14
15
        return static::fromArray($data);
16
    }
17
18
    public static function fromArray(array $data): BaseType
19
    {
20
        if (! isset($data['@context'])) {
21
            throw new InvalidArgumentException('No context given in schema.');
22
        }
23
24
        if (strpos($data['@context'], 'schema.org') === false) {
25
            throw new InvalidArgumentException('The given @context is not valid.');
26
        }
27
28
        unset($data['@context']);
29
30
        return static::generateType($data);
31
    }
32
33
    protected static function generateType(array $data): BaseType
34
    {
35
        if (! isset($data['@type'])) {
36
            throw new InvalidType('No type given in schema.');
37
        }
38
39
        $fqcn = '\\Spatie\\SchemaOrg\\'.$data['@type'];
40
        unset($data['@type']);
41
        if (! static::isValidType($fqcn)) {
42
            throw new InvalidType(sprintf('The given type "%s" is not an instance of "%s".', $fqcn, BaseType::class));
43
        }
44
        /** @var BaseType $type */
45
        $type = new $fqcn();
46
47
        foreach ($data as $property => $value) {
48
            if (is_array($value)) {
49
                if (isset($value['@type'])) {
50
                    $type->setProperty($property, static::generateType($value));
51
                    continue;
52
                }
53
54
                $type->setProperty($property, array_map(function ($value) {
55
                    if (is_array($value) && isset($value['@type'])) {
56
                        return static::generateType($value);
57
                    }
58
59
                    return $value;
60
                }, $value));
61
                continue;
62
            }
63
64
            $type->setProperty($property, $value);
65
        }
66
67
        return $type;
68
    }
69
70
    protected static function isValidType(string $fqcn): bool
71
    {
72
        return class_exists($fqcn) && is_a($fqcn, BaseType::class);
73
    }
74
}
75