Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
41498ac5 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
41498ac5 TO |
7 | | This work is published under the GNU AGPLv3 license with some | |
8 | | permitted exceptions and without any warranty. For full license | | |
9 | | and copyright information, see https://civicrm.org/licensing | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
19 | ||
19b53e5b C |
20 | namespace Civi\Api4\Service\Schema; |
21 | ||
22 | use Civi\Api4\Service\Schema\Joinable\Joinable; | |
23 | ||
24 | class Table { | |
25 | ||
26 | /** | |
27 | * @var string | |
28 | */ | |
29 | protected $name; | |
30 | ||
31 | /** | |
32 | * @var \Civi\Api4\Service\Schema\Joinable\Joinable[] | |
33 | * Array of links to other tables | |
34 | */ | |
35 | protected $tableLinks = []; | |
36 | ||
37 | /** | |
38 | * @param $name | |
39 | */ | |
40 | public function __construct($name) { | |
41 | $this->name = $name; | |
42 | } | |
43 | ||
44 | /** | |
45 | * @return string | |
46 | */ | |
47 | public function getName() { | |
48 | return $this->name; | |
49 | } | |
50 | ||
51 | /** | |
52 | * @param string $name | |
53 | * | |
54 | * @return $this | |
55 | */ | |
56 | public function setName($name) { | |
57 | $this->name = $name; | |
58 | ||
59 | return $this; | |
60 | } | |
61 | ||
62 | /** | |
63 | * @return \Civi\Api4\Service\Schema\Joinable\Joinable[] | |
64 | */ | |
65 | public function getTableLinks() { | |
66 | return $this->tableLinks; | |
67 | } | |
68 | ||
69 | /** | |
70 | * @return \Civi\Api4\Service\Schema\Joinable\Joinable[] | |
71 | * Only those links that are not joining the table to itself | |
72 | */ | |
73 | public function getExternalLinks() { | |
74 | return array_filter($this->tableLinks, function (Joinable $joinable) { | |
75 | return $joinable->getTargetTable() !== $this->getName(); | |
76 | }); | |
77 | } | |
78 | ||
79 | /** | |
80 | * @param \Civi\Api4\Service\Schema\Joinable\Joinable $linkToRemove | |
81 | */ | |
82 | public function removeLink(Joinable $linkToRemove) { | |
83 | foreach ($this->tableLinks as $index => $link) { | |
84 | if ($link === $linkToRemove) { | |
85 | unset($this->tableLinks[$index]); | |
86 | } | |
87 | } | |
88 | } | |
89 | ||
90 | /** | |
91 | * @param string $baseColumn | |
92 | * @param \Civi\Api4\Service\Schema\Joinable\Joinable $joinable | |
93 | * | |
94 | * @return $this | |
95 | */ | |
96 | public function addTableLink($baseColumn, Joinable $joinable) { | |
97 | $target = $joinable->getTargetTable(); | |
98 | $targetCol = $joinable->getTargetColumn(); | |
99 | $alias = $joinable->getAlias(); | |
100 | ||
101 | if (!$this->hasLink($target, $targetCol, $alias)) { | |
102 | if (!$joinable->getBaseTable()) { | |
103 | $joinable->setBaseTable($this->getName()); | |
104 | } | |
105 | if (!$joinable->getBaseColumn()) { | |
106 | $joinable->setBaseColumn($baseColumn); | |
107 | } | |
108 | $this->tableLinks[] = $joinable; | |
109 | } | |
110 | ||
111 | return $this; | |
112 | } | |
113 | ||
114 | /** | |
115 | * @param mixed $tableLinks | |
116 | * | |
117 | * @return $this | |
118 | */ | |
119 | public function setTableLinks($tableLinks) { | |
120 | $this->tableLinks = $tableLinks; | |
121 | ||
122 | return $this; | |
123 | } | |
124 | ||
125 | /** | |
126 | * @param $target | |
127 | * @param $targetCol | |
128 | * @param $alias | |
129 | * | |
130 | * @return bool | |
131 | */ | |
132 | private function hasLink($target, $targetCol, $alias) { | |
133 | foreach ($this->tableLinks as $link) { | |
134 | if ($link->getTargetTable() === $target | |
135 | && $link->getTargetColumn() === $targetCol | |
136 | && $link->getAlias() === $alias | |
137 | ) { | |
138 | return TRUE; | |
139 | } | |
140 | } | |
141 | ||
142 | return FALSE; | |
143 | } | |
144 | ||
145 | } |