Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions BACKLOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
* Add functionality for find places what included in specified radius
* Add functionality for find border places
* Add measuring method for calculate civil twilight for specific coordinate
* Add filtering Places by type
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 2.12

+ Add functionality for find all child places of specific Admin level collection
+ Implement type property for Place entity

## 2.1

+ Add to coordinates altitude
+ Add measuring distance between two coordinates
+ Fix potential issue in findPlaceByCoordinates method
+ Implement functionality for find neighbor places

## 2.0

Expand Down
5 changes: 5 additions & 0 deletions src/Common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 2.12

+ Add functionality for find all child places of specific Admin level collection
+ Implement type property for Place entity

## 2.1

+ Add to coordinates altitude
Expand Down
43 changes: 28 additions & 15 deletions src/Common/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* @author Borys Yermokhin <borys_ermokhin@yahoo.com>
*/
abstract class AbstractDatabase
abstract class AbstractDatabase implements DataBaseInterface
{
/**
* @var bool[]
Expand Down Expand Up @@ -106,8 +106,6 @@ public function compileKey(
* Levels compiler for forming identifier for Address entity in @see compileKey
*
* @return string[]
*
* @throws \Psr\Cache\InvalidArgumentException
*/
protected function compileLevelsForKey(Address $address): array
{
Expand Down Expand Up @@ -190,22 +188,35 @@ public function getAdminLevels(): array
* @param int $maxResults
* @param string $locale
* @param int $filterAdminLevel
* @param string $filterType
*
* @return string[]
*/
protected function makeSearch(string $phrase, int $page, int $maxResults, string $locale, int $filterAdminLevel = -1): array
{
protected function makeSearch(
string $phrase,
int $page,
int $maxResults,
string $locale,
int $filterAdminLevel = -1,
string $filterType = ''
): array {
$result = [];

foreach ($this->actualKeys[$locale] as $adminLevel => $actualKeys) {
if ($filterAdminLevel > -1 && $filterAdminLevel !== $adminLevel) {
foreach ($this->actualKeys[$locale] as $type => $adminLevels) {
if (strlen($type) > 0 && $type !== $filterType) {
continue;
}

foreach ($actualKeys as $actualKey => $objectHash) {
$grade = $this->evaluateHitPhrase($phrase, $actualKey);
if ($grade > 0) {
$result[$actualKey] = $grade;
foreach ($adminLevels as $adminLevel => $actualKeys) {
if ($filterAdminLevel > -1 && $filterAdminLevel !== $adminLevel) {
continue;
}

foreach ($actualKeys as $actualKey => $objectHash) {
$grade = $this->evaluateHitPhrase($phrase, $actualKey);
if ($grade > 0) {
$result[$actualKey] = $grade;
}
}
}
}
Expand Down Expand Up @@ -256,13 +267,15 @@ protected function evaluateHitPhrase(string $phrase, string $original): int
* @param string $locale
* @param string $key
*
* @return int
* @return array
*/
protected function findAdminLevelForKey(string $locale, string $key): int
protected function findAdminLevelAndTypeForKey(string $locale, string $key): array
{
foreach ($this->existAdminLevels as $existAdminLevel => $value) {
if (isset($this->actualKeys[$locale][$existAdminLevel][$key])) {
return $existAdminLevel;
foreach (array_keys($this->actualKeys[$locale]) as $existType) {
if (isset($this->actualKeys[$locale][$existType][$existAdminLevel][$key])) {
return [$existAdminLevel, $existType];
}
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/Common/Database/DataBaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ApacheBorys\Location\Model\Address;
use ApacheBorys\Location\Model\DBConfig;
use ApacheBorys\Location\Model\Place;
use ApacheBorys\Location\Model\PlaceCollection;

/**
* @author Borys Yermokhin <borys_ermokhin@yahoo.com>
Expand Down Expand Up @@ -61,9 +62,9 @@ public function delete(Place $place): bool;
* @param int $offset
* @param int $limit
*
* @return Place[]
* @return PlaceCollection
*/
public function getAllPlaces(int $offset = 0, int $limit = 50): array;
public function getAllPlaces(int $offset = 0, int $limit = 50): PlaceCollection;

/**
* All admin levels what contain database
Expand Down Expand Up @@ -113,7 +114,19 @@ public function compileKey(
bool $useAddress = true
): string;

/**
* Service method for update all existing admin levels
*
* @return bool
*/
public function updateExistAdminLevels(): bool;

/**
* Service method for normalize string for key name what should stored in database
*
* @param string $rawString
*
* @return mixed
*/
public function normalizeStringForKeyName(string $rawString);
}
20 changes: 11 additions & 9 deletions src/Common/Database/PdoDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ApacheBorys\Location\Database\PdoDatabase\HelperLocator;
use ApacheBorys\Location\Model\DBConfig;
use ApacheBorys\Location\Model\Place;
use ApacheBorys\Location\Model\PlaceCollection;
use ApacheBorys\Location\Model\Polygon;
use Psr\Log\InvalidArgumentException;

Expand All @@ -44,11 +45,12 @@ class PdoDatabase extends AbstractDatabase implements DataBaseInterface
/**
* By that keys we will store hashes (references) to fetch real object
* first key - locale
* second key - type of Place
* second key - admin level
* third key - compiled key from Address of current locale
* value - hash of object
*
* @var string[][][]
* @var string[][][][]
*/
protected $actualKeys = [];

Expand Down Expand Up @@ -114,9 +116,9 @@ public function get(string $searchKey, int $page = 0, int $maxResults = 30, stri
$result = [];

foreach ($this->makeSearch($searchKey, $page, $maxResults, $locale, $filterAdminLevel) as $key) {
$adminLevel = $this->findAdminLevelForKey($locale, $key);
list($adminLevel, $placeType) = $this->findAdminLevelAndTypeForKey($locale, $key);

$result[] = $this->getPlace($this->actualKeys[$locale][$adminLevel][$key]);
$result[] = $this->getPlace($this->actualKeys[$locale][$placeType][$adminLevel][$key]);
}

return $result;
Expand All @@ -126,9 +128,9 @@ public function delete(Place $place): bool
{
if ($this->deletePlace($place->getObjectHash())) {
foreach ($this->actualKeys as $locale => $localeKeys) {
$search = array_search($place->getObjectHash(), $localeKeys[$place->getMaxAdminLevel()]);
$search = array_search($place->getObjectHash(), $localeKeys[$place->getType()][$place->getMaxAdminLevel()]);
if (is_string($search)) {
unset($this->actualKeys[$locale][$place->getMaxAdminLevel()][$search]);
unset($this->actualKeys[$locale][$place->getType()][$place->getMaxAdminLevel()][$search]);
}
}
unset($this->objectsHashes[$place->getObjectHash()]);
Expand All @@ -139,7 +141,7 @@ public function delete(Place $place): bool
return false;
}

public function getAllPlaces(int $offset = 0, int $limit = 50): array
public function getAllPlaces(int $offset = 0, int $limit = 50): PlaceCollection
{
$result = [];

Expand All @@ -153,7 +155,7 @@ public function getAllPlaces(int $offset = 0, int $limit = 50): array
$result[] = $this->getPlace($rawPlace[Constants::OBJECT_HASH]);
}

return $result;
return new PlaceCollection($result);
}

public function updateExistAdminLevels(): bool
Expand Down Expand Up @@ -285,7 +287,7 @@ private function prepareSearchKeysForInsert(Place $place): array
foreach ($place->getAvailableAddresses() as $locale => $address) {
$stmtSearchKeyForAddress[$locale] = $this->prepareSearchKeyForInsert($place, $address, $locale);

$this->actualKeys[$locale][$place->getMaxAdminLevel()][$this->compileKey($address, true, false)] = $place->getObjectHash();
$this->actualKeys[$locale][$place->getType()][$place->getMaxAdminLevel()][$this->compileKey($address, true, false)] = $place->getObjectHash();
}

return $stmtSearchKeyForAddress;
Expand Down Expand Up @@ -607,7 +609,7 @@ private function getActualKeys(): bool

$rawActKeys = $stmt->fetchAll();
foreach ($rawActKeys as $rawActKey) {
$this->actualKeys[$rawActKey[Constants::LOCALE]][$rawActKey[Constants::LEVEL]][$rawActKey[Constants::SEARCH_TEXT]] = $rawActKey[Constants::OBJECT_HASH];
$this->actualKeys[$rawActKey[Constants::LOCALE]][$rawActKey[Constants::TYPE]][$rawActKey[Constants::LEVEL]][$rawActKey[Constants::SEARCH_TEXT]] = $rawActKey[Constants::OBJECT_HASH];
}

++$page;
Expand Down
3 changes: 3 additions & 0 deletions src/Common/Database/PdoDatabase/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ final class Constants

const LOCALE = 'locale';

const TYPE = 'type';

const PROVIDED_BY = 'provided_by';

const BOUNDS_WEST = 'bounds_west';
Expand Down Expand Up @@ -77,6 +79,7 @@ final class Constants
self::COMPRESSED_DATA => '',
self::PROVIDED_BY => Place::class.'::providedBy',
self::LOCALE => Place::class.'::locale',
self::TYPE => Place::class.'::type',
self::BOUNDS_WEST => Bounds::class.'::west',
self::BOUNDS_SOUTH => Bounds::class.'::south',
self::BOUNDS_NORTH => Bounds::class.'::north',
Expand Down
1 change: 1 addition & 0 deletions src/Common/Database/PdoDatabase/MysqlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function queryForCreateTables(): array
"'.Constants::COMPRESSED_DATA.'" BLOB,
"'.Constants::PROVIDED_BY.'" TEXT,
"'.Constants::LOCALE.'" TEXT,
"'.Constants::TYPE.'" TEXT,
"'.Constants::BOUNDS_SOUTH.'" REAL,
"'.Constants::BOUNDS_WEST.'" REAL,
"'.Constants::BOUNDS_NORTH.'" REAL,
Expand Down
1 change: 1 addition & 0 deletions src/Common/Database/PdoDatabase/PostgresqlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function queryForCreateTables(): array
"'.Constants::COMPRESSED_DATA.'" BYTEA,
"'.Constants::PROVIDED_BY.'" TEXT,
"'.Constants::LOCALE.'" TEXT,
"'.Constants::TYPE.'" TEXT,
"'.Constants::BOUNDS_SOUTH.'" REAL,
"'.Constants::BOUNDS_WEST.'" REAL,
"'.Constants::BOUNDS_NORTH.'" REAL,
Expand Down
1 change: 1 addition & 0 deletions src/Common/Database/PdoDatabase/SqliteHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function queryForCreateTables(): array
"'.Constants::COMPRESSED_DATA.'" BLOB,
"'.Constants::PROVIDED_BY.'" TEXT,
"'.Constants::LOCALE.'" TEXT,
"'.Constants::TYPE.'" TEXT,
"'.Constants::BOUNDS_SOUTH.'" REAL,
"'.Constants::BOUNDS_WEST.'" REAL,
"'.Constants::BOUNDS_NORTH.'" REAL,
Expand Down
20 changes: 11 additions & 9 deletions src/Common/Database/Psr6Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use ApacheBorys\Location\Model\DBConfig;
use ApacheBorys\Location\Model\Place;
use ApacheBorys\Location\Model\PlaceCollection;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\InvalidArgumentException;

Expand All @@ -25,11 +26,12 @@ class Psr6Database extends AbstractDatabase implements DataBaseInterface
/**
* By that keys we will store hashes (references) to fetch real object
* first key - locale
* second key - type of Place
* second key - admin level
* third key - compiled key from Address of current locale
* value - hash of object
*
* @var string[][][]
* @var string[][][][]
*/
protected $actualKeys = [];

Expand Down Expand Up @@ -123,9 +125,9 @@ public function get(string $searchKey, int $page = 0, int $maxResults = 30, stri
$result = [];

foreach ($this->makeSearch($searchKey, $page, $maxResults, $locale, $filterAdminLevel) as $key) {
$adminLevel = $this->findAdminLevelForKey($locale, $key);
list($adminLevel, $placeType) = $this->findAdminLevelAndTypeForKey($locale, $key);

$item = $this->databaseProvider->getItem($this->actualKeys[$locale][$adminLevel][$key]);
$item = $this->databaseProvider->getItem($this->actualKeys[$locale][$placeType][$adminLevel][$key]);
if ($item->isHit()) {
$this->dbConfig->isUseCompression() ?
$rawData = json_decode(gzuncompress($item->get()), true) :
Expand All @@ -143,10 +145,10 @@ public function get(string $searchKey, int $page = 0, int $maxResults = 30, stri
*
* @throws \Psr\Cache\InvalidArgumentException
*/
public function getAllPlaces(int $offset = 0, int $limit = 50): array
public function getAllPlaces(int $offset = 0, int $limit = 50): PlaceCollection
{
if ($offset > count($this->objectsHashes)) {
return [];
return new PlaceCollection();
}

if ($limit > $this->dbConfig->getMaxPlacesInOneResponse()) {
Expand Down Expand Up @@ -177,7 +179,7 @@ public function getAllPlaces(int $offset = 0, int $limit = 50): array
}
}

return $result;
return new PlaceCollection($result);
}

/**
Expand All @@ -198,8 +200,8 @@ public function delete(Place $place): bool
foreach ($this->actualKeys as $locale => $keys) {
$place->selectLocale($locale);
$keyForDelete = $this->compileKey($place->getSelectedAddress());
if (isset($keys[$place->getMaxAdminLevel()][$keyForDelete])) {
unset($this->actualKeys[$locale][$place->getMaxAdminLevel()][$keyForDelete]);
if (isset($keys[$place->getType()][$place->getMaxAdminLevel()][$keyForDelete])) {
unset($this->actualKeys[$locale][$place->getType()][$place->getMaxAdminLevel()][$keyForDelete]);
$this->updateActualKeys();
}
}
Expand Down Expand Up @@ -356,7 +358,7 @@ private function savePlace(Place $place): bool
$this->updateHashKeys();

foreach ($this->compileKeys($place) as $locale => $key) {
$this->actualKeys[$locale][$place->getMaxAdminLevel()][$key] = $place->getObjectHash();
$this->actualKeys[$locale][$place->getType()][$place->getMaxAdminLevel()][$key] = $place->getObjectHash();
}
$this->updateActualKeys();

Expand Down
Loading