Completed
Push — develop ( 64da83...d844ac )
by Mathieu
01:46
created

DBObjectRelations::setRelations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Suricate\Traits;
3
4
use Suricate\DBObject;
5
6
trait DBObjectRelations
7
{
8
    protected $relations                    = [];
9
    protected $relationValues               = [];
10
    protected $loadedRelations              = [];
11
12
    /**
13
     * Get relation from its name
14
     * 
15
     * @param string $name
16
     */
17 1
    protected function getRelation($name)
18
    {
19 1
        if (isset($this->relationValues[$name]) && $this->isRelationLoaded($name)) {
20 1
            return $this->relationValues[$name];
21
        }
22
23 1
        if (!$this->isRelationLoaded($name)) {
24 1
            $this->loadRelation($name);
25 1
            $this->markRelationAsLoaded($name);
26
        }
27
28 1
        if (isset($this->relationValues[$name])) {
29 1
            return $this->relationValues[$name];
30
        }
31
32
        return null;
33
    }
34
35
    /**
36
     * Check if variable is predefined relation
37
     * @param  string  $name variable name
38
     * @return boolean
39
     */
40 5
    protected function isRelation($name)
41
    {
42 5
        return isset($this->relations[$name]);
43
    }
44
    /**
45
     * Define object relations
46
     *
47
     * @return DBObject
48
     */
49 18
    protected function setRelations()
50
    {
51 18
        $this->relations = [];
52
53 18
        return $this;
54
    }
55
56
    /**
57
     * Mark a relation as loaded
58
     * @param  string $name varialbe name
59
     * @return void
60
     */
61 1
    protected function markRelationAsLoaded($name)
62
    {
63 1
        if ($this->isRelation($name)) {
64 1
            $this->loadedRelations[$name] = true;
65
        }
66 1
    }
67
     /**
68
     * Check if a relation already have been loaded
69
     * @param  string  $name Variable name
70
     * @return boolean
71
     */
72 1
    protected function isRelationLoaded($name)
73
    {
74 1
        return isset($this->loadedRelations[$name]);
75
    }
76
77
    /**
78
     * Load realation according to relation type
79
     *
80
     * @param string $name
81
     * @return void
82
     */
83 1
    protected function loadRelation($name)
84
    {
85 1
        if ($this->isRelation($name)) {
86 1
            switch ($this->relations[$name]['type']) {
87 1
                case DBObject::RELATION_ONE_ONE:
88 1
                    $this->loadRelationOneOne($name);
89 1
                    return;
90
                case DBObject::RELATION_ONE_MANY:
91
                    $this->loadRelationOneMany($name);
92
                    return;
93
                case DBObject::RELATION_MANY_MANY:
94
                    $this->loadRelationManyMany($name);
95
                    return;
96
            }
97
        }
98
    }
99
100
    /**
101
     * Load one to one relationship
102
     *
103
     * @param string $name
104
     * @return void
105
     */
106 1
    private function loadRelationOneOne($name)
107
    {
108 1
        $target = $this->relations[$name]['target'];
109 1
        $source = $this->relations[$name]['source'];
110 1
        $this->relationValues[$name] = new $target();
111 1
        $this->relationValues[$name]->load($this->$source);
112 1
    }
113
114
    /**
115
     * Load one to many relationship
116
     *
117
     * @param string $name
118
     * @return void
119
     */
120
    private function loadRelationOneMany($name)
121
    {
122
        $target         = $this->relations[$name]['target'];
123
        $parentId       = $this->{$this->relations[$name]['source']};
124
        $parentIdField  = isset($this->relations[$name]['target_field']) ? $this->relations[$name]['target_field'] : null;
125
        $validate       = dataGet($this->relations[$name], 'validate', null);
126
        
127
        $this->relationValues[$name] = $target::loadForParentId($parentId, $parentIdField, $validate);
128
    }
129
130
    /**
131
     * Load many to many relationship
132
     *
133
     * @param string $name
134
     * @return void
135
     */
136
    private function loadRelationManyMany($name)
137
    {
138
        $pivot      = $this->relations[$name]['pivot'];
139
        $sourceType = $this->relations[$name]['source_type'];
140
        $target     = dataGet($this->relations[$name], 'target');
141
        $validate   = dataGet($this->relations[$name], 'validate', null);
142
143
        $this->relationValues[$name] = $pivot::loadFor($sourceType, $this->{$this->relations[$name]['source']}, $target, $validate);
144
    }
145
}