1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Form\Former\Fields; |
13
|
|
|
|
14
|
|
|
use Former\Traits\Field; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* SelectUser is a Former field class for selecting project users. |
18
|
|
|
* |
19
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class SelectUser extends Field |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Properties to be injected as attributes. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $injectedProperties = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Prints out the current tag. |
32
|
|
|
* |
33
|
|
|
* @return string An input tag |
34
|
|
|
*/ |
35
|
1 |
|
public function render() |
36
|
|
|
{ |
37
|
1 |
|
$this->setAttribute('type', 'text'); |
38
|
1 |
|
$this->addClass('search-query'); |
39
|
1 |
|
$this->setId(); |
40
|
1 |
|
$this->removeAttribute('name'); |
41
|
|
|
|
42
|
1 |
|
$name = $this->name; |
43
|
|
|
|
44
|
|
|
// Render main input |
45
|
1 |
|
$input = parent::render(); |
46
|
|
|
|
47
|
|
|
// Render list of selected users |
48
|
1 |
|
$input .= $this->createDataList('datalist_' . $name); |
49
|
|
|
|
50
|
1 |
|
return $input; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns Html of selected users. |
55
|
|
|
* |
56
|
|
|
* @param string $id |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
1 |
|
private function createDataList($id) |
61
|
|
|
{ |
62
|
1 |
|
$dataList = '<ul id="' . $id . '" class="datalist ' . $id . '">'; |
63
|
1 |
|
if (is_array($this->value)) { |
64
|
|
|
foreach ($this->value as $key => $value) { |
65
|
|
|
$dataList .= $this->formatSelected($key, $value); |
66
|
|
|
} |
67
|
|
|
} |
68
|
1 |
|
$dataList .= '</ul>'; |
69
|
|
|
|
70
|
1 |
|
return $dataList; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns Html of a selected user row. |
75
|
|
|
* |
76
|
|
|
* @param int $id |
77
|
|
|
* @param string $name |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function formatSelected($id, $name) |
82
|
|
|
{ |
83
|
|
|
return '<li class="project-user' . $id . '">' |
84
|
|
|
. '<a href="" class="delete">' . trans('tinyissue.remove') . '</a>' |
85
|
|
|
. $name |
86
|
|
|
. '<input type="hidden" name="user[' . $id . ']" value="' . $name . '" />' |
87
|
|
|
. '</li>'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns an array of selected users. |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getValue() |
96
|
|
|
{ |
97
|
|
|
if (!is_array($this->value)) { |
98
|
|
|
return []; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return array_keys($this->value); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.