AST.php
2 months ago
ASTDefinitionBuilder.php
2 months ago
BreakingChangesFinder.php
2 months ago
BuildClientSchema.php
2 months ago
BuildSchema.php
2 months ago
InterfaceImplementations.php
2 months ago
LazyException.php
2 months ago
LexicalDistance.php
2 months ago
MixedStore.php
2 months ago
PairSet.php
2 months ago
PhpDoc.php
2 months ago
SchemaExtender.php
2 months ago
SchemaPrinter.php
2 months ago
TypeComparators.php
2 months ago
TypeInfo.php
2 months ago
Utils.php
2 months ago
Value.php
2 months ago
PairSet.php
44 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Utils; |
| 4 | |
| 5 | /** |
| 6 | * A way to keep track of pairs of things when the ordering of the pair does |
| 7 | * not matter. We do this by maintaining a sort of double adjacency sets. |
| 8 | */ |
| 9 | class PairSet |
| 10 | { |
| 11 | /** @var array<string, array<string, bool>> */ |
| 12 | private array $data = []; |
| 13 | |
| 14 | public function has(string $a, string $b, bool $areMutuallyExclusive): bool |
| 15 | { |
| 16 | $first = $this->data[$a] ?? null; |
| 17 | $result = $first !== null && isset($first[$b]) ? $first[$b] : null; |
| 18 | if ($result === null) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | // areMutuallyExclusive being false is a superset of being true, |
| 23 | // hence if we want to know if this PairSet "has" these two with no |
| 24 | // exclusivity, we have to ensure it was added as such. |
| 25 | if ($areMutuallyExclusive === false) { |
| 26 | return $result === false; |
| 27 | } |
| 28 | |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | public function add(string $a, string $b, bool $areMutuallyExclusive): void |
| 33 | { |
| 34 | $this->pairSetAdd($a, $b, $areMutuallyExclusive); |
| 35 | $this->pairSetAdd($b, $a, $areMutuallyExclusive); |
| 36 | } |
| 37 | |
| 38 | private function pairSetAdd(string $a, string $b, bool $areMutuallyExclusive): void |
| 39 | { |
| 40 | $this->data[$a] ??= []; |
| 41 | $this->data[$a][$b] = $areMutuallyExclusive; |
| 42 | } |
| 43 | } |
| 44 |