AbstractSource::setSource()   A
last analyzed

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 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Grid\Source;
13
14
use Bluz\Grid\Data;
15
use Bluz\Grid\Grid;
16
use Bluz\Grid\GridException;
17
18
/**
19
 * Adapter
20
 *
21
 * @package  Bluz\Grid
22
 * @author   Anton Shevchuk
23
 */
24
abstract class AbstractSource
25
{
26
    /**
27
     * @var mixed source for build grid
28
     */
29
    protected $source;
30
31
    /**
32
     * @var array available filters
33
     */
34
    protected $filters = [
35
        Grid::FILTER_EQ => '=',
36
        Grid::FILTER_NE => '!=',
37
        Grid::FILTER_GT => '>',
38
        Grid::FILTER_GE => '>=',
39
        Grid::FILTER_LT => '<',
40
        Grid::FILTER_LE => '<=',
41
        Grid::FILTER_LIKE => 'like',
42
    ];
43
44
    /**
45
     * Process source
46
     *
47
     * @param int   $page
48
     * @param int   $limit
49
     * @param array $filters
50
     * @param array $orders
51
     *
52
     * @return Data
53
     */
54
    abstract public function process(int $page, int $limit, array $filters = [], array $orders = []): Data;
55
56
    /**
57
     * Setup source adapter
58
     *
59
     * @param  mixed $source
60
     *
61
     * @return void
62
     */
63 32
    public function setSource($source): void
64
    {
65 32
        $this->source = $source;
66 32
    }
67
68
    /**
69
     * Get source adapter
70
     *
71
     * @return mixed
72
     * @throws GridException
73
     */
74 14
    public function getSource()
75
    {
76 14
        if (!$this->source) {
77
            throw new GridException('Source Adapter should be initialized by `setSource` method');
78
        }
79 14
        return $this->source;
80
    }
81
}
82