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