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\db\BaseActiveRecord; |
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
use yii\helpers\Inflector; |
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
|
|
|
|
45
|
|
|
foreach ($this->resolveFields($this->fields(), $fields) as $name => $definition) { |
46
|
|
|
$attributes[$name] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $name); |
47
|
|
|
} |
48
|
|
|
return $attributes; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getResourceRelationships() |
55
|
|
|
{ |
56
|
|
|
$relationships = []; |
57
|
|
|
|
58
|
|
|
foreach ($this->resolveFields($this->extraFields()) as $name => $definition) { |
59
|
|
|
if (is_string($definition)) { |
60
|
|
|
$relation = $this->$definition; |
61
|
|
|
if (!is_array($relation)) { |
62
|
|
|
$relation = [$relation]; |
63
|
|
|
} |
64
|
|
|
foreach ($relation as $item) { |
65
|
|
|
$relationships[$name] = $item; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $relationships; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name the case sensitive name of the relationship. |
74
|
|
|
* @param $relationship |
75
|
|
|
*/ |
76
|
|
|
public function setResourceRelationship($name, $relationship) |
77
|
|
|
{ |
78
|
|
|
if (!is_array($relationship)) { |
79
|
|
|
$relationship = [$relationship]; |
80
|
|
|
} |
81
|
|
|
foreach ($relationship as $key => $value) { |
82
|
|
|
if ($value instanceof ActiveRecordInterface) { |
83
|
|
|
$this->link($name, $value); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function fields() |
92
|
|
|
{ |
93
|
|
|
$fields = array_keys(\Yii::getObjectVars($this)); |
94
|
|
|
return array_combine($fields, $fields); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function extraFields() |
101
|
|
|
{ |
102
|
|
|
return []; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $fields |
107
|
|
|
* @param array $fieldSet |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function resolveFields(array $fields, array $fieldSet = []) |
111
|
|
|
{ |
112
|
|
|
$result = []; |
113
|
|
|
|
114
|
|
|
foreach ($fields as $field => $definition) { |
115
|
|
|
if (is_int($field)) { |
116
|
|
|
$field = $definition; |
117
|
|
|
} |
118
|
|
|
if (empty($fieldSet) || in_array($field, $fieldSet, true)) { |
119
|
|
|
$result[$field] = $definition; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
} |
|
|
|
|
126
|
|
|
|
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.