DibiFluent   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 81.48%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 2
dl 0
loc 164
ccs 44
cts 54
cp 0.8148
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFluent() 0 4 1
A getLimit() 0 4 1
A getOffset() 0 4 1
A makeWhere() 0 12 3
A getRow() 0 7 1
A getCount() 0 5 1
A getData() 0 4 1
A filter() 0 6 2
A limit() 0 5 1
A sort() 0 6 2
B suggest() 0 29 7
1
<?php
2
3
/**
4
 * This file is part of the Grido (https://github.com/o5/grido)
5
 *
6
 * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
7
 *
8
 * For the full copyright and license information, please view
9
 * the file LICENSE.md that was distributed with this source code.
10
 */
11
12
namespace Grido\DataSources;
13
14
use Grido\Exception;
15
16
/**
17
 * Dibi Fluent data source.
18
 *
19
 * @package     Grido
20
 * @subpackage  DataSources
21
 * @author      Petr Bugyík
22
 *
23
 * @property-read \Dibi\Fluent $fluent
24
 * @property-read int $limit
25
 * @property-read int $offset
26
 * @property-read int $count
27
 * @property-read array $data
28
 */
29 1
class DibiFluent implements IDataSource
30
{
31
    use \Nette\SmartObject;
32
33
    /** @var \Dibi\Fluent */
34
    protected $fluent;
35
36
    /** @var int */
37
    protected $limit;
38
39
    /** @var int */
40
    protected $offset;
41
42
    /**
43
     * @param \Dibi\Fluent $fluent
44
     */
45 1
    public function __construct(\Dibi\Fluent $fluent)
46 1
    {
47
        $this->fluent = $fluent;
48
    }
49
50
    /**
51
     * @return \Dibi\Fluent
52
     */
53
    public function getFluent()
54
    {
55
        return $this->fluent;
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function getLimit()
62
    {
63 1
        return $this->limit;
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function getOffset()
70
    {
71
        return $this->offset;
72
    }
73
74
    /**
75
     * @param \Grido\Components\Filters\Condition $condition
76
     * @param \Dibi\Fluent $fluent
77
     */
78
    protected function makeWhere(\Grido\Components\Filters\Condition $condition, \Dibi\Fluent $fluent = NULL)
79 1
    {
80 1
        $fluent = $fluent === NULL
81
            ? $this->fluent
82 1
            : $fluent;
83 1
84 1
        if ($condition->callback) {
85 1
            call_user_func_array($condition->callback, [$condition->value, $fluent]);
86
        } else {
87 1
            call_user_func_array([$fluent, 'where'], $condition->__toArray('[', ']'));
88
        }
89
    }
90
91
    /********************************** inline editation helpers ************************************/
92
93
    /**
94
     * Default callback used when an editable column has customRender.
95
     * @param mixed $id
96
     * @param string $idCol
97
     * @return \DibiRow
98
     */
99
    public function getRow($id, $idCol)
100
    {
101
        $fluent = clone $this->fluent;
102
        return $fluent
103
            ->where("%n = %s", $idCol, $id)
104
            ->fetch();
105
    }
106
107
    /*********************************** interface IDataSource ************************************/
108
109
    /**
110
     * @return int
111
     */
112 1
    public function getCount()
113 1
    {
114
        $fluent = clone $this->fluent;
115
        return $fluent->count();
116
    }
117
118
    /**
119
     * @return array
120
     */
121 1
    public function getData()
122
    {
123
        return $this->fluent->fetchAll($this->offset, $this->limit);
124
    }
125
126 1
    /**
127
     * @param array $conditions
128
     */
129 1
    public function filter(array $conditions)
130 1
    {
131 1
        foreach ($conditions as $condition) {
132 1
            $this->makeWhere($condition);
133
        }
134
    }
135
136
    /**
137
     * @param int $offset
138
     * @param int $limit
139
     */
140 1
    public function limit($offset, $limit)
141 1
    {
142 1
        $this->offset = $offset;
143
        $this->limit = $limit;
144
    }
145
146
    /**
147
     * @param array $sorting
148
     */
149 1
    public function sort(array $sorting)
150 1
    {
151 1
        foreach ($sorting as $column => $sort) {
152 1
            $this->fluent->orderBy("%n", $column, $sort);
153
        }
154
    }
155
156
    /**
157
     * @param mixed $column
158
     * @param array $conditions
159
     * @param int $limit
160
     * @return array
161
     * @throws Exception
162
     */
163 1
    public function suggest($column, array $conditions, $limit)
164 1
    {
165 1
        $fluent = clone $this->fluent;
166 1
        if (is_string($column)) {
167
            $fluent->removeClause('SELECT')->select("DISTINCT %n", $column)->orderBy("%n", $column, 'ASC');
168 1
        }
169 1
170 1
        foreach ($conditions as $condition) {
171
            $this->makeWhere($condition, $fluent);
172 1
        }
173 1
174 1
        $items = [];
175 1
        $data = $fluent->fetchAll(0, $limit);
176 1
        foreach ($data as $row) {
177 1
            if (is_string($column)) {
178
                $value = (string) $row[$column];
179
            } elseif (is_callable($column)) {
180
                $value = (string) $column($row);
181
            } else {
182
                $type = gettype($column);
183
                throw new Exception("Column of suggestion must be string or callback, $type given.");
184 1
            }
185 1
186
            $items[$value] = \Latte\Runtime\Filters::escapeHtml($value);
187 1
        }
188 1
189
        is_callable($column) && sort($items);
190
        return array_values($items);
191 1
    }
192
}
193