Completed
Push — master ( ed6595...d773f4 )
by Oscar
02:51
created

SimpleCrud::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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