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