|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rebolon\Request\ParamConverter; |
|
4
|
|
|
|
|
5
|
|
|
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException; |
|
6
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
7
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* Class ListAbstractConverter |
|
12
|
|
|
* @package Rebolon\Request\ParamConverter\Library |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class ListAbstractConverter extends AbstractConverter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @inheritdoc |
|
18
|
|
|
*/ |
|
19
|
2 |
|
public function initFromRequest($jsonOrArray, $propertyPath) |
|
20
|
|
|
{ |
|
21
|
2 |
|
$this->checkMandatoriesImplementations(); |
|
22
|
|
|
|
|
23
|
2 |
|
self::$propertyPath[] = $propertyPath; |
|
24
|
|
|
|
|
25
|
2 |
|
$json = $this->checkJsonOrArray($jsonOrArray); |
|
26
|
|
|
|
|
27
|
|
|
// the API accept authors as one object or as an array of object, so i need to transform at least in one array |
|
28
|
2 |
|
$listItems = $json; |
|
29
|
2 |
|
if (is_object($json)) { |
|
30
|
|
|
$listItems = [$json]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
$entities = []; |
|
34
|
|
|
try { |
|
35
|
2 |
|
foreach ($listItems as $item) { |
|
36
|
2 |
|
self::$propertyPath[count(self::$propertyPath)] = '[' . count($entities) . ']'; |
|
37
|
|
|
|
|
38
|
2 |
|
$entities[] = $this->buildEntity($item); |
|
39
|
|
|
|
|
40
|
1 |
|
array_pop(self::$propertyPath); |
|
41
|
|
|
} |
|
42
|
1 |
|
} catch (ValidationException $e) { |
|
43
|
1 |
|
throw $e; |
|
44
|
|
|
} catch (\Exception $e) { |
|
45
|
|
|
$violationList = new ConstraintViolationList(); |
|
46
|
|
|
$violation = new ConstraintViolation($e->getMessage(), null, [], null, implode('.', self::$propertyPath), null); |
|
47
|
|
|
$violationList->add($violation); |
|
48
|
|
|
throw new ValidationException($violationList, sprintf('Wrong parameter to create new %s (generic)', static::RELATED_ENTITY), 420, $e); |
|
49
|
1 |
|
} finally { |
|
50
|
2 |
|
array_pop(self::$propertyPath); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
return $entities; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|