]> git.mxchange.org Git - city.git/blob - application/city/classes/database/frontend/region/class_RegionInformationDatabaseWrapper.php
3938f9357c8cc707b9388ce7a8785fe98c6ac902
[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 application-specific stuff
6 use Org\Mxchange\City\Factory\Manager\ManagerFactory;
7
8 // Import framework stuff
9 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseWrapper;
10 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
11 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
12 use Org\Mxchange\CoreFramework\Registry\Registerable;
13
14 /**
15  * A database wrapper for region informations
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2015, 2016 City 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 RegionInformationDatabaseWrapper extends BaseDatabaseWrapper implements RegionInformationWrapper, Registerable {
37         // Constants for database table names
38         const DB_TABLE_REGION_INFORMATION = 'region_data';
39
40         // Constants for database column names
41         const DB_COLUMN_REGION_ID          = 'region_id';
42         const DB_COLUMN_REGION_NAME        = 'region_name';
43         const DB_COLUMN_REGION_USER_ID     = 'region_user_id';
44
45         /**
46          * Protected constructor
47          *
48          * @return      void
49          */
50         protected function __construct () {
51                 // Call parent constructor
52                 parent::__construct(__CLASS__);
53         }
54
55         /**
56          * Creates an instance of this database wrapper by a provided user class
57          *
58          * @return      $wrapperInstance        An instance of the created wrapper class
59          */
60         public static final function createRegionInformationDatabaseWrapper () {
61                 // Get a new instance
62                 $wrapperInstance = new RegionInformationDatabaseWrapper();
63
64                 // Set (primary!) table name
65                 $wrapperInstance->setTableName(self::DB_TABLE_REGION_INFORMATION);
66
67                 // Return the instance
68                 return $wrapperInstance;
69         }
70
71         /**
72          * Removes non-public data from given array.
73          *
74          * @param       $data   An array with possible non-public data that needs to be removed.
75          * @return      $data   A cleaned up array with only public data.
76          */
77         public function removeNonPublicDataFromArray(array $data) {
78                 // Currently call only inner method
79                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('REGION-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
80                 $data = parent::removeNonPublicDataFromArray($data);
81
82                 // Return cleaned data
83                 return $data;
84         }
85
86         /**
87          * Checks whether the user has already founded a region
88          *
89          * @return      $hasFounded             Whether the user has already founded a region
90          */
91         public function ifUserHasCreatedRegion () {
92                 // Get user instance
93                 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
94
95                 // Now get a search criteria instance
96                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
97
98                 // Search for user's cities
99                 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
100
101                 // Get a result back
102                 $resultInstance = $this->doSelectByCriteria($searchInstance);
103
104                 // Get region manager instance
105                 $managerInstance = ManagerFactory::createManagerByType('region');
106
107                 // Make sure the instance is valid
108                 assert($managerInstance instanceof ManageableRegion);
109
110                 // Set result instance
111                 $managerInstance->setResultInstance($resultInstance);
112
113                 // Has it been founded?
114                 $hasFounded = $resultInstance->valid();
115
116                 // Return result
117                 return $hasFounded;
118         }
119
120         /**
121          * Checks whether the given region name is taken
122          *
123          * @param       $regionName             Name of region
124          * @return      $isTaken                Whether the given region name is taken
125          */
126         public function ifRegionExists ($regionName) {
127                 // Now get a search criteria instance
128                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
129
130                 // Search for the region number one which is hard-coded the default
131                 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_NAME, $regionName);
132                 $searchInstance->setLimit(1);
133
134                 // Get a result back
135                 $resultInstance = $this->doSelectByCriteria($searchInstance);
136
137                 // Check it
138                 $isTaken = $resultInstance->next();
139                 //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken));
140
141                 // Get manger instance
142                 $managerInstance = ManagerFactory::createManagerByType('region');
143
144                 // Make sure the instance is valid
145                 assert($managerInstance instanceof ManageableRegion);
146
147                 // Set result instance
148                 $managerInstance->setResultInstance($resultInstance);
149
150                 // Return result
151                 return $isTaken;
152         }
153
154         /**
155          * Creates a region by given name
156          *
157          * @param       $regionName             Name of region
158          * @return      void
159          */
160         public function createRegionByName ($regionName) {
161                 // Pre-check name
162                 assert(!$this->ifRegionExists($regionName));
163
164                 // Get user instance
165                 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
166
167                 // Get a dataset instance
168                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION));
169
170                 // Set the primary key
171                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
172
173                 // Add region name and assign user id
174                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_ID     , ($this->countTotalRows() + 1));
175                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_NAME   , $regionName);
176                 $dataSetInstance->addCriteria(self::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
177
178                 // "Insert" this dataset instance completely into the database
179                 $this->queryInsertDataSet($dataSetInstance);
180
181                 // Post-check name
182                 assert($this->ifRegionExists($regionName));
183         }
184 }