You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.2 KiB
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace App\Helper;
use App\Annotation\Dict as DictAnnotation;
use Hyperf\Di\Annotation\AnnotationCollector;
class Dict
{
/**
* @var int 是
*/
public const YES = 0;
/**
* @var int 否
*/
public const NO = 1;
public static function get(array $names, int $language)
{
return array_merge(self::getFromProperties($names), self::getFromMethods($names, $language));
}
private static function getFromProperties(array $names): array
{
$items = AnnotationCollector::getPropertiesByAnnotation(DictAnnotation::class);
$properties = [];
foreach ($items as $item) {
$properties[$item['annotation']->name] = ['class' => $item['class'], 'property' => $item['property'], 'transform' => $item['annotation']->transform];
}
$records = [];
foreach ($names as $name) {
if (isset($properties[$name])) {
$property = $properties[$name];
$items = $property['class']::${$property['property']};
if ($property['transform']) {
foreach ($items as $key => $value) {
$records[$name][] = ['key' => $key, 'value' => $value];
}
} else {
$records[$name] = $items;
}
}
}
return $records;
}
private static function getFromMethods(array $names, int $language): array
{
$items = AnnotationCollector::getMethodsByAnnotation(DictAnnotation::class);
$methods = [];
foreach ($items as $item) {
$methods[$item['annotation']->name] = ['class' => $item['class'], 'method' => $item['method']];
}
$records = [];
foreach ($names as $name) {
if (isset($methods[$name])) {
$method = $methods[$name];
$object = make($method['class']);
if ($language > 0) {
$records[$name] = $object->{$method['method']}($language);
} else {
$records[$name] = $object->{$method['method']}();
}
}
}
return $records;
}
}