|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\Schema\Definition\Map; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Schema\Definition\ForeignKey; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Manage the set of foreign keys associated with the entity. |
|
11
|
|
|
* |
|
12
|
|
|
* @implements \IteratorAggregate<non-empty-string, ForeignKey> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class ForeignKeyMap implements \IteratorAggregate, \Countable |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array<non-empty-string, ForeignKey> |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
|
|
private array $foreignKeys = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Cloning. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __clone() |
|
25
|
|
|
{ |
|
26
|
|
|
foreach ($this->foreignKeys as $index => $foreignKey) { |
|
27
|
|
|
$this->foreignKeys[$index] = clone $foreignKey; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function has(ForeignKey $foreignKey): bool |
|
32
|
|
|
{ |
|
33
|
|
|
return isset($this->foreignKeys[$this->generateIdentifier($foreignKey)]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function set(ForeignKey $foreignKey): self |
|
37
|
|
|
{ |
|
38
|
|
|
$this->foreignKeys[$this->generateIdentifier($foreignKey)] = $foreignKey; |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function remove(ForeignKey $foreignKey): self |
|
44
|
|
|
{ |
|
45
|
|
|
unset($this->foreignKeys[$this->generateIdentifier($foreignKey)]); |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function count(): int |
|
51
|
|
|
{ |
|
52
|
|
|
return \count($this->foreignKeys); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getIterator(): \Traversable |
|
56
|
|
|
{ |
|
57
|
|
|
return new \ArrayIterator($this->foreignKeys); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return non-empty-string |
|
|
|
|
|
|
62
|
|
|
*/ |
|
63
|
|
|
private function generateIdentifier(ForeignKey $foreignKey): string |
|
64
|
|
|
{ |
|
65
|
|
|
return \sprintf( |
|
66
|
|
|
'%s:%s:%s', |
|
67
|
|
|
$foreignKey->getTable(), |
|
68
|
|
|
\implode(',', $foreignKey->getInnerColumns()), |
|
69
|
|
|
\implode(',', $foreignKey->getOuterColumns()) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|