Code Duplication    Length = 95-95 lines in 2 locations

generator/templates/static/Graph.php 1 location

@@ 12-106 (lines=95) @@
9
/**
10
 * @mixin \Spatie\SchemaOrg\Schema
11
 */
12
class Graph extends BaseType
13
{
14
    /** @var array */
15
    protected $hidden = [];
16
17
    public function __call(string $method, array $arguments)
18
    {
19
        if (is_callable([Schema::class, $method])) {
20
            $type = (new ReflectionClass(Schema::class))->getMethod($method)->getReturnType();
21
22
            $schema = $this->getOrCreate($type);
23
24
            if (isset($arguments[0]) && is_callable($arguments[0])) {
25
                call_user_func($arguments[0], $schema, $this);
26
27
                return $this;
28
            }
29
30
            return $schema;
31
        }
32
33
        throw new BadMethodCallException(sprintf('The method "%" does not exist on class "%s".', $method, get_class($this)));
34
    }
35
36
    public function add(Type $schema): self
37
    {
38
        $type = get_class($schema);
39
        
40
        if ($this->has($type)) {
41
            throw new InvalidArgumentException(sprintf('The graph already has an item of type "%s".', $type));
42
        }
43
44
        return $this->set($schema);
45
    }
46
47
    public function has(string $type): bool
48
    {
49
        return $this->offsetExists($type);
50
    }
51
52
    public function set(Type $schema)
53
    {
54
        return $this->setProperty(get_class($schema), $schema);
55
    }
56
57
    public function get(string $type): Type
58
    {
59
        if (! $this->has($type)) {
60
            throw new InvalidArgumentException(sprintf('The graph does not have an item of type "%s".', $type));
61
        }
62
63
        return $this->getProperty($type);
64
    }
65
66
    public function getOrCreate(string $type): Type
67
    {
68
        if (! is_subclass_of($type, Type::class)) {
69
            throw new InvalidArgumentException(sprintf('The given type "%s" is not an instance of "%s".', $type, Type::class));
70
        }
71
72
        if (! $this->has($type)) {
73
            $this->set(new $type());
74
        }
75
76
        return $this->get($type);
77
    }
78
79
    public function hide(string $type): self
80
    {
81
        $this->hidden[$type] = true;
82
83
        return $this;
84
    }
85
86
    public function show(string $type): self
87
    {
88
        $this->hidden[$type] = false;
89
90
        return $this;
91
    }
92
93
    public function toArray(): array
94
    {
95
        $properties = $this->getProperties();
96
        
97
        foreach ($this->hidden as $type => $hide) {
98
            if ($hide) {
99
                unset($properties[$type]);
100
            }
101
        }
102
103
        return [
104
            '@context' => $this->getContext(),
105
            '@graph' => $this->serializeProperty(array_values($properties)),
106
        ];
107
    }
108
}
109

src/Graph.php 1 location

@@ 12-106 (lines=95) @@
9
/**
10
 * @mixin Schema
11
 */
12
class Graph extends BaseType
13
{
14
    /** @var array */
15
    protected $hidden = [];
16
17
    public function __call(string $method, array $arguments)
18
    {
19
        if (is_callable([Schema::class, $method])) {
20
            $type = (new ReflectionClass(Schema::class))->getMethod($method)->getReturnType();
21
22
            $schema = $this->getOrCreate($type);
23
24
            if (isset($arguments[0]) && is_callable($arguments[0])) {
25
                call_user_func($arguments[0], $schema, $this);
26
27
                return $this;
28
            }
29
30
            return $schema;
31
        }
32
33
        throw new BadMethodCallException(sprintf('The method "%" does not exist on class "%s".', $method, get_class($this)));
34
    }
35
36
    public function add(Type $schema): self
37
    {
38
        $type = get_class($schema);
39
        if ($this->has($type)) {
40
            throw new InvalidArgumentException(sprintf('The graph already has an item of type "%s".', $type));
41
        }
42
43
        return $this->set($schema);
44
    }
45
46
    public function has(string $type): bool
47
    {
48
        return $this->offsetExists($type);
49
    }
50
51
    public function set(Type $schema)
52
    {
53
        return $this->setProperty(get_class($schema), $schema);
54
    }
55
56
    public function get(string $type): Type
57
    {
58
        if (! $this->has($type)) {
59
            throw new InvalidArgumentException(sprintf('The graph does not have an item of type "%s".', $type));
60
        }
61
62
        return $this->getProperty($type);
63
    }
64
65
    public function getOrCreate(string $type): Type
66
    {
67
        if (! is_subclass_of($type, Type::class)) {
68
            throw new InvalidArgumentException(sprintf('The given type "%s" is not an instance of "%s".', $type, Type::class));
69
        }
70
71
        if (! $this->has($type)) {
72
            $this->set(new $type());
73
        }
74
75
        return $this->get($type);
76
    }
77
78
    public function hide(string $type): self
79
    {
80
        $this->hidden[$type] = true;
81
82
        return $this;
83
    }
84
85
    public function show(string $type): self
86
    {
87
        $this->hidden[$type] = false;
88
89
        return $this;
90
    }
91
92
    public function toArray(): array
93
    {
94
        $properties = $this->getProperties();
95
        foreach ($this->hidden as $type => $hide) {
96
            if ($hide) {
97
                unset($properties[$type]);
98
            }
99
        }
100
101
        return [
102
            '@context' => $this->getContext(),
103
            '@graph' => $this->serializeProperty(array_values($properties)),
104
        ];
105
    }
106
}
107