1 | <?php |
||
15 | abstract class BaseColumn |
||
16 | { |
||
17 | use Configurable, Attributable; |
||
18 | |||
19 | const |
||
20 | FORMATTER_HTML = 'html', |
||
21 | FORMATTER_IMAGE = 'image', |
||
22 | FORMATTER_TEXT = 'text', |
||
23 | FORMATTER_URL = 'url', |
||
24 | |||
25 | FORMATTER_DEFINITIONS = [ |
||
26 | self::FORMATTER_HTML => HtmlFormatter::class, |
||
27 | self::FORMATTER_IMAGE => ImageFormatter::class, |
||
28 | self::FORMATTER_TEXT => TextFormatter::class, |
||
29 | self::FORMATTER_URL => UrlFormatter::class, |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $label; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $attribute; |
||
41 | |||
42 | /** |
||
43 | * @var bool|null|string |
||
44 | */ |
||
45 | protected $sort; |
||
46 | |||
47 | /** |
||
48 | * @var string $value |
||
49 | */ |
||
50 | protected $value; |
||
51 | |||
52 | /** |
||
53 | * @var bool|null|string|BaseFilter |
||
54 | */ |
||
55 | protected $filter; |
||
56 | |||
57 | /** |
||
58 | * @var string|Formattable |
||
59 | */ |
||
60 | protected $format; |
||
61 | |||
62 | /** |
||
63 | * BaseColumn constructor. |
||
64 | * @param array $config |
||
65 | */ |
||
66 | public function __construct(array $config) |
||
72 | |||
73 | /** |
||
74 | * @param $row |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function getValue($row) |
||
80 | |||
81 | /** |
||
82 | * Render row attribute value. |
||
83 | * @param $row |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function render($row) |
||
90 | |||
91 | /** |
||
92 | * Format value with formatter. |
||
93 | * @param $value |
||
94 | * @return mixed |
||
95 | */ |
||
96 | public function formatTo($value) |
||
100 | |||
101 | /** |
||
102 | * Get title for grid head. |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getLabel(): string |
||
109 | |||
110 | /** |
||
111 | * Get attribute. |
||
112 | * @return string|null |
||
113 | */ |
||
114 | public function getAttribute() |
||
118 | |||
119 | /** |
||
120 | * @return bool|null|string |
||
121 | */ |
||
122 | public function getSort() |
||
129 | |||
130 | /** |
||
131 | * @return BaseFilter |
||
132 | */ |
||
133 | public function getFilter() |
||
137 | |||
138 | /** |
||
139 | * @param BaseFilter $filter |
||
140 | */ |
||
141 | protected function setFilter(BaseFilter $filter): void |
||
145 | |||
146 | /** |
||
147 | * @return void |
||
148 | */ |
||
149 | protected function buildFilter(): void |
||
170 | |||
171 | /** |
||
172 | * @param Formattable $formatter |
||
173 | */ |
||
174 | protected function setFormatter(Formattable $formatter): void |
||
178 | |||
179 | /** |
||
180 | * @throws Exception |
||
181 | * @return void |
||
182 | */ |
||
183 | protected function buildFormatter(): void |
||
202 | } |
||
203 |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.