Skip to content

Commit e52f123

Browse files
committed
small changes
1 parent 0fbe52b commit e52f123

File tree

4 files changed

+1469
-97
lines changed

4 files changed

+1469
-97
lines changed

.gitattributes

+29-30
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
# Autodetect file teks
2-
* text=auto
2+
* text=auto
33

44
# Perlakukan file - file ini sebagai teks
5-
*.php text
6-
*.css text
7-
*.js text
8-
*.txt text
9-
*.md text
10-
*.xml text
11-
*.json text
12-
*.bat text
13-
*.sql text
14-
*.yml text
5+
*.php text
6+
*.css text
7+
*.js text
8+
*.txt text
9+
*.md text
10+
*.xml text
11+
*.json text
12+
*.bat text
13+
*.sql text
14+
*.yml text
1515

1616
# Pastikan file - file ini tidak diutak-atik
17-
*.png binary
18-
*.jpg binary
19-
*.gif binary
20-
*.ttf binary
21-
*.phar binary
22-
*.pem binary
17+
*.png binary
18+
*.jpg binary
19+
*.gif binary
20+
*.ttf binary
21+
*.phar binary
22+
*.pem binary
2323

2424
# Jangan sertakan file dan folder ini ke arsip rilis
25-
/.github export-ignore
26-
/tests export-ignore
27-
/.editorconfig export-ignore
28-
/.gitattributes export-ignore
29-
/.gitignore export-ignore
25+
/.github export-ignore
26+
/tests export-ignore
27+
/.gitattributes export-ignore
28+
/.gitignore export-ignore
3029
/repositories.json export-ignore
31-
/.sami export-ignore
32-
*.bc.php export-ignore
33-
*.cache.php export-ignore
34-
*.log.php export-ignore
35-
*.session.php export-ignore
36-
/.php_cs export-ignore
37-
*.phar export-ignore
38-
*.pem export-ignore
30+
/.sami export-ignore
31+
*.bc.php export-ignore
32+
*.cache.php export-ignore
33+
*.log.php export-ignore
34+
*.session.php export-ignore
35+
/.php_cs export-ignore
36+
*.phar export-ignore
37+
*.pem export-ignore

system/foundation/http/header.php

+48-39
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace System\Foundation\Http;
44

5-
defined('DS') or exit('No direct script access.');
5+
defined('DS') or exit('No direct access.');
66

