| @@ 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 | ||
| @@ 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 | ||