Completed
Push — master ( 506ee6...2ad65e )
by Oscar
02:37
created

SimpleCrud::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
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
25
            ->select()
26
            ->orderBy("`{$entity->name}`.`id` DESC");
27
28
        if ($search->getPage()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $search->getPage() of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
29
            $query
30
                ->offset(($search->getPage() * 50) - 50)
31
                ->limit(50);
32
        }
33
34
        if ($this->searchFields === null) {
35
            $this->searchFields = [$this->getFirstField()];
36
        }
37
38
        foreach ($search->getWords() as $k => $word) {
39
            foreach ($this->searchFields as $field) {
40
                $query->where("`{$entity->name}`.`{$field}` LIKE :w{$k}", [":w{$k}" => "%{$word}%"]);
41
            }
42
        }
43
44
        $db = $entity->getDatabase();
45
46
        foreach ($search->getConditions() as $name => $value) {
47
            $related = $db->$name
48
                ->select()
49
                ->by('id', $value)
50
                ->run();
51
52
            $query->relatedWith($related);
53
        }
54
55
        return $query;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function search(SearchQuery $search)
62
    {
63
        $result = [];
64
65
        foreach ($this->getQuery($search)->run() as $row) {
66
            $result[$row->id] = $row->toArray();
67
        }
68
69
        return $result;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function create(array $data)
76
    {
77
        $row = $this->getDbEntity()->create();
78
79
        $this->save($row, $data);
80
81
        return $row->id;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function read($id)
88
    {
89
        $entity = $this->getDbEntity();
90
91
        $row = $entity[$id];
92
93
        if (empty($row)) {
94
            return;
95
        }
96
97
        $relations = $entity->getScheme()['relations'];
98
        $array = $row->toArray();
99
100
        foreach ($relations as $name => $relation) {
101
            if ($relation[0] === Scheme::HAS_MANY || $relation[0] === Scheme::HAS_MANY_TO_MANY) {
102
                $array[$name] = array_values($row->$name->id);
103
            }
104
        }
105
106
        return $array;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function update($id, array $data)
113
    {
114
        $row = $this->getDbEntity()[$id];
115
116
        $this->save($row, $data);
117
118
        return $row->toArray();
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function delete($id)
125
    {
126
        $entity = $this->getDbEntity();
127
128
        unset($entity[$id]);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getLabel($id, array $data)
135
    {
136
        return "{$id} - ".$data[$this->getFirstField()];
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    protected function getFirstField()
143
    {
144
        $entity = $this->getDbEntity();
145
146
        foreach (array_keys($entity->fields) as $key) {
147
            if ($key !== 'id') {
148
                return $key;
149
            }
150
        }
151
    }
152
153
    protected function save(Row $row, array $data)
154
    {
155
        $entity = $this->getDbEntity();
156
        $db = $entity->getDatabase();
157
        $scheme = $entity->getScheme();
158
159
        foreach ($data as $name => $value) {
160
            if (isset($scheme['relations'][$name])) {
161
                $row->$name = $db->$name->select()->by('id', $value)->run();
162
            } else {
163
                $row->$name = $value;
164
            }
165
        }
166
167
        $row->save(true);
168
    }
169
}
170