Created
December 15, 2016 09:49
-
-
Save novomirskoy/f5139545fb0dcd24ff5eaf7aa19e4047 to your computer and use it in GitHub Desktop.
Реализация словарей
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
<?php | |
declare(strict_types=1); | |
namespace common\values; | |
interface ObjectValue | |
{ | |
/** | |
* ObjectValue constructor. | |
* | |
* @param string $value | |
* @param string $label | |
*/ | |
public function __construct(string $value, string $label); | |
/** | |
* @return string | |
*/ | |
public function getLabel(); | |
/** | |
* @return string | |
*/ | |
public function __toString(); | |
} |
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
<?php | |
declare(strict_types=1); | |
namespace common\repositories; | |
use common\values\ObjectValue; | |
interface DictionaryRepository | |
{ | |
/** | |
* @return string[] | |
*/ | |
public function getValues(); | |
/** | |
* @return DictionaryObject[] | |
*/ | |
public function findAll(); | |
/** | |
* @return ObjectValue | |
*/ | |
public function className(); | |
} |
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
<?php | |
declare(strict_types=1); | |
namespace common\values; | |
class DictionaryObject implements ObjectValue | |
{ | |
/** | |
* @var string | |
*/ | |
private $value; | |
/** | |
* @var string | |
*/ | |
private $label; | |
/** | |
* {@inheritDoc} | |
*/ | |
public function __construct(string $value, string $label) | |
{ | |
$this->value = $value; | |
$this->label = $label; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function __toString() | |
{ | |
return $this->value; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getLabel() | |
{ | |
return $this->label; | |
} | |
} |
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
<?php | |
declare(strict_types=1); | |
namespace common\repositories\dictionary; | |
use common\exceptions\ObjectValueNotFound; | |
use common\helpers\DictionaryTranslateHelper; | |
use common\repositories\DictionaryRepository as DictionaryRepositoryInterface; | |
use common\values\DictionaryObject; | |
/** | |
* {@inheritDoc} | |
*/ | |
abstract class DictionaryRepository implements DictionaryRepositoryInterface | |
{ | |
/** | |
* @var DictionaryObject[] | |
*/ | |
private $storage = []; | |
/** | |
* @var DictionaryTranslateHelper | |
*/ | |
private $translateHelper; | |
/** | |
* DictionaryRepository constructor. | |
* @param DictionaryTranslateHelper $helper | |
*/ | |
public function __construct(DictionaryTranslateHelper $helper) | |
{ | |
$this->translateHelper = $helper; | |
$this->fillStorage(); | |
} | |
/** | |
* {@inheritDoc} | |
* @throws \common\exceptions\ObjectValueNotFound | |
*/ | |
public function find($id) | |
{ | |
foreach ($this->storage as $value) { | |
if ((string) $value === $id) { | |
return $value; | |
} | |
} | |
throw new ObjectValueNotFound(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function findAll() | |
{ | |
return $this->storage; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function findAllAsList() | |
{ | |
$result = []; | |
foreach ($this->storage as $item) { | |
$result[(string) $item] = $item->getLabel(); | |
} | |
return $result; | |
} | |
private function fillStorage() | |
{ | |
$className = $this->className(); | |
foreach ($this->getValues() as $status) { | |
$translateText = $className . $status; | |
$label = $this->translateHelper->translate($translateText); | |
$this->storage[] = new $className($status, $label); | |
} | |
} | |
} |
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
<?php | |
declare(strict_types=1); | |
namespace common\values; | |
class Status extends DictionaryObject | |
{ | |
const ENABLED = 'enabled'; | |
const DISABLED = 'disabled'; | |
const DELETED = 'deleted'; | |
} |
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
<?php | |
declare(strict_types=1); | |
namespace common\repositories\dictionary; | |
use common\repositories\StatusRepository as StatusRepositoryInterface; | |
use common\values\Status; | |
/** | |
* {@inheritDoc} | |
*/ | |
class StatusRepository extends DictionaryRepository implements StatusRepositoryInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getValues() | |
{ | |
return [ | |
Status::ENABLED, | |
Status::DISABLED, | |
Status::DELETED, | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function className() | |
{ | |
return Status::class; | |
} | |
} |
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
<?php | |
declare(strict_types=1); | |
use common\values\Status; | |
return [ | |
Status::class . Status::DELETED => 'Удален', | |
Status::class . Status::DISABLED => 'Выключен', | |
Status::class . Status::ENABLED => 'Активен', | |
]; |
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
<?php | |
// Настройка | |
$container->setSingleton(StatusRepositoryInterface::class, StatusRepository::class); | |
// Использование | |
$statusRepository = $container->get(StatusRepositoryInterface::class); | |
// Получение конкретного статуса | |
$statusDeletedObject = $statusRepository->get(Status::DELETED); | |
// Вывести значение статуса | |
echo $statusDeletedObject; // deleted | |
// Вывести метку статуса | |
echo $statusDeletedObject->getLabel(); // Удален | |
// Получить все статусы | |
$statuses = $statusRepository->findAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment