|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Anton Tuyakhov <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace tuyakhov\jsonapi; |
|
7
|
|
|
|
|
8
|
|
|
use yii\base\Arrayable; |
|
9
|
|
|
use yii\db\ActiveRecordInterface; |
|
10
|
|
|
use yii\helpers\Inflector; |
|
11
|
|
|
use yii\web\Link; |
|
12
|
|
|
use yii\web\Linkable; |
|
13
|
|
|
|
|
14
|
|
|
trait ResourceTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @return null|string |
|
18
|
|
|
*/ |
|
19
|
|
|
public function getId() |
|
20
|
|
|
{ |
|
21
|
|
|
if ($this instanceof ActiveRecordInterface) { |
|
22
|
|
|
return (string) $this->getPrimaryKey(); |
|
23
|
|
|
} |
|
24
|
|
|
return null; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getType() |
|
31
|
|
|
{ |
|
32
|
|
|
$reflect = new \ReflectionClass($this); |
|
|
|
|
|
|
33
|
|
|
$className = $reflect->getShortName(); |
|
34
|
|
|
return Inflector::pluralize(Inflector::camel2id($className)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $fields |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getResourceAttributes(array $fields = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$attributes = []; |
|
44
|
|
|
if ($this instanceof Arrayable) { |
|
45
|
|
|
$fieldDefinitions = $this->fields(); |
|
46
|
|
|
} else { |
|
47
|
|
|
$vars = array_keys(\Yii::getObjectVars($this)); |
|
|
|
|
|
|
48
|
|
|
$fieldDefinitions = array_combine($vars, $vars); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
foreach ($this->resolveFields($fieldDefinitions, $fields) as $name => $definition) { |
|
|
|
|
|
|
52
|
|
|
$attributes[$name] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $name); |
|
53
|
|
|
} |
|
54
|
|
|
return $attributes; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getResourceRelationships() |
|
61
|
|
|
{ |
|
62
|
|
|
$relationships = []; |
|
63
|
|
|
$fields = []; |
|
|
|
|
|
|
64
|
|
|
if ($this instanceof Arrayable) { |
|
65
|
|
|
$fields = $this->extraFields(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
foreach ($this->resolveFields($fields) as $name => $definition) { |
|
|
|
|
|
|
69
|
|
|
$relationships[$name] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $name); |
|
70
|
|
|
} |
|
71
|
|
|
return $relationships; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $name the case sensitive name of the relationship. |
|
76
|
|
|
* @param $relationship |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setResourceRelationship($name, $relationship) |
|
79
|
|
|
{ |
|
80
|
|
|
if (!is_array($relationship)) { |
|
81
|
|
|
$relationship = [$relationship]; |
|
82
|
|
|
} |
|
83
|
|
|
foreach ($relationship as $key => $value) { |
|
84
|
|
|
if ($value instanceof ActiveRecordInterface) { |
|
85
|
|
|
$this->link($name, $value); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $name the case sensitive name of the relationship. |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getRelationshipLinks($name) |
|
95
|
|
|
{ |
|
96
|
|
|
if (!$this instanceof Linkable) { |
|
97
|
|
|
return []; |
|
98
|
|
|
} |
|
99
|
|
|
$primaryLinks = $this->getLinks(); |
|
100
|
|
|
if (!array_key_exists(Link::REL_SELF, $primaryLinks)) { |
|
101
|
|
|
return []; |
|
102
|
|
|
} |
|
103
|
|
|
$resourceLink = is_string($primaryLinks[Link::REL_SELF]) ? rtrim($primaryLinks[Link::REL_SELF], '/') : null; |
|
104
|
|
|
if (!$resourceLink) { |
|
|
|
|
|
|
105
|
|
|
return []; |
|
106
|
|
|
} |
|
107
|
|
|
return [ |
|
108
|
|
|
Link::REL_SELF => "{$resourceLink}/relationships/{$name}", |
|
109
|
|
|
'related' => "{$resourceLink}/{$name}", |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param array $fields |
|
115
|
|
|
* @param array $fieldSet |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function resolveFields(array $fields, array $fieldSet = []) |
|
119
|
|
|
{ |
|
120
|
|
|
$result = []; |
|
121
|
|
|
|
|
122
|
|
|
foreach ($fields as $field => $definition) { |
|
123
|
|
|
if (is_int($field)) { |
|
124
|
|
|
$field = $definition; |
|
125
|
|
|
} |
|
126
|
|
|
if (empty($fieldSet) || in_array($field, $fieldSet, true)) { |
|
127
|
|
|
$result[$field] = $definition; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $result; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
|
|
|
|
|
134
|
|
|
|
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.