Completed
Pull Request — master (#92)
by Tom
13:51
created

LdJson::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
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
        return static::generateType($data);
30
    }
31
32
    protected static function generateType(array $data): BaseType
33
    {
34
        if (!isset($data['@type'])) {
35
            throw new InvalidType('No type given in schema.');
36
        }
37
38
        $fqcn = '\\Spatie\\SchemaOrg\\' . $data['@type'];
39
        unset($data['@type']);
40
        if (!static::isValidType($fqcn)) {
41
            throw new InvalidType(sprintf('The given type "%s" is not an instance of "%s".', $fqcn, BaseType::class));
42
        }
43
        /** @var BaseType $type */
44
        $type = new $fqcn();
45
46
        foreach ($data as $property => $value) {
47
            if(is_array($value)) {
48
                if(isset($value['@type'])) {
49
                    $type->setProperty($property, static::generateType($value));
50
                    continue;
51
                }
52
53
                $type->setProperty($property, array_map(function ($value) {
54
                    if(is_array($value) && isset($value['@type'])) {
55
                        return static::generateType($value);
56
                    }
57
58
                    return $value;
59
                }, $value));
60
                continue;
61
            }
62
63
            $type->setProperty($property, $value);
64
        }
65
66
        return $type;
67
    }
68
69
    protected static function isValidType(string $fqcn): bool
70
    {
71
        return class_exists($fqcn) && is_a($fqcn, BaseType::class);
72
    }
73
}
74