Bonjour et joyeux Noël à tous.
J'ai réécrit trois classes pour l'écriture d'une chaîne correspondant à une liste d'options complète:
- OptListSelect crée la balise <select>
- OptListGroup crée une balise <optgroup>
- OptListOption crée une balise <option>
J'aimerais mettre ces classes à disposition de tous mais je ne sais pas comment m'y prendre.
Auparavant, il faudrait que j'améliore la gestion de l'attribut selected. Cette gestion fonctionne bien en l'absence de groupes d'options mais ne permet pas de gérer la ou les options sélectionnées dans les groupes. Avant de monter une usine à gaz pour faire fonctionner cette partie (gestion de la ou des options sélectionnées), je pense que la structure de mes deux dernières classes n'est pas optimale. Merci de me donner votre avis. Est-ce que ce ne serait pas mieux de gérer toutes les options ensemble et de les affecter séparément aux groupes.
Je vous donne le code des trois classes dans leur état actuel et un exemple de code d'utilisation:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 <?php # Version 1.2 /** Compared to version 1.1, there is no more id handle. Id is now handled like other html attributes. */ namespace moimp\OptListSelect; use Exception; use moimp\OptListGroup\OptListGroup; use moimp\OptListOption\OptListOption; /** * Optionlist <select> : OptListSelect * * @link none * @author moimp * @copyright No copyright * Version 1.2, December 2019 * * Copyright (C) 2019 moimp * * Everyone is permitted to copy and distribute verbatim or modified * copies of this license document, and changing it is allowed as long * as the name is changed. */ class OptListSelect { /** * Options * @property-read array OptListOptions::options */ private $options = []; /** * Groups * @property-read array OptListGroups::groups */ private $groups = []; /** * Name * @property string name */ private $name = ''; /** * Attributes * @property-read array attributes */ private $attributes = []; public function getName():string { return $this->name; } public function setName(string $value) { $this->name = $value; } public function unsetName() { $this->name = ''; } public function getAttributes() { return $this->attributes; } public function addAttribute($name, $value='') { $this->attributes[$name] = $value; } public function removeAttribute($name) { unset($this->attributes[$name]); } // TESTER VALEUR RETOURNEE ET CE QU ON EN FAIT public function getSelected() { $return = null; foreach($this->options as $option) { if($option->getSelected()) { echo __line__,'<br/>'; //$return = $option; // valeur pour optgroup $return = ['value'=>$option->getValue(), 'text'=>$option->getText()]; break; } } return $return; } public function getOptions() { return $this->options; } // A TESTER public function addOption(OptListOption $option) { $this->options[] = $option; } // CREER CODE INTERNE A LA FONCTION public function removeOption($option) { } public function getGroups() { return $this->groups; } // A TESTER public function addGroup(optListGroup $group) { $this->groups[] = $group; } // CREER CODE INTERNE A LA FONCTION public function removeGroup($group) { } public function __toString() { $toInsert = []; $attributes = ''; // Makes attributestring if ( !empty($this->attributes) ) { foreach ($this->attributes as $attribute=>$value) { $attributes .= " $attribute='$value'"; } unset($attribute, $value); } // Makes options if (!empty($this->options)) $toInsert = array_merge($toInsert, $this->options); // Makes groups if (!empty($this->groups)) $toInsert = array_merge($toInsert, $this->groups); // Order of parameters %s within string select tag: name, attributes, $toInsert (= options and groups) return sprintf( "<select%s%s>%s</select>", empty($this->name) ? '' : sprintf(" name='%s'", $this->name), //empty($this->attributes['id']) ? '' : sprintf(" id='%s'", $this->attributes['id']), $attributes, implode('', $toInsert) ); } public function __construct(string $name, array $attributes = []) { if (!$name) throw new Exception("Invalid call for ".__METHOD__. ". First parameter 'name' is empty."); $this->setName($name); if (!empty($attributes)) { $allowedAttributeKeys = ['autocomplete', 'autofocus', 'class', 'disabled', 'id', 'multiple', 'required', 'size', 'style', 'tabindex']; $attributes = array_change_key_case($attributes); $keys = array_keys($attributes); foreach ($keys as $key=>&$value) { if (!in_array($value, $allowedAttributeKeys, true)) throw new Exception("Invalid call for ".__METHOD__. ". Second parameter key $key is not allowed as attribute."); $attributes[$value] = (string) $attributes[$value]; } unset($key, $value); foreach ($attributes as $attribute=>$value) { $this->addAttribute($attribute, $value); } unset($attribute, $value); } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 <?php # Version 1.2 /** This class is based on class OptListSelect. It uses options like OptListSelect. */ namespace moimp\OptListGroup; use Exception; use moimp\OptListOption\OptListOption; /** * Group for optionlist <optgroup> : OptListGroup * * @link none * @author moimp * @copyright No copyright * Version 1.2, December 2019 * * Copyright (C) 2019 moimp * * Everyone is permitted to copy and distribute verbatim or modified * copies of this license document, and changing it is allowed as long * as the name is changed. */ class OptListGroup { /** * Options * @property-read array OptListOptions::options */ private $options = []; /** * Label * @property string $label */ private $label; /** * @property string id // usefull for JavaScript */ private $id; public function getLabel() { return $this->label; } public function setLabel(string $value) { $this->label = $value; } public function unsetLabel() { $this->label = ''; } public function getId() { return $this->id; } public function setId(string $value) { $this->id = $value; } public function unsetId() { $this->id = null; } // TESTER VALEUR RETOURNEE ET CE QU ON EN FAIT public function getSelected() { $return = null; foreach($this->options as $option) { if($option->getSelected()) { //$return = $option; // valeur originale pour le groupe $return = ['value'=>$option->getValue(), 'text'=>$option->getText()]; break; } } return $return; } public function getOptions() { return $this->options; } public function addOption(OptListOption $option) { $this->options[] = $option; } // CREER CODE INTERNE A LA FONCTION public function removeOption($option) { } public function __toString() { // Order of parameters %s within string optgroup tag: label, id, options return sprintf( '<optgroup%s%s>%s</optgroup>', empty($this->label) ? '': sprintf(' label="%s"', $this->label), empty($this->id) ? '' : sprintf(' id="%s"', $this->id), implode('', $this->options) ); } public function __construct($label, $id=null) { if (!$label) throw new Exception("Invalid call for ".__METHOD__. ". Parameter 1 is missing"); $this->setLabel($label); if (!is_null($id)) { $this->setId($id); } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 <?php # Version 1.2 /** This class create a string as option tag <option> */ namespace moimp\OptListOption; use Exception; /** * Create an option string <option> for tag <select> or optgroup : OptListOption * * @link none * @author moimp * @copyright No copyright * Version 1.2, December 2019 * * Copyright (C) 2019 moimp * * Everyone is permitted to copy and distribute verbatim or modified * copies of this license document, and changing it is allowed as long * as the name is changed. */ class OptListOption { /** * value * @property string value: value of option (html attribute value) */ private $value = ''; /** * text * @property string text: Text to be inserted between opening and closing tags */ private $text = ''; /** * selected * @property boolean selected: html attribute selected */ private $selected = false; public function getValue():string { return $this->value; } public function setValue(string $value) { $this->value = $value; } public function unsetValue() { $this->value = null; } public function getText():string { return $this->text; } public function setText(string $value) { $this->text = $value; } public function getSelected():bool { return $this->selected; } public function setSelected($value) { $this->selected = (bool)$value; } public function __toString() { return sprintf( '<option%s%s>%s</option>', sprintf(" value='%s'", $this->value), $this->selected ? ' selected="selected"' : '', $this->text ); } public function __construct($value = '', $text = '', $selected = false) { $this->setValue($value); $this->setText($text); $this->setSelected($selected); echo __file__, ' ', __line__, ' ', $this->getSelected(), '<br/>'; } }P.S. J'envisage la même réécriture et mise à disposition de mes classes de construction de tableaux.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 <?php // Exemple de fichier utilisateur require('OptListSelect.php'); require('OptListGroup.php'); require('OptListOption.php'); use moimp\OptListSelect\OptListSelect; use moimp\OptListGroup\OptListGroup; use moimp\OptListOption\OptListOption; $x = new OptListSelect('xx', ['id'=>22]); $x->addOption(new OptListOption('','')); $x->addOption(new OptListOption('M','homme')); $x->addOption(new OptListOption('F','femme')); $g = new OptListGroup(('Âges')); $x->addGroup($g); $g->addOption(new OptListOption('<18', 'jeune')); $g->addOption(new OptListOption('18-62', 'adulte', true)); $g->addOption(new OptListOption('>62', 'sénior')); print_r($x->getOptions()); // ne retourne pas les options des groupes print_r('///'.$x->getSelected().'///');echo '<br/>'; print_r($g->getOptions());echo '<br/>'; print_r('///'.$g->getSelected().'///');echo '<br/>'; echo $x,'<br/>'; echo htmlspecialchars($x),'<br/>';
Partager