Completed
Pull Request — master (#2)
by Mathieu
05:06 queued 01:16
created

DBCollectionOneMany::getParentId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Suricate;
4
5
class DBCollectionOneMany extends DBCollection
6
{
7
    /* @var string Name of parent identifier field */
8
    protected $parentIdField    = 'parent_id';
9
10
    /* @var mixed parent identifier */
11
    protected $parentId;
12
13
    /* @var string parent field filter name */
14
    protected $parentFilterName;
15
16
    /* @var mixed parent filtering value */
17
    protected $parentFilterType;
18
19
   
20 1
    public function getParentIdField(): string
21
    {
22 1
        return $this->parentIdField;
23
    }
24
25 1
    public function getParentId()
26
    {
27 1
        return $this->parentId;
28
    }
29
30
    public function getParentFilterName()
31
    {
32
        return $this->parentFilterName;
33
    }
34
35
    public function getParentFilterType()
36
    {
37
        return $this->parentFilterType;
38
    }
39
40
    /**
41
     * Load entire table into collection
42
     * @return Collection Loaded collection
43
     */
44
    public static function loadAll()
45
    {
46
        $calledClass    = get_called_class();
47
        $collection     = new $calledClass;
48
49
        $sql  = "SELECT *";
50
        $sql .= "   FROM `" . $collection->getTableName() . "`";
51
        $sql .= "WHERE " . $collection->parentFilterName . "=:type";
52
53
        $sqlParams = ['type' => $collection->parentFilterType];
54
55
        $collection->loadFromSql($sql, $sqlParams);
56
57
        return $collection;
58
    }
59
60
    /**
61
     * Load items linked to a parentId
62
     * @param mixed        $parentId       Parent id description
63
     * @param string       $parentIdField  Name of parent id referencing field
64
     * @param \Closure|null $validate       Callback use to validate add to items collection
65
     */
66
    public static function loadForParentId($parentId, $parentIdField = null, $validate = null)
67
    {
68
        $calledClass   = get_called_class();
69
        $collection     = new $calledClass;
70
71
        if ($parentId != '') {
72
            $sqlParams     = [];
73
            $dbHandler     = Suricate::Database(true);
74
75
            if ($collection->getDBConfig() !== '') {
76
                $dbHandler->setConfig($collection->getDBConfig());
77
            }
78
79
            $sql  = "SELECT *";
80
            $sql .= " FROM `" . $collection->getTableName() . "`";
81
            $sql .= " WHERE";
82
            if ($parentIdField !== null) {
83
                $sql .= "`" . $parentIdField . "`=:parent_id";
84
            } else {
85
                $sql .= "`" . $collection->getParentIdField() . "`=:parent_id";
86
            }
87
88
            if ($collection->parentFilterType !== null) {
89
                $sql .= "   AND " . $collection->parentFilterName . "=:parent_type";
90
                $sqlParams['parent_type'] = $collection->parentFilterType;
91
            }
92
93
            $sqlParams['parent_id'] = $parentId;
94
            $results = $dbHandler->query($sql, $sqlParams)->fetchAll();
95
96
            if ($results !== false) {
97
                foreach ($results as $currentResult) {
98
                    $itemName = $collection->getItemsType();
99
                    $item = $itemName::instanciate($currentResult);
100
                    if ($validate === null || $validate($item)) {
101
                        $collection->addItem($item);
102
                    }
103
                }
104
            }
105
106
            $collection->parentId = $parentId;
107
        }
108
109
        return $collection;
110
    }
111
112
    public function setParentIdForAll($parentId)
113
    {
114
        $this->parentId = $parentId;
115
        foreach (array_keys($this->items) as $key) {
116
            $this->items[$key]->{$this->parentIdField} = $parentId;
117
        }
118
        return $this;
119
120
    }
121
122
    public function craftItem($itemData)
123
    {
124
        $itemName = $this->itemsType;
125
126
        foreach ($itemData as $data) {
127
            $newItem = new $itemName();
128
            $newItem->{$this->parentIdField} = $this->parentId;
129
            $hasData = false;
130
            foreach ($data as $field => $value) {
131
                $newItem->$field = $value;
132
                if ($value != '') {
133
                    $hasData = true;
134
                }
135
            }
136
137
            // Only add item if there's data inside
138
            if ($hasData) {
139
                $this->addItem($newItem);
140
            }
141
        }
142
    }
143
144
    public function save()
145
    {
146
        // 1st step : delete all records for current parentId
147
        $sql  = "DELETE FROM `" . $this->tableName . "`";
148
        $sql .= " WHERE";
149
        $sql .= "   `" . $this->parentIdField . "`=:parent_id";
150
151
        $sqlParams = ['parent_id' => $this->parentId];
152
        
153
        $this->connectDB();
154
        $this->dbLink->query($sql, $sqlParams);
155
156
        // 2nd step : save all current items
157
        foreach ($this->items as $currentItem) {
158
            $currentItem->save(true); // Force insert
159
        }
160
    }
161
}
162