|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +/** |
| 4 | + * ConsistenceEnumField.php |
| 5 | + * |
| 6 | + * @license More in LICENSE.md |
| 7 | + * @copyright https://www.fastybird.com |
| 8 | + * @author Adam Kadlec <adam.kadlec@fastybird.com> |
| 9 | + * @package FastyBird:JsonApi! |
| 10 | + * @subpackage Hydrators |
| 11 | + * @since 0.1.0 |
| 12 | + * |
| 13 | + * @date 26.05.20 |
| 14 | + */ |
| 15 | + |
| 16 | +namespace FastyBird\JsonApi\Hydrators\Fields; |
| 17 | + |
| 18 | +use Consistence; |
| 19 | +use IPub\JsonAPIDocument; |
| 20 | +use function call_user_func; |
| 21 | +use function is_callable; |
| 22 | + |
| 23 | +/** |
| 24 | + * Entity consistence enum field |
| 25 | + * |
| 26 | + * @package FastyBird:JsonApi! |
| 27 | + * @subpackage Hydrators |
| 28 | + * |
| 29 | + * @author Adam Kadlec <adam.kadlec@fastybird.com> |
| 30 | + */ |
| 31 | +final class ConsistenceEnumField extends Field |
| 32 | +{ |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private readonly string $typeClass, |
| 36 | + private readonly bool $isNullable, |
| 37 | + string $mappedName, |
| 38 | + string $fieldName, |
| 39 | + bool $isRequired, |
| 40 | + bool $isWritable, |
| 41 | + ) |
| 42 | + { |
| 43 | + parent::__construct($mappedName, $fieldName, $isRequired, $isWritable); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @param JsonAPIDocument\Objects\IStandardObject<string, mixed> $attributes |
| 48 | + */ |
| 49 | + public function getValue(JsonAPIDocument\Objects\IStandardObject $attributes): Consistence\Enum\Enum|null |
| 50 | + { |
| 51 | + $value = $attributes->get($this->getMappedName()); |
| 52 | + |
| 53 | + $callable = [$this->typeClass, 'get']; |
| 54 | + |
| 55 | + if (is_callable($callable)) { |
| 56 | + $result = $value !== null ? call_user_func($callable, $value) : null; |
| 57 | + |
| 58 | + return $result instanceof Consistence\Enum\Enum ? $result : null; |
| 59 | + } |
| 60 | + |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + public function isNullable(): bool |
| 65 | + { |
| 66 | + return $this->isNullable; |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments