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