Skip to content
This repository has been archived by the owner on Jun 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from ggrachdev/dev
Browse files Browse the repository at this point in the history
0.0.1 beta
  • Loading branch information
ggrachdev authored May 26, 2021
2 parents ddb7dea + 15c6282 commit e7b4ef4
Show file tree
Hide file tree
Showing 21 changed files with 1,007 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/ggrachdev.multiregionality/tests/run.sh
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 = [];
}
}
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';
}

}
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;
}
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;
}
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;
}
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;
}

}
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;
}

}
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;
}
}
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];
}

}
Loading

0 comments on commit e7b4ef4

Please sign in to comment.