/** * @param string[] $expectedNameList * @param array[] $actualItemList * @param string|null $message */ private function _assertItemsByName($expectedNameList, $actualItemList, $message = null) { $actualNameList = Functional\map($actualItemList, function (array $actualItem) { $actualLocation = new CM_Model_Location($actualItem['level'], $actualItem['id']); return $actualLocation->getName(); }); $this->assertSame($expectedNameList, $actualNameList, $message); }
/** * @param CM_Model_Location $location * @return CM_Model_Location|null */ private function _squashLocationInConstraints(CM_Model_Location $location = null) { if (null === $location) { return null; } if ($location->getLevel() < $this->_options['levelMin']) { return null; } if ($location->getLevel() > $this->_options['levelMax']) { $location = $location->get($this->_options['levelMax']); } return $location; }
public static function setUpBeforeClass() { $country1 = CM_Model_Location_Country::create('Andorra', 'AD'); $state1 = CM_Model_Location_State::create($country1, 'Canillo', null, 'AD02'); $state2 = CM_Model_Location_State::create($country1, 'Encamp', null, 'AD03'); $state3 = CM_Model_Location_State::create($country1, 'La Massana', null, 'AD04'); $city = CM_Model_Location_City::create($country1, $state1, 'Canillo', '42.567', '1.6', '146765'); $cityNext = CM_Model_Location_City::create($country1, $state1, 'El Tarter', '42.583', '1.65', '211022'); CM_Model_Location_City::create($country1, $state1, 'Meritxell', '42.55', '1.6', '230839'); CM_Model_Location_City::create($country1, $state1, 'Pas De La Casa', '42.55', '1.733', '177897'); CM_Model_Location_City::create($country1, $state1, 'Soldeu', '42.583', '1.667', '177181'); CM_Model_Location_City::create($country1, $state2, 'Encamp', '42.533', '1.583', '58282'); CM_Model_Location_City::create($country1, $state3, 'Arinsal', '42.567', '1.483', '209956'); CM_Model_Location_City::create($country1, $state3, 'El Serrat', '42.617', '1.55', '209961'); $country2 = CM_Model_Location_Country::create('Australia', 'AU'); $state4 = CM_Model_Location_State::create($country2, 'Queensland', null, 'AU04'); $state5 = CM_Model_Location_State::create($country2, 'Victoria', null, 'AU07'); $state6 = CM_Model_Location_State::create($country2, 'Tasmania', null, 'AU06'); CM_Model_Location_City::create($country2, $state4, 'Abermain', '-27.567', '152.783', '33924'); CM_Model_Location_City::create($country2, $state5, 'Acheron', '-37.25', '145.7', '195676'); CM_Model_Location_City::create($country2, $state6, 'Baden', '-42.433', '147.467', '242082'); CM_Model_Location::createAggregation(); CMTest_TH::getServiceManager()->getElasticsearch()->setEnabled(true); self::$_type = new CM_Elasticsearch_Type_Location(); self::$_searchIndexCli = new CM_Elasticsearch_Index_Cli(); self::$_searchIndexCli->create(self::$_type->getIndex()->getName()); self::$_countryId1 = $country1->getId(); self::$_countryId2 = $country2->getId(); self::$_stateId1 = $state1->getId(); self::$_stateId4 = $state4->getId(); self::$_cityId = $city->getId(); self::$_cityIdNext = $cityNext->getId(); }
public function prepare(CM_Frontend_Environment $environment, CM_Frontend_ViewResponse $viewResponse) { if (CM_Http_Request_Abstract::hasInstance()) { $ip = CM_Http_Request_Abstract::getInstance()->getIp(); if ($locationGuess = CM_Model_Location::findByIp($ip)) { $this->getField('location')->setValue($locationGuess); } } }
/** * @param CM_Model_Location $location * @return CM_Model_Currency|null */ public static function findByLocation(CM_Model_Location $location) { $country = $location->get(CM_Model_Location::LEVEL_COUNTRY); if (null === $country) { return null; } $cache = CM_Cache_Local::getInstance(); $cacheKey = CM_CacheConst::Currency_CountryId . '_countryId:' . $country->getId(); if (false === ($currencyId = $cache->get($cacheKey))) { $currencyId = CM_Db_Db::select('cm_model_currency_country', 'currencyId', ['countryId' => $country->getId()])->fetchColumn(); $currencyId = $currencyId ? (int) $currencyId : null; $cache->set($cacheKey, $currencyId); } if (null === $currencyId) { return null; } return new self($currencyId); }
public function testCountryOnly() { $country = CM_Model_Location::createCountry('My country', 'MC'); $partLabeler = function (CM_Model_Location $locationPart, CM_Model_Location $location) { return $locationPart->getName(); }; $flagLabeler = function (CM_Model_Location $locationPart, CM_Model_Location $location) { return null; }; $expected = '<span class="function-location contains-flag">My country</span>'; $this->_assertSame($expected, array('location' => $country, 'partLabeler' => $partLabeler, 'flagLabeler' => $flagLabeler)); }
public function testFindByLocationUseCityWithCountry() { $countryId = CM_Db_Db::insert('cm_model_location_country', array('abbreviation' => 'DE', 'name' => 'Germany')); $country = new CM_Model_Location(CM_Model_Location::LEVEL_COUNTRY, $countryId); $cityId = CM_Db_Db::insert('cm_model_location_city', array('stateId' => null, 'countryId' => $countryId, 'name' => 'Berlin', 'lat' => 12.345678, 'lon' => 12.345678)); $city = new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $cityId); $this->assertNull(CM_Model_Currency::findByLocation($city)); $currency = CM_Model_Currency::create('978', 'EUR'); $cache = CM_Cache_Local::getInstance(); $cacheKey = CM_CacheConst::Currency_CountryId . '_countryId:' . $country->getId(); $currency->setCountryMapping($country); $cache->delete($cacheKey); $this->assertEquals($currency, CM_Model_Currency::findByLocation($city)); }
public function testGetters() { $site = CM_Site_Abstract::factory(); $user = CM_Model_User::createStatic(); $language = CM_Model_Language::create('English', 'en', true); $timezone = new DateTimeZone('Europe/London'); $debug = true; $location = CM_Model_Location::createCountry('United Kingdom', 'UK'); $environment = new CM_Frontend_Environment($site, $user, $language, $timezone, $debug, $location); $this->assertSame($site, $environment->getSite()); $this->assertSame($user, $environment->getViewer(true)); $this->assertSame($language, $environment->getLanguage()); $this->assertSame($language->getAbbreviation(), $environment->getLocale()); $this->assertSame($timezone, $environment->getTimeZone()); $this->assertSame($debug, $environment->isDebug()); $this->assertSame($location, $environment->getLocation()); }
/** * @param CM_Model_Location $location */ public function filterLocation(CM_Model_Location $location) { $this->filterTerm('ids.' . $location->getLevel(), $location->getId()); }
private function _recreateLocationIndex() { CM_Model_Location::createAggregation(); $searchIndexCli = new CM_Elasticsearch_Index_Cli(); $searchIndexCli->create((new CM_Elasticsearch_Type_Location($this->_elasticsearchClient))->getIndexName()); }
/** * @return CM_Model_Location|null */ public function getLocation() { $ipAddress = $this->getIp(); if (null === $ipAddress) { return null; } return CM_Model_Location::findByIp($ipAddress); }
/** * @param CM_Model_Location $city * @param string $name * @param float $latitude * @param float $longitude * @throws CM_Exception_Invalid * @return CM_Model_Location */ public static function createZip(CM_Model_Location $city, $name, $latitude, $longitude) { if (CM_Model_Location::LEVEL_CITY !== $city->getLevel()) { throw new CM_Exception_Invalid('The parent location should be a city'); } $zip = CM_Model_Location_Zip::create($city->_getLocation(), $name, $latitude, $longitude); return self::fromLocation($zip); }
protected function _updateSearchIndex() { CM_Model_Location::createAggregation(); while (CM_Model_Location::getCreateAggregationInProgress($this->getServiceManager()->getDatabases()->getReadMaintenance())) { sleep(1); } $client = $this->getServiceManager()->getElasticsearch()->getClient(); $type = new CM_Elasticsearch_Type_Location($client); $searchIndexCli = new CM_Elasticsearch_Index_Cli(null, $this->_streamOutput, $this->_streamError); $searchIndexCli->create($type->getIndexName()); }
/** * @param string $field * @param CM_Model_Location $location */ public function sortGeoDistance($field, CM_Model_Location $location) { if (!$location->getCoordinates()) { return; } $this->_sort(array('_geo_distance' => array($field => $location->getCoordinates()))); }
protected function _updateSearchIndex() { CM_Model_Location::createAggregation(); $type = new CM_Elasticsearch_Type_Location(); $searchIndexCli = new CM_Elasticsearch_Index_Cli(null, $this->_streamOutput, $this->_streamError); $searchIndexCli->create($type->getIndex()->getName()); }
/** * @param int|null $level * @return CM_Model_Location */ public static function createLocation($level = null) { $country = CM_Db_Db::insert('cm_model_location_country', array('abbreviation' => 'FOO', 'name' => 'countryFoo')); $state = CM_Db_Db::insert('cm_model_location_state', array('countryId' => $country, 'name' => 'stateFoo')); $city = CM_Db_Db::insert('cm_model_location_city', array('stateId' => $state, 'countryId' => $country, 'name' => 'cityFoo', 'lat' => 10, 'lon' => 15)); $zip = CM_Db_Db::insert('cm_model_location_zip', array('cityId' => $city, 'name' => '1000', 'lat' => 10, 'lon' => 15)); CM_Model_Location::createAggregation(); switch ($level) { case CM_Model_Location::LEVEL_COUNTRY: return new CM_Model_Location(CM_Model_Location::LEVEL_COUNTRY, $country); case CM_Model_Location::LEVEL_CITY: return new CM_Model_Location(CM_Model_Location::LEVEL_CITY, $city); case CM_Model_Location::LEVEL_STATE: return new CM_Model_Location(CM_Model_Location::LEVEL_STATE, $state); default: return new CM_Model_Location(CM_Model_Location::LEVEL_ZIP, $zip); } }
public function testArrayConvertible() { $location = CMTest_TH::createLocation(); $this->assertEquals($location, CM_Model_Location::fromArray($location->toArray())); }
<?php if (!CM_Db_Db::existsColumn('cm_locationState', 'abbreviation')) { CM_Db_Db::exec('ALTER TABLE `cm_locationState` ADD `abbreviation` char(2) AFTER `name`'); } CM_Model_Location::createUSStatesAbbreviation();
/** * @expectedException CM_Db_Exception */ public function testFindByAttributesException() { CM_Model_Location::findByAttributes(CM_Model_Location::LEVEL_COUNTRY, ['notExistingField' => 'CH']); }
<?php CM_Model_Location::createAggregation(); $searchCli = new CM_Elasticsearch_Index_Cli(null, new CM_OutputStream_Stream_Output()); $searchCli->create('location');