1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Grandstream-XMLApp |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 pudelek.org.pl |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view source file |
10
|
|
|
* that is bundled with this package in the file LICENSE |
11
|
|
|
* |
12
|
|
|
* @author Marcin Pudełek <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace mrcnpdlk\Grandstream\XMLApp\Application\Model\Components; |
17
|
|
|
|
18
|
|
|
use mrcnpdlk\Grandstream\XMLApp\Application\Model\Styles; |
19
|
|
|
use mrcnpdlk\Grandstream\XMLApp\Application\ModelInterface; |
20
|
|
|
use mrcnpdlk\Grandstream\XMLApp\MyXML; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ElemSelect |
24
|
|
|
* |
25
|
|
|
* This element is to render selection list fields on screen so that users could choose the answer to submit or |
26
|
|
|
* proceed. "name=value" will be passed to the query. The text for <item> element is the displayed option for |
27
|
|
|
* the list |
28
|
|
|
* |
29
|
|
|
* @package mrcnpdlk\Grandstream\XMLApp\Application\Model\Components |
30
|
|
|
*/ |
31
|
|
|
class ElemSelect implements ModelInterface, ElemInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* A unique id for the select field |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $sName; |
39
|
|
|
/** |
40
|
|
|
* @var \mrcnpdlk\Grandstream\XMLApp\Application\Model\Styles |
41
|
|
|
*/ |
42
|
|
|
private $oStyles; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $tItems; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* ElemSelect constructor. |
51
|
|
|
* |
52
|
|
|
* @param string $sName |
53
|
|
|
*/ |
54
|
|
|
public function __construct(string $sName) |
55
|
|
|
{ |
56
|
|
|
$this->sName = $sName; |
57
|
|
|
$this->setStyles(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Styles $oStyles |
62
|
|
|
* |
63
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\Application\Model\Components\ElemSelect |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function setStyles(Styles $oStyles = null) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
if (is_null($oStyles)) { |
68
|
|
|
$oStyles = new Styles(); |
69
|
|
|
} |
70
|
|
|
$this->oStyles = $oStyles; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\MyXML |
77
|
|
|
*/ |
78
|
|
|
public function getXml(): MyXML |
79
|
|
|
{ |
80
|
|
|
$oXml = new MyXML('select'); |
81
|
|
|
$oXml->setName($this->sName); |
82
|
|
|
if ($this->oStyles) { |
83
|
|
|
$oXml->insertChild($this->getStyles()->getXml()->asObject()); |
84
|
|
|
} |
85
|
|
|
$oItems = new MyXML('items'); |
86
|
|
|
foreach ($this->tItems as $item) { |
87
|
|
|
$oItem = new MyXML('item'); |
88
|
|
|
$oItem->asObject()->addAttribute('value', $item['value']); |
89
|
|
|
$oItem->asObject()[0] = $item['name']; |
90
|
|
|
$oItems->insertChild($oItem->asObject()); |
91
|
|
|
} |
92
|
|
|
$oXml->insertChild($oItems->asObject()); |
93
|
|
|
|
94
|
|
|
return $oXml; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\Application\Model\Styles |
99
|
|
|
*/ |
100
|
|
|
public function getStyles() |
101
|
|
|
{ |
102
|
|
|
return $this->oStyles; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $sName |
107
|
|
|
* @param string $sValue |
108
|
|
|
* |
109
|
|
|
* @return ElemSelect |
110
|
|
|
*/ |
111
|
|
|
public function addItem(string $sName, string $sValue) |
112
|
|
|
{ |
113
|
|
|
$this->tItems[] = [ |
114
|
|
|
'name' => $sName, |
115
|
|
|
'value' => $sValue, |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param int $iX |
123
|
|
|
* @param int $iY |
124
|
|
|
* |
125
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\Application\Model\Components\ElemSelect |
126
|
|
|
*/ |
127
|
|
|
public function move(int $iX, int $iY) |
128
|
|
|
{ |
129
|
|
|
$this->getStyles()->move($iX, $iY); |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.