77
class Header implements \IteratorAggregate, \Countable
88
{
@@ -31,20 +31,23 @@ public function __construct(array $headers = [])
3131
*/
3232
public function __toString()
3333
{
34-
if (! $this->headers) {
34+
if (!$this->headers) {
3535
return '';
3636
}
3737

38-
$max = max(array_map('strlen', array_keys($this->headers))) + 1;
38+
$max = max(array_map(function ($key) {
39+
return mb_strlen((string) $key, '8bit');
40+
}, array_keys($this->headers))) + 1;
41+
3942
ksort($this->headers);
4043

4144
$content = '';
4245

4346
foreach ($this->headers as $name => $values) {
44-
$name = implode('-', array_map('ucfirst', explode('-', $name)));
47+
$name = $this->standardizeKey($name);
4548

4649
foreach ($values as $value) {
47-
$content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
50+
$content .= sprintf("%-{$max}s %s\r\n", $name . ':', $value);
4851
}
4952
}
5053

@@ -105,9 +108,9 @@ public function add(array $headers)
105108
*/
106109
public function get($key, $default = null, $first = true)
107110
{
108-
$key = strtr(strtolower($key), '_', '-');
111+
$key = $this->standardizeKey($key);
109112

110-
if (! array_key_exists($key, $this->headers)) {
113+
if (!array_key_exists($key, $this->headers)) {
111114
if (null === $default) {
112115
return $first ? null : [];
113116
}
@@ -131,18 +134,13 @@ public function get($key, $default = null, $first = true)
131134
*/
132135
public function set($key, $values, $replace = true)
133136
{
134-
$values = (array) $values;
135-
136-
$key = strtr(strtolower($key), '_', '-');
137-
$values = array_values($values);
138-
139-
if (true === $replace || ! isset($this->headers[$key])) {
140-
$this->headers[$key] = $values;
141-
} else {
142-
$this->headers[$key] = array_merge($this->headers[$key], $values);
143-
}
137+
$key = $this->standardizeKey($key);
138+
$values = array_values((array) $values);
139+
$this->headers[$key] = (true === $replace || !isset($this->headers[$key]))
140+
? $values
141+
: array_merge($this->headers[$key], $values);
144142

145-
if ('cache-control' === $key) {
143+
if ('Cache-Control' === $key) {
146144
$this->cacheControl = $this->parseCacheControl($values[0]);
147145
}
148146
}
@@ -156,7 +154,8 @@ public function set($key, $values, $replace = true)
156154
*/
157155
public function has($key)
158156
{
159-
return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
157+
$key = $this->standardizeKey($key);
158+
return array_key_exists($key, $this->headers);
160159
}
161160

162161
/**
@@ -179,10 +178,11 @@ public function contains($key, $value)
179178
*/
180179
public function remove($key)
181180
{
182-
$key = strtr(strtolower($key), '_', '-');
181+
$key = $this->standardizeKey($key);
182+
183183
unset($this->headers[$key]);
184184

185-
if ('cache-control' === $key) {
185+
if ('Cache-Control' === $key) {
186186
$this->cacheControl = [];
187187
}
188188
}
@@ -197,21 +197,19 @@ public function remove($key)
197197
*/
198198
public function getDate($key, \DateTime $default = null)
199199
{
200-
if (null === $value = $this->get($key)) {
200+
if (null === ($value = $this->get($key))) {
201201
return $default;
202202
}
203203

204204
if (false === ($date = \DateTime::createFromFormat(DATE_RFC2822, $value))) {
205-
throw new \RuntimeException(sprintf(
206-
"The '%s' HTTP header is not parseable (%s).", $key, $value
207-
));
205+
throw new \RuntimeException(sprintf("The '%s' HTTP header is not parseable (%s).", $key, $value));
208206
}
209207

210208
return $date;
211209
}
212210

213211
/**
214-
* Tambahkan penunjuk cache-control.
212+
* Tambahkan header Cache-Control.
215213
*
216214
* @param string $key
217215
* @param mixed $value
@@ -223,7 +221,7 @@ public function addCacheControlDirective($key, $value = true)
223221
}
224222

225223
/**
226-
* Periksa ada tidaknya suatu penunjuk cache-control.
224+
* Periksa ada tidaknya suatu header Cache-Control.
227225
*
228226
* @param string $key
229227
*
@@ -235,21 +233,19 @@ public function hasCacheControlDirective($key)
235233
}
236234

237235
/**
238-
* Ambil penunjuk cache-control berdasarkan key-nya.
236+
* Ambil penunjuk Cache-Control berdasarkan key-nya.
239237
*
240238
* @param string $key
241239
*
242240
* @return string|null
243241
*/
244242
public function getCacheControlDirective($key)
245243
{
246-
return array_key_exists($key, $this->cacheControl)
247-
? $this->cacheControl[$key]
248-
: null;
244+
return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
249245
}
250246

251247
/**
252-
* Hapus penunjuk cache-control berdasarkan key-nya.
248+
* Hapus penunjuk Cache-Control berdasarkan key-nya.
253249
*
254250
* @param string $key
255251
*/
@@ -282,7 +278,7 @@ public function count()
282278
}
283279

284280
/**
285-
* Ambil seluruh data cache-control.
281+
* Ambil seluruh data Cache-Control.
286282
*
287283
* @return string
288284
*/
@@ -296,19 +292,19 @@ protected function getCacheControlHeader()
296292
if (true === $value) {
297293
$parts[] = $key;
298294
} else {
299-
if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
300-
$value = '"'.$value.'"';
295+
if (preg_match('/[^a-zA-Z0-9._-]/', $value)) {
296+
$value = '"' . $value . '"';
301297
}
302298

303-
$parts[] = $key.'='.$value;
299+
$parts[] = $key . '=' . $value;
304300
}
305301
}
306302

307303
return implode(', ', $parts);
308304
}
309305

310306
/**
311-
* Parse header cache-control.
307+
* Parse header Cache-Control.
312308
*
313309
* @param string $header
314310
*
@@ -317,7 +313,7 @@ protected function getCacheControlHeader()
317313
protected function parseCacheControl($header)
318314
{
319315
preg_match_all(
320-
'#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#',
316+
'/([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?/',
321317
$header,
322318
$matches,
323319
PREG_SET_ORDER
@@ -326,11 +322,24 @@ protected function parseCacheControl($header)
326322
$parsed = [];
327323

328324
foreach ($matches as $match) {
329-
$parsed[strtolower($match[1])] = isset($match[3])
325+
$parsed[strtolower((string) $match[1])] = isset($match[3])
330326
? $match[3]
331327
: (isset($match[2]) ? $match[2] : true);
332328
}
333329

334330
return $parsed;
335331
}
332+
333+
/**
334+
* Standarisasi nama header
335+
*
336+
* @param string $key
337+
*
338+
* @return string
339+
*/
340+
protected static function standardizeKey($key)
341+
{
342+
$key = strtr(strtolower((string) $key), '_', '-');
343+
return str_replace(' ', '-', ucwords(strtr($key, '-', ' ')));
344+
}
336345
}

system/foundation/http/parameter.php

+13-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace System\Foundation\Http;
44

5-
defined('DS') or exit('No direct script access.');
5+
defined('DS') or exit('No direct access.');
66

77
class Parameter implements \IteratorAggregate, \Countable
88
{
@@ -74,58 +74,54 @@ public function add(array $parameters = [])
7474
*/
7575
public function get($path, $default = null, $deep = false)
7676
{
77-
if (! $deep || false === ($pos = strpos($path, '['))) {
77+
$path = (string) $path;
78+
79+
if (!$deep || false === ($pos = strpos($path, '['))) {
7880
return array_key_exists($path, $this->parameters)
7981
? $this->parameters[$path]
8082
: $default;
8183
}
8284

8385
$root = substr($path, 0, $pos);
8486

85-
if (! array_key_exists($root, $this->parameters)) {
87+
if (!array_key_exists($root, $this->parameters)) {
8688
return $default;
8789
}
8890

8991
$value = $this->parameters[$root];
9092
$currentKey = null;
9193

92-
for ($i = $pos, $count = strlen($path); $i < $count; ++$i) {
94+
for ($i = $pos, $count = mb_strlen($path, '8bit'); $i < $count; ++$i) {
9395
$char = $path[$i];
9496

9597
if ('[' === $char) {
9698
if (null !== $currentKey) {
97-
throw new \InvalidArgumentException(
98-
sprintf("Malformed path. Unexpected '[' at position %s", $i)
99-
);
99+
throw new \Exception(sprintf("Malformed path. Unexpected '[' at position %s", $i));
100100
}
101101

102102
$currentKey = '';
103103
} elseif (']' === $char) {
104104
if (null === $currentKey) {
105-
throw new \InvalidArgumentException(
106-
sprintf("Malformed path. Unexpected ']' at position %s", $i)
107-
);
105+
throw new \Exception(sprintf("Malformed path. Unexpected ']' at position %s", $i));
108106
}
109107

110-
if (! is_array($value) || ! array_key_exists($currentKey, $value)) {
108+
if (!is_array($value) || !array_key_exists($currentKey, $value)) {
111109
return $default;
112110
}
113111

114112
$value = $value[$currentKey];
115113
$currentKey = null;
116114
} else {
117115
if (null === $currentKey) {
118-
throw new \InvalidArgumentException(
119-
sprintf("Malformed path. Unexpected '%s' at position %s", $char, $i)
120-
);
116+
throw new \Exception(sprintf("Malformed path. Unexpected '%s' at position %s", $char, $i));
121117
}
122118

123119
$currentKey .= $char;
124120
}
125121
}
126122

127123
if (null !== $currentKey) {
128-
throw new \InvalidArgumentException("Malformed path. Path must ended with ']'.");
124+
throw new \Exception("Malformed path. Path must ended with ']'.");
129125
}
130126

131127
return $value;
@@ -244,11 +240,11 @@ public function filter(
244240
) {
245241
$value = $this->get($key, $default, $deep);
246242

247-
if (! is_array($options) && $options) {
243+
if (!is_array($options) && $options) {
248244
$options = ['flags' => $options];
249245
}
250246

251-
if (is_array($value) && ! isset($options['flags'])) {
247+
if (is_array($value) && !isset($options['flags'])) {
252248
$options['flags'] = FILTER_REQUIRE_ARRAY;
253249
}
254250

0 commit comments

Comments
 (0)