This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ggrachdev/dev
0.0.1 beta
- Loading branch information
Showing
21 changed files
with
1,007 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/ggrachdev.multiregionality/tests/run.sh |
45 changes: 45 additions & 0 deletions
45
ggrachdev.multiregionality/classes/general/Multiregionality/Cache/RuntimeCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality\Cache; | ||
|
||
/** | ||
* Простой регистр | ||
*/ | ||
class RuntimeCache { | ||
|
||
private static $cacheRegister; | ||
|
||
public static function get($key) { | ||
if (self::has($key)) | ||
{ | ||
return self::getRegister()[$key]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function has($key) { | ||
return \array_key_exists($key, self::getRegister()); | ||
} | ||
|
||
public static function set($key, $value) { | ||
self::$cacheRegister[$key] = $value; | ||
} | ||
|
||
public static function getRegister() { | ||
return self::$cacheRegister; | ||
} | ||
|
||
|
||
public static function remove($key) { | ||
if(self::has($key)) | ||
{ | ||
unset(self::$cacheRegister[$key]); | ||
} | ||
} | ||
|
||
public static function removeAll() | ||
{ | ||
self::$cacheRegister = []; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ev.multiregionality/classes/general/Multiregionality/Configurator/RegionsConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality\Configurator; | ||
|
||
use GGrach\Multiregionality\Contract\IConfigurator; | ||
|
||
class RegionsConfigurator implements IConfigurator { | ||
|
||
public function __toString(): string | ||
{ | ||
return 'default_region_configurator'; | ||
} | ||
|
||
public function getCodePropertyUrlRegion(): string { | ||
return 'URL'; | ||
} | ||
|
||
public function getNameCookieNowRegion(): string { | ||
return 'GGRACH_MULTIREGIONALITY_NOW_REGION'; | ||
} | ||
|
||
public function getCodeIblockRegionsApi(): string { | ||
return 'regionsMultiregionalityApiCode'; | ||
} | ||
|
||
public function getCodeTypeIblockRegionsCode(): string { | ||
return 'multiregionalityIblockType'; | ||
} | ||
|
||
public function getCodeIblockRegionsCode(): string { | ||
return 'regionsMultiregionalityCode'; | ||
} | ||
|
||
public function getCodePropertyFormName1(): string { | ||
return 'NAME_FORM_1'; | ||
} | ||
|
||
public function getCodePropertyFormName2(): string { | ||
return 'NAME_FORM_2'; | ||
} | ||
|
||
public function getCodePropertyFormName3(): string { | ||
return 'NAME_FORM_3'; | ||
} | ||
|
||
public function getCodePropertyFormName4(): string { | ||
return 'NAME_FORM_4'; | ||
} | ||
|
||
public function getCodePropertyFormName5(): string { | ||
return 'NAME_FORM_5'; | ||
} | ||
|
||
public function getCodePropertyFormName6(): string { | ||
return 'NAME_FORM_6'; | ||
} | ||
|
||
public function getCodePropertyIsDefaultRegion(): string { | ||
return 'IS_DEFAULT_REGION'; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
ggrachdev.multiregionality/classes/general/Multiregionality/Contract/IConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality\Contract; | ||
|
||
interface IConfigurator { | ||
public function getCodeIblockRegionsApi(): string; | ||
|
||
public function __toString(): string; | ||
|
||
public function getCodeIblockRegionsCode(): string; | ||
|
||
public function getCodePropertyIsDefaultRegion(): string; | ||
|
||
public function getCodeTypeIblockRegionsCode(): string; | ||
|
||
public function getNameCookieNowRegion(): string; | ||
|
||
public function getCodePropertyUrlRegion(): string; | ||
|
||
public function getCodePropertyFormName1(): string; | ||
|
||
public function getCodePropertyFormName2(): string; | ||
|
||
public function getCodePropertyFormName3(): string; | ||
|
||
public function getCodePropertyFormName4(): string; | ||
|
||
public function getCodePropertyFormName5(): string; | ||
|
||
public function getCodePropertyFormName6(): string; | ||
} |
10 changes: 10 additions & 0 deletions
10
ggrachdev.multiregionality/classes/general/Multiregionality/Contract/IRegionDeterminator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality\Contract; | ||
|
||
use GGrach\Multiregionality\Contract\IRegionsRepository; | ||
use GGrach\Multiregionality\Entity\Region; | ||
|
||
interface IRegionDeterminator { | ||
public function determinate(string $url, IRegionsRepository $repository): Region; | ||
} |
23 changes: 23 additions & 0 deletions
23
ggrachdev.multiregionality/classes/general/Multiregionality/Contract/IRegionsRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality\Contract; | ||
|
||
use GGrach\Multiregionality\Contract\IConfigurator; | ||
|
||
interface IRegionsRepository { | ||
|
||
public function __construct(IConfigurator $configurator); | ||
|
||
public function create(): bool; | ||
|
||
/** | ||
* @return array<GGrach\Multiregionality\Entity\Region> | ||
*/ | ||
public function getList(): array; | ||
|
||
/** | ||
* @param array $arFilter | ||
* @return array<GGrach\Multiregionality\Entity\Region> | ||
*/ | ||
public function getFilteredList(array $arFilter): array; | ||
} |
45 changes: 45 additions & 0 deletions
45
...dev.multiregionality/classes/general/Multiregionality/Determinator/RegionDeterminator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality\Determinator; | ||
|
||
use GGrach\Multiregionality\Contract\IRegionDeterminator; | ||
use GGrach\Multiregionality\Contract\IRegionsRepository; | ||
use GGrach\Multiregionality\Entity\Region; | ||
use GGrach\Multiregionality\Utils\UrlParser; | ||
|
||
class RegionDeterminator implements IRegionDeterminator { | ||
|
||
public function determinate(string $url, IRegionsRepository $repository): Region { | ||
$emptyRegion = new Region(); | ||
|
||
$urlData = UrlParser::parse($url); | ||
|
||
if (!empty($urlData['host'])) { | ||
$arRegions = $repository->getList(); | ||
if (!empty($arRegions)) { | ||
foreach ($arRegions as $region) { | ||
|
||
$urlItemData = UrlParser::parse($region->getUrl()); | ||
|
||
if ( | ||
!empty($urlItemData['host']) && | ||
strcasecmp(trim($urlItemData['host']), trim($urlData['host'])) === 0 | ||
) { | ||
return $region; | ||
} | ||
} | ||
|
||
// Если регион не установлен, то ставим регион по умолчанию как определенный | ||
foreach ($arRegions as $region) { | ||
if($region->isDefaultRegion()) | ||
{ | ||
return $region; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $emptyRegion; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
ggrachdev.multiregionality/classes/general/Multiregionality/Entity/Region.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality\Entity; | ||
|
||
class Region { | ||
|
||
protected $id; | ||
protected $name; | ||
protected $isDefaultRegion; | ||
protected $nameForms = []; | ||
protected $url; | ||
protected $data = []; | ||
|
||
public function getId() { | ||
return $this->id; | ||
} | ||
|
||
public function getName() { | ||
return $this->name; | ||
} | ||
|
||
public function getNameForms(): array { | ||
return $this->nameForms; | ||
} | ||
|
||
public function getUrl() { | ||
return $this->url; | ||
} | ||
|
||
public function getData(): array { | ||
return $this->data; | ||
} | ||
|
||
public function isDefaultRegion(): bool { | ||
return $this->isDefaultRegion === true; | ||
} | ||
|
||
|
||
public function setId($id): void { | ||
$this->id = $id; | ||
} | ||
|
||
public function setName($name): void { | ||
$this->name = $name; | ||
} | ||
|
||
public function setIsDefaultRegion(bool $isDefaultRegion): void { | ||
$this->isDefaultRegion = $isDefaultRegion; | ||
} | ||
|
||
public function setNameForms(array $nameForms): void { | ||
$this->nameForms = $nameForms; | ||
} | ||
|
||
public function setUrl($url): void { | ||
$this->url = $url; | ||
} | ||
|
||
public function setData($data): void { | ||
$this->data = $data; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
ggrachdev.multiregionality/classes/general/Multiregionality/Facade/Regions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality\Facade; | ||
|
||
use GGrach\Multiregionality\Contract\IConfigurator; | ||
use GGrach\Multiregionality\Contract\IRegionsRepository; | ||
use GGrach\Multiregionality\Contract\IRegionDeterminator; | ||
use GGrach\Multiregionality\Entity\Region; | ||
|
||
/** | ||
* Description of Regions | ||
* | ||
* @author ggrachdev | ||
*/ | ||
class Regions { | ||
|
||
private $repository; | ||
private $configurator; | ||
private $determinator; | ||
|
||
public function __construct(string $url, IConfigurator $configurator, IRegionsRepository $repository, IRegionDeterminator $determinator) { | ||
$this->repository = $repository; | ||
$this->configurator = $configurator; | ||
$this->determinator = $determinator; | ||
} | ||
|
||
public function getNowRegionData(): Region { | ||
$nowRegion = new Region(); | ||
|
||
return $nowRegion; | ||
} | ||
|
||
public function getRegionsData(): array { | ||
return $this->repository->getList(); | ||
} | ||
|
||
public function getConfigurator(): IConfigurator { | ||
return $this->configurator; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
ggrachdev.multiregionality/classes/general/Multiregionality/RegionsFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace GGrach\Multiregionality; | ||
|
||
use GGrach\Multiregionality\Facade\Regions; | ||
use GGrach\Multiregionality\Contract\IConfigurator; | ||
use GGrach\Multiregionality\Configurator; | ||
|
||
class RegionsFactory { | ||
|
||
private static $instances = []; | ||
|
||
/** | ||
* Одиночки не должны быть восстанавливаемыми из строк. | ||
*/ | ||
public function __wakeup() { | ||
throw new \Exception("Cannot unserialize a singleton."); | ||
} | ||
|
||
public static function getInstance(string $url = null, IConfigurator $configurator = null): Regions { | ||
if ($url === null) { | ||
$url = $_SERVER['DOCUMENT_URI']; | ||
} | ||
|
||
if ($configurator === null) { | ||
$url = new Configurator(); | ||
} | ||
|
||
$key = $url . $configurator; | ||
if (!isset(self::$instances[$key])) { | ||
self::$instances[$key] = new Regions(); | ||
} | ||
|
||
return self::$instances[$cls]; | ||
} | ||
|
||
} |
Oops, something went wrong.