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