1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Anton Tuyakhov <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace tuyakhov\jsonapi; |
7
|
|
|
|
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\helpers\ArrayHelper; |
10
|
|
|
use yii\helpers\Inflector; |
11
|
|
|
use \yii\web\JsonParser; |
12
|
|
|
|
13
|
|
|
class JsonApiParser extends JsonParser |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Converts 'type' member to form name |
17
|
|
|
* If not set, type will be converted to singular form. |
18
|
|
|
* For example, 'articles' will be converted to 'Article' |
19
|
|
|
* @var callable |
20
|
|
|
*/ |
21
|
|
|
public $formNameCallback; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Converts member names to variable names |
25
|
|
|
* If not set, all special characters will be replaced by underscore |
26
|
|
|
* For example, 'first-name' will be converted to 'first_name' |
27
|
|
|
* @var callable |
28
|
|
|
*/ |
29
|
|
|
public $memberNameCallback; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Parse resource object into the input data to populates the model |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
|
|
public function parse($rawBody, $contentType) |
36
|
|
|
{ |
37
|
|
|
$array = parent::parse($rawBody, $contentType); |
38
|
|
|
if ($type = ArrayHelper::getValue($array, 'data.type')) { |
39
|
|
|
$formName = $this->typeToFormName($type); |
40
|
|
|
if ($attributes = ArrayHelper::getValue($array, 'data.attributes')) { |
41
|
|
|
$result[$formName] = array_combine($this->parseMemberNames(array_keys($attributes)), array_values($attributes)); |
|
|
|
|
42
|
|
|
} elseif ($id = ArrayHelper::getValue($array, 'data.id')) { |
43
|
|
|
$result[$formName] = ['id' => $id, 'type' => $type]; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
if ($relationships = ArrayHelper::getValue($array, 'data.relationships')) { |
46
|
|
|
foreach ($relationships as $name => $relationship) { |
47
|
|
|
if (isset($relationship[0])) { |
48
|
|
View Code Duplication |
foreach ($relationship as $item) { |
|
|
|
|
49
|
|
|
if (isset($item['type']) && isset($item['id'])) { |
50
|
|
|
$formName = $this->typeToFormName($item['type']); |
|
|
|
|
51
|
|
|
$result[$name][$formName][] = $item; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} elseif (isset($relationship['type']) && isset($relationship['id'])) { |
55
|
|
|
$formName = $this->typeToFormName($relationship['type']); |
|
|
|
|
56
|
|
|
$result[$name][$formName] = $relationship; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
$data = ArrayHelper::getValue($array, 'data', []); |
62
|
|
View Code Duplication |
foreach ($data as $relationLink) { |
|
|
|
|
63
|
|
|
if (isset($relationLink['type']) && isset($relationLink['id'])) { |
64
|
|
|
$formName = $this->typeToFormName($relationLink['type']); |
|
|
|
|
65
|
|
|
$result[$formName][] = $relationLink; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return isset($result) ? $result : $array; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $type 'type' member of the document |
74
|
|
|
* @return string form name |
75
|
|
|
*/ |
76
|
|
|
protected function typeToFormName($type) |
77
|
|
|
{ |
78
|
|
|
if ($this->formNameCallback !== null) { |
79
|
|
|
return call_user_func($this->formNameCallback, $type); |
80
|
|
|
} |
81
|
|
|
return Inflector::id2camel(Inflector::singularize($type)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $memberNames |
86
|
|
|
* @return array variable names |
87
|
|
|
*/ |
88
|
|
|
protected function parseMemberNames(array $memberNames = []) |
89
|
|
|
{ |
90
|
|
|
$callback = $this->memberNameCallback !== null ? $this->memberNameCallback : function($name) { |
91
|
|
|
return str_replace(' ', '_', preg_replace('/[^A-Za-z0-9]+/', ' ', $name)); |
92
|
|
|
}; |
93
|
|
|
return array_map($callback, $memberNames); |
94
|
|
|
} |
95
|
|
|
} |
|
|
|
|
96
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.