CustomFieldSelectConverter.php 872 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Services\History\Converter;
  3. use App\Models\CustomField;
  4. class CustomFieldSelectConverter extends NotEagerLoadingConverter implements ConverterContact
  5. {
  6. public function __construct(protected string $group, protected string $field)
  7. {
  8. }
  9. public function handle(mixed $value)
  10. {
  11. $customField = CustomField::query()->where("group", $this->group)->where("key", $this->field)->first();
  12. $relations = [];
  13. if ($customField?->options) {
  14. $alias = [
  15. 'zh_CN' => 'zh',
  16. ];
  17. $lang = $alias[config("app.locale")] ?? config("app.locale");
  18. foreach ($customField->options as $option) {
  19. $relations[$option['value']] = $option['lang'][$lang] ?? $option['lang']['en'];
  20. }
  21. }
  22. return $relations[$value] ?? $value;
  23. }
  24. }