| Total Complexity | 6 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 11 | final class ResourceToIdentifierTransformer implements DataTransformerInterface |
||
| 12 | { |
||
| 13 | /** @var RepositoryInterface */ |
||
| 14 | private $repository; |
||
| 15 | |||
| 16 | /** @var string */ |
||
| 17 | private $identifier; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param string $identifier |
||
| 21 | */ |
||
| 22 | public function __construct($repository, ?string $identifier = null) |
||
| 23 | { |
||
| 24 | $this->repository = $repository; |
||
| 25 | $this->identifier = $identifier ?? 'id'; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | public function transform($value) |
||
| 32 | { |
||
| 33 | if (null === $value) { |
||
| 34 | return null; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** @psalm-suppress ArgumentTypeCoercion */ |
||
| 38 | Assert::isInstanceOf($value, $this->repository->getClassName()); |
||
| 39 | |||
| 40 | return PropertyAccess::createPropertyAccessor()->getValue($value, $this->identifier); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | public function reverseTransform($value) |
||
| 63 | } |
||
| 64 | } |
||
| 65 |