forked from oreillymedia/Learning_PHP
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathValidatingFormHelper.php
More file actions
149 lines (137 loc) · 5.58 KB
/
Copy pathValidatingFormHelper.php
File metadata and controls
149 lines (137 loc) · 5.58 KB
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
<?php
// RUNNER STUB START
class FormHelper {
// RUNNER STUB END
// 이 코드는 "class FormHelper" 선언 다음에 들어간다.
// 다음 배열은 원소를 특정하고 어떤 속성에 어떤 값을 허용할지 결정한다.
protected $allowedAttributes = ['button' => ['type' => ['submit',
'reset',
'button' ] ] ];
// RUNNER STUB START
protected $values = array();
public function __construct($values = array()) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->values = $_POST;
} else {
$this->values = $values;
}
}
public function input($type, $attributes = array(), $isMultiple = false) {
$attributes['type'] = $type;
if (($type == 'radio') || ($type == 'checkbox')) {
if ($this->isOptionSelected($attributes['name'] ?? null,
$attributes['value'] ?? null)) {
$attributes['checked'] = true;
}
}
return $this->tag('input', $attributes, $isMultiple);
}
public function select($options, $attributes = array()) {
$multiple = $attributes['multiple'] ?? false;
return
$this->start('select', $attributes, $multiple) .
$this->options($attributes['name'] ?? null, $options) .
$this->end('select');
}
public function textarea($attributes = array()) {
$name = $attributes['name'] ?? null;
$value = $this->values[$name] ?? '';
return $this->start('textarea', $attributes) .
htmlentities($value) .
$this->end('textarea');
}
// RUNNER STUB END
// tag()를 고쳐 $this->attributes()의 첫 번째 인수로 $tag를 전달한다.
public function tag($tag, $attributes = array(), $isMultiple = false) {
return "<$tag {$this->attributes($tag, $attributes, $isMultiple)} />";
}
// start() 또한 $this->attributes()의 첫 번째 인수로 $tag를 전달하도록 고친다.
public function start($tag, $attributes = array(), $isMultiple = false) {
// <select>와 <textarea> 태그는 value 속성이 없다.
$valueAttribute = (! (($tag == 'select')||($tag == 'textarea')));
$attrs = $this->attributes($tag, $attributes, $isMultiple,
$valueAttribute);
return "<$tag $attrs>";
}
// RUNNER STUB START
public function end($tag) {
return "</$tag>";
}
// RUNNER STUB END
// attributes()는 $tag를 첫 번째 인수로 전달받고,
// $this->allowedAttributes에 허용된 속성이면 $attributeCheck를 설정한다.
// 허용된 속성이 정의되면 제공된 값을 보고,
// 허용된 값이 아니라면 예외를 발생시킨다.
protected function attributes($tag, $attributes, $isMultiple,
$valueAttribute = true) {
$tmp = array();
// 이 tag에 value 속성이 있고,
// name에 해당하는 값이 values 배열에 있으면
// value 속성을 설정한다.
if ($valueAttribute && isset($attributes['name']) &&
array_key_exists($attributes['name'], $this->values)) {
$attributes['value'] = $this->values[$attributes['name']];
}
if (isset($this->allowedAttributes[$tag])) {
$attributeCheck = $this->allowedAttributes[$tag];
} else {
$attributeCheck = array();
}
foreach ($attributes as $k => $v) {
// 속성의 값이 사용됐는지 확인한다.
if (isset($attributeCheck[$k]) &&
(! in_array($v, $attributeCheck[$k]))) {
throw new InvalidArgumentException("$v 는 $k 에 허용되지 않는 값입니다");
}
// 참 값은 불리언 속성을 의미한다.
if (is_bool($v)) {
if ($v) { $tmp[] = $this->encode($k); }
}
// 그렇지 않으면k =v 형태로 표현한다.
else {
$value = $this->encode($v);
// 다중 값이 지정된 원소라면,
//n ame 값 뒤에 []를 붙인다.
if ($isMultiple && ($k == 'name')) {
$value .= '[]';
}
$tmp[] = "$k=\"$value\"";
}
}
return implode(' ', $tmp);
}
// RUNNER STUB START
protected function options($name, $options) {
$tmp = array();
foreach ($options as $k => $v) {
$s = "<option value=\"{$this->encode($k)}\"";
if ($this->isOptionSelected($name, $k)) {
$s .= ' selected';
}
$s .= ">{$this->encode($v)}</option>";
$tmp[] = $s;
}
return implode('', $tmp);
}
protected function isOptionSelected($name, $value) {
// If there's no entry for $name in the values array,
// then this option can't be selected
if (! isset($this->values[$name])) {
return false;
}
// If the entry for $name in the values array is itself
// an array, check if $value is in that array
else if (is_array($this->values[$name])) {
return in_array($value, $this->values[$name]);
}
// Otherwise, compare $value to the entry to for $name
// in the values array
else {
return $value == $this->values[$name];
}
}
public function encode($s) {
return htmlentities($s);
}
}
// RUNNER STUB END