Bonjour à tous,
J'ai deux classes nommées optListSelect et optListOption. Ces deux classes sont très pratiques pour le traitement automatique des listes d'options constituées en php à partir de bases de données.
Je voudrais créer une troisième classe optListGroup mais je suis dans le flou total sur la façon de commencer et d'articuler cette classe avec les deux autres.
Voici le code des deux classes existantes.
Classe optListSelect:Classe optListOption:
Code php : 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 <?php /** * @property-read array optListOption::options * @property string $name * @property string|int $id * @property boolean $disabled * @property-read optListOption::selected */ class optListSelect { private $options = []; private $name = ''; private $id = ''; private $disabled = false; public function get_name() { return $this->name; } public function set_name($val) { if(!is_string($val)) throw new InvalidArgumentException("Invalid argument for name attribute of <select> tag. String expected"); $this->name = $val; } public function unset_name() { $this->name = ''; } public function get_id() { return $this->id; } public function set_id($val) { if(!(is_string($val) or is_int($val))) throw new InvalidArgumentException("Invalid argument for id attribute of <select> tag. String or int expected"); $this->id = $val; } public function unset_id() { $this->id = ''; } public function get_disabled() { return $this->disabled; } public function set_disabled($val) { $this->disabled = (bool)$val; } public function unset_disabled($val) { $this->disabled = false; } public function get_selected() { $return = null; foreach($this->options as $option) { if($option->get_selected()) { //$return = $option; $return = ['value'=>$option->get_value(), 'text'=>$option->get_text()]; break; } } return $return; } public function get_options() { return $this->options; } public function addOption(optListOption $option) { $this->options[] = $option; } public function __toString() { // ordre des paramètres: name, id, disabled, options return sprintf( '<select%s%s%s>%s</select>', empty($this->name) ? '' : sprintf(' name="%s"', $this->name), empty($this->id) ? '' : sprintf(' id="%s"', $this->id), $this->disabled ? ' disabled="disabled"' : '', implode('', $this->options) ); } public function __construct($name, $id='', $disabled = false) { if (!$name) throw new BadMethodCallException("Invalid call for ".__METHOD__. ". Missing parameter"); $this->set_name($name); if (!$id) $id = $name; $this->set_id($id); $this->set_disabled($disabled); } }
Code php : 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 <?php /** * Classe décrivant une option de liste de sélection * @property mixed $value Valeur de l'option (attribut html value) * @property string $text Texte à afficher pour l'option. * @property boolean $selected L'option est sélectionnée ou non. */ class optListOption { private $value = null; private $text; private $selected = false; public function get_value() { return $this->value; } public function set_value($value) { if(!(is_null($value) or is_string($value) or is_int($value))) throw new InvalidArgumentException("Invalid typ for the value of the option"); $this->value = $value; } public function unset_value() { $this->value = null; } public function get_text() { return $this->text; } public function set_text($value) { if(!(is_null($value) or is_string($value) or is_int($value))) throw new InvalidArgumentException("Invalid typ for the text of the option"); $this->text = $value; } public function get_selected() { return $this->selected; } public function set_selected($val) { $this->selected = (bool)$val; } public function __construct($text = '', $value = null, $selected = false) { $this->set_value($value); $this->set_text($text); $this->set_selected($selected); } public function __toString() { return sprintf( '<option%s%s>%s</option>', is_null($this->value) ? '' : sprintf(' value="%s"', $this->value), $this->selected ? ' selected="selected"' : '', $this->text ); } }
Partager