3 namespace Org\Mxchange\City\Database\Frontend\RegionInformation;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Registry\Registry;
10 * A database wrapper for region informations
12 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
31 class RegionInformationDatabaseWrapper extends BaseDatabaseWrapper implements RegionInformationWrapper, Registerable {
32 // Constants for database table names
33 const DB_TABLE_REGION_INFORMATION = 'region_data';
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';
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
51 * Creates an instance of this database wrapper by a provided user class
53 * @return $wrapperInstance An instance of the created wrapper class
55 public static final function createRegionInformationDatabaseWrapper () {
57 $wrapperInstance = new RegionInformationDatabaseWrapper();
59 // Set (primary!) table name
60 $wrapperInstance->setTableName(self::DB_TABLE_REGION_INFORMATION);
62 // Return the instance
63 return $wrapperInstance;
67 * Removes non-public data from given array.
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.
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);
77 // Return cleaned data
82 * Checks whether the user has already founded a region
84 * @return $hasFounded Whether the user has already founded a region
86 public function ifUserHasCreatedRegion () {
88 $userInstance = Registry::getRegistry()->getInstance('user');
90 // Now get a search criteria instance
91 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
93 // Search for user's cities
94 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
97 $resultInstance = $this->doSelectByCriteria($searchInstance);
99 // Get region manager instance
100 $managerInstance = ManagerFactory::createManagerByType('region');
102 // Make sure the instance is valid
103 assert($managerInstance instanceof ManageableRegion);
105 // Set result instance
106 $managerInstance->setResultInstance($resultInstance);
108 // Has it been founded?
109 $hasFounded = $resultInstance->valid();
116 * Checks whether the given region name is taken
118 * @param $regionName Name of region
119 * @return $isTaken Whether the given region name is taken
121 public function ifRegionExists ($regionName) {
122 // Now get a search criteria instance
123 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
130 $resultInstance = $this->doSelectByCriteria($searchInstance);
133 $isTaken = $resultInstance->next();
134 //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken));
136 // Get manger instance
137 $managerInstance = ManagerFactory::createManagerByType('region');
139 // Make sure the instance is valid
140 assert($managerInstance instanceof ManageableRegion);
142 // Set result instance
143 $managerInstance->setResultInstance($resultInstance);
150 * Creates a region by given name
152 * @param $regionName Name of region
155 public function createRegionByName ($regionName) {
157 assert(!$this->ifRegionExists($regionName));
160 $userInstance = Registry::getRegistry()->getInstance('user');
162 // Get a dataset instance
163 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION));
165 // Set the primary key
166 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
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());
173 // "Insert" this dataset instance completely into the database
174 $this->queryInsertDataSet($dataSetInstance);
177 assert($this->ifRegionExists($regionName));