Completed
Push — master ( 0964f5...56617a )
by Oscar
03:10
created

SimpleCrud::getQuery()   C

Complexity

Conditions 8
Paths 96

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 41
rs 5.3846
nc 96
cc 8
eloc 23
nop 1
1
<?php
2
3
namespace Folk\Entities;
4
5
use Folk\SearchQuery;
6
use SimpleCrud\Row;
7
use SimpleCrud\Scheme\Scheme;
8
9
abstract class SimpleCrud extends AbstractEntity implements EntityInterface
10
{
11
    protected $searchFields;
12
13
    /**
14
     * Returns the simple-crud entity.
15
     * 
16
     * @return SimpleCrud\Entity
17
     */
18
    abstract protected function getDbEntity();
19
20
    protected function getQuery(SearchQuery $search)
21
    {
22
        $entity = $this->getDbEntity();
23
24
        $query = $entity->select();
25
26
        if ($search->getPage() !== null) {
27
            $query
28
                ->offset(($search->getPage() * 50) - 50)
29
                ->limit(50);
30
        }
31
32
        if ($this->searchFields === null) {
33
            $this->searchFields = [$this->getFirstField()];
34
        }
35
36
        $orderBy = $search->getSort() ?: 'id';
37
38
        if (isset($entity->getScheme()[$orderBy])) {
39
            $query->orderBy("`{$entity->name}`.`{$orderBy}`", $search->getDirection());
40
        }
41
42
        foreach ($search->getWords() as $k => $word) {
43
            foreach ($this->searchFields as $field) {
44
                $query->where("`{$entity->name}`.`{$field}` LIKE :w{$k}", [":w{$k}" => "%{$word}%"]);
45
            }
46
        }
47
48
        $db = $entity->getDatabase();
49
50
        foreach ($search->getConditions() as $name => $value) {
51
            $related = $db->$name
52
                ->select()
53
                ->by('id', $value)
54
                ->run();
55
56
            $query->relatedWith($related);
57
        }
58
59
        return $query;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function search(SearchQuery $search)
66
    {
67
        $result = [];
68
69
        foreach ($this->getQuery($search)->run() as $row) {
70
            $result[$row->id] = $row->toArray();
71
        }
72
73
        return $result;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function create(array $data)
80
    {
81
        $row = $this->getDbEntity()->create();
82
83
        $this->save($row, $data);
84
85
        return $row->id;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function read($id)
92
    {
93
        $entity = $this->getDbEntity();
94
95
        $row = $entity[$id];
96
97
        if (empty($row)) {
98
            return;
99
        }
100
101
        $relations = $entity->getScheme()['relations'];
102
        $array = $row->toArray();
103
104
        foreach ($relations as $name => $relation) {
105
            if ($relation[0] === Scheme::HAS_MANY || $relation[0] === Scheme::HAS_MANY_TO_MANY) {
106
                $array[$name] = array_values($row->$name->id);
107
            }
108
        }
109
110
        return $array;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function update($id, array $data)
117
    {
118
        $row = $this->getDbEntity()[$id];
119
120
        $this->save($row, $data);
121
122
        return $row->toArray();
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function delete($id)
129
    {
130
        $entity = $this->getDbEntity();
131
132
        unset($entity[$id]);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function getLabel($id, array $data)
139
    {
140
        return "{$id} - ".$data[$this->getFirstField()];
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    protected function getFirstField()
147
    {
148
        $entity = $this->getDbEntity();
149
150
        foreach (array_keys($entity->fields) as $key) {
151
            if ($key !== 'id') {
152
                return $key;
153
            }
154
        }
155
    }
156
157
    protected function save(Row $row, array $data)
158
    {
159
        $entity = $this->getDbEntity();
160
        $db = $entity->getDatabase();
161
        $scheme = $entity->getScheme();
162
163
        foreach ($data as $name => $value) {
164
            if (isset($scheme['relations'][$name])) {
165
                $row->$name = $db->$name->select()->by('id', $value)->run();
166
            } else {
167
                $row->$name = $value;
168
            }
169
        }
170
171
        $row->save(true);
172
    }
173
}
174