1 | <?php |
||
12 | class JsonApiParser extends JsonParser |
||
13 | { |
||
14 | /** |
||
15 | * Converts 'type' member to form name |
||
16 | * If not set, type will be converted to singular form. |
||
17 | * For example, 'articles' will be converted to 'Article' |
||
18 | * @var callable |
||
19 | */ |
||
20 | public $formNameCallback = ['tuyakhov\jsonapi\Inflector', 'type2form']; |
||
21 | |||
22 | /** |
||
23 | * Converts member names to variable names |
||
24 | * If not set, all special characters will be replaced by underscore |
||
25 | * For example, 'first-name' will be converted to 'first_name' |
||
26 | * @var callable |
||
27 | */ |
||
28 | public $memberNameCallback = ['tuyakhov\jsonapi\Inflector', 'member2var']; |
||
29 | |||
30 | /** |
||
31 | * Parse resource object into the input data to populates the model |
||
32 | * @inheritdoc |
||
33 | */ |
||
34 | public function parse($rawBody, $contentType) |
||
35 | { |
||
36 | $array = parent::parse($rawBody, $contentType); |
||
37 | if (!empty($array) && !ArrayHelper::keyExists('data', $array)) { |
||
38 | if ($this->throwException) { |
||
39 | throw new BadRequestHttpException('The request MUST include a single resource object as primary data.'); |
||
40 | } |
||
41 | return []; |
||
42 | } |
||
43 | $data = ArrayHelper::getValue($array, 'data', []); |
||
44 | if (empty($data)) { |
||
45 | return []; |
||
46 | } |
||
47 | if (ArrayHelper::isAssociative($data)) { |
||
48 | $result = $this->parseResource($data); |
||
49 | |||
50 | $relObjects = ArrayHelper::getValue($data, 'relationships', []); |
||
|
|||
51 | $result['relationships'] = $this->parseRelationships($relObjects); |
||
52 | } else { |
||
53 | foreach ($data as $object) { |
||
54 | $resource = $this->parseResource($object); |
||
55 | foreach (array_keys($resource) as $key) { |
||
56 | $result[$key][] = $resource[$key]; |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | return isset($result) ? $result : $array; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param $type 'type' member of the document |
||
66 | * @return string form name |
||
67 | */ |
||
68 | protected function typeToFormName($type) |
||
72 | |||
73 | /** |
||
74 | * @param array $memberNames |
||
75 | * @return array variable names |
||
76 | */ |
||
77 | protected function parseMemberNames(array $memberNames = []) |
||
81 | |||
82 | /** |
||
83 | * @param $item |
||
84 | * @return array |
||
85 | * @throws BadRequestHttpException |
||
86 | */ |
||
87 | protected function parseResource($item) |
||
106 | |||
107 | /** |
||
108 | * @param array $relObjects |
||
109 | * @return array |
||
110 | */ |
||
111 | protected function parseRelationships(array $relObjects = []) |
||
132 | } |
||
133 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.