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