3 namespace Org\Mxchange\City\Database\Frontend\RegionInformation;
5 // Import application-specific stuff
6 use Org\Mxchange\City\Factory\Manager\ManagerFactory;
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\Registerable;
12 use Org\Mxchange\CoreFramework\Registry\Registry;
15 * A database wrapper for region informations
17 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
36 class RegionInformationDatabaseWrapper extends BaseDatabaseWrapper implements RegionInformationWrapper, Registerable {
37 // Constants for database table names
38 const DB_TABLE_REGION_INFORMATION = 'region_data';
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';
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of this database wrapper by a provided user class
58 * @return $wrapperInstance An instance of the created wrapper class
60 public static final function createRegionInformationDatabaseWrapper () {
62 $wrapperInstance = new RegionInformationDatabaseWrapper();
64 // Set (primary!) table name
65 $wrapperInstance->setTableName(self::DB_TABLE_REGION_INFORMATION);
67 // Return the instance
68 return $wrapperInstance;
72 * Removes non-public data from given array.
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.
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);
82 // Return cleaned data
87 * Checks whether the user has already founded a region
89 * @return $hasFounded Whether the user has already founded a region
91 public function ifUserHasCreatedRegion () {
93 $userInstance = Registry::getRegistry()->getInstance('user');
95 // Now get a search criteria instance
96 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
98 // Search for user's cities
99 $searchInstance->addCriteria(RegionInformationDatabaseWrapper::DB_COLUMN_REGION_USER_ID, $userInstance->getUserId());
102 $resultInstance = $this->doSelectByCriteria($searchInstance);
104 // Get region manager instance
105 $managerInstance = ManagerFactory::createManagerByType('region');
107 // Make sure the instance is valid
108 assert($managerInstance instanceof ManageableRegion);
110 // Set result instance
111 $managerInstance->setResultInstance($resultInstance);
113 // Has it been founded?
114 $hasFounded = $resultInstance->valid();
121 * Checks whether the given region name is taken
123 * @param $regionName Name of region
124 * @return $isTaken Whether the given region name is taken
126 public function ifRegionExists ($regionName) {
127 // Now get a search criteria instance
128 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
135 $resultInstance = $this->doSelectByCriteria($searchInstance);
138 $isTaken = $resultInstance->next();
139 //* NOISY-DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] isTaken[' . gettype($isTaken) . ']=' . intval($isTaken));
141 // Get manger instance
142 $managerInstance = ManagerFactory::createManagerByType('region');
144 // Make sure the instance is valid
145 assert($managerInstance instanceof ManageableRegion);
147 // Set result instance
148 $managerInstance->setResultInstance($resultInstance);
155 * Creates a region by given name
157 * @param $regionName Name of region
160 public function createRegionByName ($regionName) {
162 assert(!$this->ifRegionExists($regionName));
165 $userInstance = Registry::getRegistry()->getInstance('user');
167 // Get a dataset instance
168 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_REGION_INFORMATION));
170 // Set the primary key
171 $dataSetInstance->setUniqueKey(self::DB_COLUMN_REGION_ID);
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());
178 // "Insert" this dataset instance completely into the database
179 $this->queryInsertDataSet($dataSetInstance);
182 assert($this->ifRegionExists($regionName));