VSVverkeerskunde /
gvq-api
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace VSV\GVQ_API\Account\Serializers; |
||
| 4 | |||
| 5 | use Ramsey\Uuid\UuidFactoryInterface; |
||
| 6 | use VSV\GVQ_API\Common\Serializers\JsonEnricher; |
||
| 7 | use VSV\GVQ_API\User\ValueObjects\Role; |
||
| 8 | |||
| 9 | class RegistrationJsonEnricher implements JsonEnricher |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var UuidFactoryInterface |
||
| 13 | */ |
||
| 14 | private $uuidFactory; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param UuidFactoryInterface $uuidFactory |
||
| 18 | */ |
||
| 19 | public function __construct(UuidFactoryInterface $uuidFactory) |
||
| 20 | { |
||
| 21 | $this->uuidFactory = $uuidFactory; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $json |
||
| 26 | * @return string |
||
| 27 | */ |
||
| 28 | public function enrich(string $json): string |
||
| 29 | { |
||
| 30 | $registrationAsArray = json_decode($json, true); |
||
| 31 | |||
| 32 | $registrationAsArray['id'] = $this->uuidFactory->uuid4()->toString(); |
||
| 33 | |||
| 34 | for ($index = 0; $index < count($registrationAsArray['aliases']); $index++) { |
||
|
0 ignored issues
–
show
|
|||
| 35 | $registrationAsArray['aliases'][$index]['id'] = $this->uuidFactory->uuid4()->toString(); |
||
| 36 | } |
||
| 37 | |||
| 38 | $registrationAsArray['user']['id'] = $this->uuidFactory->uuid4()->toString(); |
||
| 39 | $registrationAsArray['user']['role'] = Role::CONTACT; |
||
| 40 | |||
| 41 | return json_encode($registrationAsArray); |
||
| 42 | } |
||
| 43 | } |
||
| 44 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: