StaticState::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Listing;
9
10
/**
11
 * Manually set state. Can be used to define default settings for listing as default state.
12
 */
13
class StaticState implements StateInterface
14
{
15
    /**
16
     * Static state does not have namespace by default.
17
     *
18
     * @var string
19
     */
20
    private $namespace = '';
21
22
    /**
23
     * Default sorter.
24
     *
25
     * @var string|null
26
     */
27
    private $sorter = null;
28
29
    /**
30
     * Default sorting direction.
31
     *
32
     * @var int
33
     */
34
    private $direction = SorterInterface::ASC;
35
36
    /**
37
     * Default limit.
38
     *
39
     * @var int
40
     */
41
    private $limit = 25;
42
43
    /**
44
     * Default set of filters associated with their values.
45
     *
46
     * @var array
47
     */
48
    private $filters = [];
49
50
    /**
51
     * @param string $sorter        Default sorter
52
     * @param array  $filters       Default set of filters in a form [filter => value]
53
     * @param int    $sortDirection Default sorter direction, ASC by default
54
     * @param int    $limit         Default limit, 25 by default
55
     */
56
    public function __construct(
57
        $sorter,
58
        array $filters = [],
59
        $sortDirection = SorterInterface::ASC,
60
        $limit = 25
61
    ) {
62
        $this->sorter = $sorter;
63
        $this->direction = $sortDirection;
64
        $this->filters = $filters;
65
        $this->limit = $limit;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function isActive()
72
    {
73
        //Static state is always active
74
        return true;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function withNamespace($namespace)
81
    {
82
        $state = clone $this;
83
        $state->namespace = $namespace;
84
85
        return $state;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getNamespace()
92
    {
93
        return $this->namespace;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function activeFilters()
100
    {
101
        return array_keys($this->filters);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getValue($filter, $default = null)
108
    {
109
        if (array_key_exists($filter, $this->filters)) {
110
            return $this->filters[$filter];
111
        }
112
113
        return $default;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function activeSorter()
120
    {
121
        return $this->sorter;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function sortDirection()
128
    {
129
        return $this->direction;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getPage()
136
    {
137
        return 1;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getLimit()
144
    {
145
        return $this->limit;
146
    }
147
}