ArgumentNode.php
2 months ago
BooleanValueNode.php
2 months ago
DefinitionNode.php
2 months ago
DirectiveDefinitionNode.php
2 months ago
DirectiveNode.php
2 months ago
DocumentNode.php
2 months ago
EnumTypeDefinitionNode.php
2 months ago
EnumTypeExtensionNode.php
2 months ago
EnumValueDefinitionNode.php
2 months ago
EnumValueNode.php
2 months ago
ExecutableDefinitionNode.php
2 months ago
FieldDefinitionNode.php
2 months ago
FieldNode.php
2 months ago
FloatValueNode.php
2 months ago
FragmentDefinitionNode.php
2 months ago
FragmentSpreadNode.php
2 months ago
HasSelectionSet.php
2 months ago
InlineFragmentNode.php
2 months ago
InputObjectTypeDefinitionNode.php
2 months ago
InputObjectTypeExtensionNode.php
2 months ago
InputValueDefinitionNode.php
2 months ago
IntValueNode.php
2 months ago
InterfaceTypeDefinitionNode.php
2 months ago
InterfaceTypeExtensionNode.php
2 months ago
ListTypeNode.php
2 months ago
ListValueNode.php
2 months ago
Location.php
2 months ago
NameNode.php
2 months ago
NamedTypeNode.php
2 months ago
Node.php
2 months ago
NodeKind.php
2 months ago
NodeList.php
2 months ago
NonNullTypeNode.php
2 months ago
NullValueNode.php
2 months ago
ObjectFieldNode.php
2 months ago
ObjectTypeDefinitionNode.php
2 months ago
ObjectTypeExtensionNode.php
2 months ago
ObjectValueNode.php
2 months ago
OperationDefinitionNode.php
2 months ago
OperationTypeDefinitionNode.php
2 months ago
ScalarTypeDefinitionNode.php
2 months ago
ScalarTypeExtensionNode.php
2 months ago
SchemaDefinitionNode.php
2 months ago
SchemaExtensionNode.php
2 months ago
SelectionNode.php
2 months ago
SelectionSetNode.php
2 months ago
StringValueNode.php
2 months ago
TypeDefinitionNode.php
2 months ago
TypeExtensionNode.php
2 months ago
TypeNode.php
2 months ago
TypeSystemDefinitionNode.php
2 months ago
TypeSystemExtensionNode.php
2 months ago
UnionTypeDefinitionNode.php
2 months ago
UnionTypeExtensionNode.php
2 months ago
ValueNode.php
2 months ago
VariableDefinitionNode.php
2 months ago
VariableNode.php
2 months ago
Node.php
146 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Language\AST; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Error\InvariantViolation; |
| 6 | use Automattic\WooCommerce\Vendor\GraphQL\Utils\Utils; |
| 7 | |
| 8 | /** |
| 9 | * type Node = NameNode |
| 10 | * | DocumentNode |
| 11 | * | OperationDefinitionNode |
| 12 | * | VariableDefinitionNode |
| 13 | * | VariableNode |
| 14 | * | SelectionSetNode |
| 15 | * | FieldNode |
| 16 | * | ArgumentNode |
| 17 | * | FragmentSpreadNode |
| 18 | * | InlineFragmentNode |
| 19 | * | FragmentDefinitionNode |
| 20 | * | IntValueNode |
| 21 | * | FloatValueNode |
| 22 | * | StringValueNode |
| 23 | * | BooleanValueNode |
| 24 | * | EnumValueNode |
| 25 | * | ListValueNode |
| 26 | * | ObjectValueNode |
| 27 | * | ObjectFieldNode |
| 28 | * | DirectiveNode |
| 29 | * | ListTypeNode |
| 30 | * | NonNullTypeNode. |
| 31 | * |
| 32 | * @see \Automattic\WooCommerce\Vendor\GraphQL\Tests\Language\AST\NodeTest |
| 33 | */ |
| 34 | abstract class Node implements \JsonSerializable |
| 35 | { |
| 36 | public ?Location $loc = null; |
| 37 | |
| 38 | public string $kind; |
| 39 | |
| 40 | /** @param array<string, mixed> $vars */ |
| 41 | public function __construct(array $vars) |
| 42 | { |
| 43 | Utils::assign($this, $vars); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns a clone of this instance and all its children, except Location $loc. |
| 48 | * |
| 49 | * @throws \JsonException |
| 50 | * @throws InvariantViolation |
| 51 | * |
| 52 | * @return static |
| 53 | */ |
| 54 | public function cloneDeep(): self |
| 55 | { |
| 56 | return static::cloneValue($this); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @template TNode of Node |
| 61 | * @template TCloneable of TNode|NodeList<TNode>|Location|string |
| 62 | * |
| 63 | * @phpstan-param TCloneable $value |
| 64 | * |
| 65 | * @throws \JsonException |
| 66 | * @throws InvariantViolation |
| 67 | * |
| 68 | * @phpstan-return TCloneable |
| 69 | */ |
| 70 | protected static function cloneValue($value) |
| 71 | { |
| 72 | if ($value instanceof self) { |
| 73 | $cloned = clone $value; |
| 74 | foreach (get_object_vars($cloned) as $prop => $propValue) { |
| 75 | $cloned->{$prop} = static::cloneValue($propValue); // @phpstan-ignore argument.templateType |
| 76 | } |
| 77 | |
| 78 | return $cloned; |
| 79 | } |
| 80 | |
| 81 | if ($value instanceof NodeList) { |
| 82 | /** |
| 83 | * @phpstan-var TCloneable |
| 84 | * |
| 85 | * @phpstan-ignore varTag.nativeType (PHPStan is strict about template types and sees NodeList<TNode> as potentially different from TCloneable) |
| 86 | */ |
| 87 | return $value->cloneDeep(); |
| 88 | } |
| 89 | |
| 90 | return $value; |
| 91 | } |
| 92 | |
| 93 | /** @throws \JsonException */ |
| 94 | public function __toString(): string |
| 95 | { |
| 96 | return json_encode($this, JSON_THROW_ON_ERROR); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Improves upon the default serialization by: |
| 101 | * - excluding null values |
| 102 | * - excluding large reference values such as @see Location::$source. |
| 103 | * |
| 104 | * @return array<string, mixed> |
| 105 | */ |
| 106 | public function jsonSerialize(): array |
| 107 | { |
| 108 | return $this->toArray(); |
| 109 | } |
| 110 | |
| 111 | /** @return array<string, mixed> */ |
| 112 | public function toArray(): array |
| 113 | { |
| 114 | return self::recursiveToArray($this); |
| 115 | } |
| 116 | |
| 117 | /** @return array<string, mixed> */ |
| 118 | private static function recursiveToArray(Node $node): array |
| 119 | { |
| 120 | $result = []; |
| 121 | |
| 122 | foreach (get_object_vars($node) as $prop => $propValue) { |
| 123 | if ($propValue === null) { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | if ($propValue instanceof NodeList) { |
| 128 | $converted = []; |
| 129 | foreach ($propValue as $item) { |
| 130 | $converted[] = self::recursiveToArray($item); |
| 131 | } |
| 132 | } elseif ($propValue instanceof Node) { |
| 133 | $converted = self::recursiveToArray($propValue); |
| 134 | } elseif ($propValue instanceof Location) { |
| 135 | $converted = $propValue->toArray(); |
| 136 | } else { |
| 137 | $converted = $propValue; |
| 138 | } |
| 139 | |
| 140 | $result[$prop] = $converted; |
| 141 | } |
| 142 | |
| 143 | return $result; |
| 144 | } |
| 145 | } |
| 146 |