1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace VSV\GVQ_API\Question\Serializers; |
||
4 | |||
5 | use Ramsey\Uuid\UuidFactoryInterface; |
||
6 | use VSV\GVQ_API\Common\Serializers\JsonEnricher; |
||
7 | |||
8 | class QuestionJsonEnricher implements JsonEnricher |
||
9 | { |
||
10 | /** |
||
11 | * @var UuidFactoryInterface |
||
12 | */ |
||
13 | private $uuidFactory; |
||
14 | |||
15 | /** |
||
16 | * @param UuidFactoryInterface $uuidFactory |
||
17 | */ |
||
18 | public function __construct(UuidFactoryInterface $uuidFactory) |
||
19 | { |
||
20 | $this->uuidFactory = $uuidFactory; |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * @param string $json |
||
25 | * @return string |
||
26 | */ |
||
27 | public function enrich(string $json): string |
||
28 | { |
||
29 | $questionAsArray = json_decode($json, true); |
||
30 | |||
31 | $questionAsArray['id'] = $this->uuidFactory->uuid4()->toString(); |
||
32 | |||
33 | for ($index = 0; $index < count($questionAsArray['answers']); $index++) { |
||
0 ignored issues
–
show
|
|||
34 | $questionAsArray['answers'][$index]['id'] = $this->uuidFactory->uuid4()->toString(); |
||
35 | } |
||
36 | |||
37 | return json_encode($questionAsArray); |
||
38 | } |
||
39 | } |
||
40 |
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: