|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column\Filter; |
|
4
|
|
|
|
|
5
|
|
|
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Range extends BaseColumnFilter |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $view = 'column.filter.range'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var ColumnFilterInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $from; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var ColumnFilterInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $to; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initialize column filter. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function initialize() |
|
28
|
|
|
{ |
|
29
|
|
|
parent::initialize(); |
|
30
|
|
|
|
|
31
|
|
|
$this->setHtmlAttribute('data-type', 'range'); |
|
32
|
|
|
$this->setHtmlAttribute('class', 'column-filter'); |
|
33
|
|
|
|
|
34
|
|
|
$this->getFrom()->initialize(); |
|
35
|
|
|
$this->getTo()->initialize(); |
|
36
|
|
|
|
|
37
|
|
|
$this->getFrom()->removeHtmlAttribute('data-type'); |
|
38
|
|
|
$this->getTo()->removeHtmlAttribute('data-type'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return ColumnFilterInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getFrom() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->from; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param ColumnFilterInterface $from |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setFrom(ColumnFilterInterface $from) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->from = $from; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return ColumnFilterInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getTo() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->to; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param ColumnFilterInterface $to |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setTo(ColumnFilterInterface $to) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->to = $to; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
public function toArray() |
|
85
|
|
|
{ |
|
86
|
|
|
return parent::toArray() + [ |
|
87
|
|
|
'from' => $this->getFrom(), |
|
88
|
|
|
'to' => $this->getTo(), |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $range |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function parseValue($range) |
|
98
|
|
|
{ |
|
99
|
|
|
if (strpos($range, '::') === false) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
list($from, $to) = explode('::', $range, 2); |
|
104
|
|
|
$from = $this->from->parseValue($from); |
|
105
|
|
|
$to = $this->to->parseValue($to); |
|
106
|
|
|
|
|
107
|
|
|
if (! empty($from) && ! empty($to)) { |
|
108
|
|
|
$this->setOperator('between'); |
|
109
|
|
|
|
|
110
|
|
|
return [$from, $to]; |
|
|
|
|
|
|
111
|
|
|
} elseif (! empty($from)) { |
|
112
|
|
|
$this->setOperator('greater_or_equal'); |
|
113
|
|
|
|
|
114
|
|
|
return $from; |
|
115
|
|
|
} elseif (! empty($to)) { |
|
116
|
|
|
$this->setOperator('less_or_equal'); |
|
117
|
|
|
|
|
118
|
|
|
return $to; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.