]> git.mxchange.org Git - city.git/blob - application/city/classes/database/frontend/region/class_RegionInformationDatabaseWrapper.php
Next wave:
[city.git] / application / city / classes / database / frontend / region / class_RegionInformationDatabaseWrapper.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Database\Frontend\RegionInformation;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Registry\Registry;
8
9 /**
10  * A database wrapper for region informations
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2015, 2016 City Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.shipsimu.org
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 class RegionInformationDatabaseWrapper extends BaseDatabaseWrapper implements RegionInformationWrapper, Registerable {
32         // Constants for database table names
33         const DB_TABLE_REGION_INFORMATION = 'region_data';
34
35         // Constants for database column names
36         const DB_COLUMN_REGION_ID          = 'region_id';
37         const DB_COLUMN_REGION_NAME        = 'region_name';
38         const DB_COLUMN_REGION_USER_ID     = 'region_user_id';
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48         }
49
50         /**
51          * Creates an instance of this database wrapper by a provided user class
52          *
53          * @return      $wrapperInstance        An instance of the created wrapper class
54          */
55         public static final function createRegionInformationDatabaseWrapper () {
56                 // Get a new instance
57                 $wrapperInstance = new RegionInformationDatabaseWrapper();
58
59                 // Set (primary!) table name
60                 $wrapperInstance->setTableName(self::DB_TABLE_REGION_INFORMATION);
61
62                 // Return the instance
63                 return $wrapperInstance;
64         }
65
66         /**
67          * Removes non-public data from given array.
68          *
69          * @param       $data   An array with possible non-public data that needs to be removed.
70          * @return      $data   A cleaned up array with only public data.
71          */
72         public function removeNonPublicDataFromArray(array $data) {
73                 // Currently call only inner method
74                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGION-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
75                 $data = parent::removeNonPublicDataFromArray($data);
76
77                 // Return cleaned data
78                 return $data;
79         }
80
81         /**
82          * Checks whether the user has already founded a region
83          *
84          * @return      $hasFounded             Whether the user has already founded a region
85          */
86         public function ifUserHasCreatedRegion () {
87                 // Get user instance
88                 $userInstance = Registry::getRegistry()->getInstance('user');
89
90                 // Now get a search criteria instance
91                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
92
93                 // Search for user's cities
94                 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
95
96                 // Get a result back
97                 $resultInstance = $this->doSelectByCriteria($searchInstance);
98
99                 // Get region manager instance
100                 $managerInstance = ManagerFactory::createManagerByType('region');
101
102                 // Make sure the instance is valid
103                 assert($managerInstance instanceof ManageableRegion);
104
105                 // Set result instance
106                 $managerInstance->setResultInstance($resultInstance);
107
108                 // Has it been founded?
109                 $hasFounded = $resultInstance->valid();
110
111                 // Return result
112                 return $hasFounded;
113         }
114
115         /**
116          * Checks whether the given region name is taken
117          *
118          * @param       $regionName             Name of region
119          * @return      $isTaken                Whether the given region name is taken
120          */
121         public function ifRegionExists ($regionName) {
122                 // Now get a search criteria instance
123                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
124
125                 // Search for the region number one which is hard-coded the default
126                 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_NAME, $regionName);
127                 $searchInstance->setLimit(1);
128
129                 // Get a result back
130                 $resultInstance = $this->doSelectByCriteria($searchInstance);
131
132                 // Check it
133                 $isTaken = $resultInstance->next();
134                 //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken));
135
136                 // Get manger instance
137                 $managerInstance = ManagerFactory::createManagerByType('region');
138
139                 // Make sure the instance is valid
140                 assert($managerInstance instanceof ManageableRegion);
141
142                 // Set result instance
143                 $managerInstance->setResultInstance($resultInstance);
144
145                 // Return result
146                 return $isTaken;
147         }
148
149         /**
150          * Creates a region by given name
151          *
152          * @param       $regionName             Name of region
153          * @return      void
154          */
155         public function createRegionByName ($regionName) {
156                 // Pre-check name
157                 assert(!$this->ifRegionExists($regionName));
158
159                 // Get user instance
160                 $userInstance = Registry::getRegistry()->getInstance('user');
161
162                 // Get a dataset instance
163                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION));
164
165                 // Set the primary key
166                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
167
168                 // Add region name and assign user id
169                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID     , ($this->countTotalRows() + 1));
170                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME   , $regionName);
171                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
172
173                 // "Insert" this dataset instance completely into the database
174                 $this->queryInsertDataSet($dataSetInstance);
175
176                 // Post-check name
177                 assert($this->ifRegionExists($regionName));
178         }
179 }