Continued:
[core.git] / framework / main / classes / criteria / dataset / class_DataSetCriteria.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Criteria\DataSet;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\BaseCriteria;
7 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
8 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
9 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13
14 /**
15  * A set of data storeable in databases
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
37         /**
38          * Table name
39          */
40         private $tableName = '';
41
42         /**
43          * Unique key
44          */
45         private $uniqueKey = '';
46
47         /**
48          * Primary key
49          */
50         private $primaryKey = '';
51
52         /**
53          * Primary keys
54          */
55         private $primaryKeys = [];
56
57         /**
58          * Search criteria instance
59          */
60         private $searchInstance = NULL;
61
62         /**
63          * Protected constructor
64          *
65          * @return      void
66          */
67         private function __construct () {
68                 // Call parent constructor
69                 parent::__construct(__CLASS__);
70         }
71
72         /**
73          * Creates an instance of this criteria
74          *
75          * @param       $tableName                      Name of the table
76          * @return      $criteriaInstance       An instance of this criteria
77          * @throws      InvalidArgumentException        If a parameter is not valid
78          */
79         public static final function createDataSetCriteria (string $tableName) {
80                 // Validate parameter
81                 if (empty($tableName)) {
82                         // Throw IAE
83                         throw new InvalidArgumentException('Parameter "tableName" is empty');
84                 }
85
86                 // Get a new instance
87                 $criteriaInstance = new DataSetCriteria();
88
89                 // Set table name
90                 $criteriaInstance->setTableName($tableName);
91
92                 // Return the instance
93                 return $criteriaInstance;
94         }
95
96         /**
97          * Setter for table name
98          *
99          * @param       $tableName      Name of the table to set
100          * @return      void
101          */
102         public final function setTableName (string $tableName) {
103                 $this->tableName = $tableName;
104         }
105
106         /**
107          * Getter for table name
108          *
109          * @return      $tableName      Name of the table to set
110          */
111         public final function getTableName () {
112                 return $this->tableName;
113         }
114
115         /**
116          * Setter for unique key
117          *
118          * @param       $uniqueKey      Column to use as unique key
119          * @return      void
120          */
121         public final function setUniqueKey (string $uniqueKey) {
122                 $this->uniqueKey = $uniqueKey;
123         }
124
125         /**
126          * Getter for unique key
127          *
128          * @return      $uniqueKey      Column to use as unique key
129          */
130         public final function getUniqueKey () {
131                 return $this->uniqueKey;
132         }
133
134         /**
135          * Setter for primary key
136          *
137          * @param       $primaryKey             Primary key to set
138          * @return      void
139          */
140         public final function setPrimaryKey (string $primaryKey) {
141                 $this->primaryKey = $primaryKey;
142         }
143
144         /**
145          * Setter for primary key array
146          *
147          * @param       $primaryKeys    Primary key array to set
148          * @return      void
149          */
150         public function setPrimaryKeyCombined (array $primaryKeys) {
151                 $this->primaryKeys = $primaryKeys;
152         }
153
154         /**
155          * Getter for primary keys
156          *
157          * @return      $primaryKeys    Primary key array
158          */
159         public final function getPrimaryKeys () {
160                 // Return it
161                 return $this->primaryKeys;
162         }
163
164         /**
165          * Setter for search instance
166          *
167          * @param       $searchInstance         Searchable criteria instance
168          * @return      void
169          */
170         public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
171                 $this->searchInstance = $searchInstance;
172         }
173
174         /**
175          * Getter for search instance
176          *
177          * @return      $searchInstance         Searchable criteria instance
178          */
179         public final function getSearchInstance () {
180                 return $this->searchInstance;
181         }
182
183         /**
184          * Getter for unique key value
185          *
186          * @return      $uniqueValue    Value of the unique key
187          */
188         public final function getUniqueValue () {
189                 // Get primary key(s) first
190                 $primaryKey  = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
191                 $primaryKeys = $this->getPrimaryKeys();
192
193                 // Debug message
194                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
195
196                 /*
197                  * If this is not set, this could mean a badly written frontend as
198                  * tables should always have a primary key.
199                  */
200                 if (count($primaryKeys) > 0) {
201                         /*
202                          * Init return value, this can be put all together without any
203                          * separator as it only aids as a "unique value" for generating the
204                          * "row file name".
205                          */
206                         $return = '';
207
208                         // Combination set, so get all
209                         foreach ($primaryKeys as $primaryKey) {
210                                 // Add it
211                                 $return .= trim($this->getCriteriaElemnent($primaryKey));
212                         }
213
214                         // Debug message
215                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
216
217                         // Return it
218                         return $return;
219                 } elseif (!empty($primaryKey)) {
220                         // Return primary key
221                         return $primaryKey;
222                 } else {
223                         // @TODO Issue a warning
224                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
225
226                         // Get unique key
227                         $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
228
229                         // Debug message
230                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATA-SET-CRITERIA: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
231
232                         // Is it empty, too?
233                         if (empty($uniqueKey)) {
234                                 // Bad news, nothing is "unique" by design for this table
235                                 ApplicationEntryPoint::exitApplication('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
236                         } else {
237                                 // Return unique key
238                                 return $uniqueKey;
239                         }
240                 }
241         }
242
243         /**
244          * Getter for primary key or unique key if not set
245          *
246          * @return      $primaryKey             Primary key or unique key if not set
247          */
248         public final function getPrimaryKey () {
249                 // Get primary key by default
250                 $primaryKey = $this->primaryKey;
251
252                 if (empty($primaryKey)) {
253                         // Get uniqueKey
254                         $primaryKey = $this->getUniqueKey();
255                 }
256
257                 // Return it
258                 return $primaryKey;
259         }
260
261 }