Passed
Push — master ( 672c08...9a8256 )
by Anton
02:40
created

ResourceTrait::setResourceRelationship()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 6
nop 2
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);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like link() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
126