Skip to content

Commit f374655

Browse files
committed
Route: refactoring - added new consts
1 parent 0435cc7 commit f374655

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

src/Application/Routers/Route.php

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,28 @@ class Route implements Application\IRouter
3131
FILTER_TABLE = 'filterTable',
3232
FILTER_STRICT = 'filterStrict';
3333

34-
private const
35-
PRESENTER_KEY = 'presenter',
36-
MODULE_KEY = 'module';
34+
/** key used in metadata */
35+
protected const
36+
DEFAULT = 'defOut',
37+
FIXITY = 'fixity',
38+
FILTER_TABLE_FLIP = 'filterFlip';
3739

3840
/** url type */
39-
private const
41+
protected const
4042
HOST = 1,
4143
PATH = 2,
4244
RELATIVE = 3;
4345

4446
/** fixity types - how to handle default value? {@link Route::$metadata} */
45-
private const
47+
protected const
4648
OPTIONAL = 0,
4749
PATH_OPTIONAL = 1,
4850
CONSTANT = 2;
4951

52+
private const
53+
PRESENTER_KEY = 'presenter',
54+
MODULE_KEY = 'module';
55+
5056
/** @var array */
5157
public static $styles = [
5258
'#' => [ // default style for path parameters
@@ -193,7 +199,7 @@ public function match(Nette\Http\IRequest $httpRequest): ?Application\Request
193199

194200
// 2) CONSTANT FIXITY
195201
foreach ($this->metadata as $name => $meta) {
196-
if (!isset($params[$name]) && isset($meta['fixity']) && $meta['fixity'] !== self::OPTIONAL) {
202+
if (!isset($params[$name]) && isset($meta[self::FIXITY]) && $meta[self::FIXITY] !== self::OPTIONAL) {
197203
$params[$name] = null; // cannot be overwriten in 3) and detected by isset() in 4)
198204
}
199205
}
@@ -220,12 +226,12 @@ public function match(Nette\Http\IRequest $httpRequest): ?Application\Request
220226

221227
} elseif (isset($meta[self::FILTER_IN])) { // applies filterIn only to scalar parameters
222228
$params[$name] = $meta[self::FILTER_IN]((string) $params[$name]);
223-
if ($params[$name] === null && !isset($meta['fixity'])) {
229+
if ($params[$name] === null && !isset($meta[self::FIXITY])) {
224230
return null; // rejected by filter
225231
}
226232
}
227233

228-
} elseif (isset($meta['fixity'])) {
234+
} elseif (isset($meta[self::FIXITY])) {
229235
$params[$name] = $meta[self::VALUE];
230236
}
231237
}
@@ -279,7 +285,7 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re
279285

280286
if (isset($metadata[self::MODULE_KEY])) { // try split into module and [submodule:]presenter parts
281287
$module = $metadata[self::MODULE_KEY];
282-
if (isset($module['fixity'], $module[self::VALUE]) && strncmp($presenter, $module[self::VALUE] . ':', strlen($module[self::VALUE]) + 1) === 0) {
288+
if (isset($module[self::FIXITY], $module[self::VALUE]) && strncmp($presenter, $module[self::VALUE] . ':', strlen($module[self::VALUE]) + 1) === 0) {
283289
$a = strlen($module[self::VALUE]);
284290
} else {
285291
$a = strrpos($presenter, ':');
@@ -308,20 +314,20 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re
308314
$params[$name] = $params[$name] === false ? '0' : (string) $params[$name];
309315
}
310316

311-
if (isset($meta['fixity'])) {
317+
if (isset($meta[self::FIXITY])) {
312318
if ($params[$name] === $meta[self::VALUE]) { // remove default values; null values are retain
313319
unset($params[$name]);
314320
continue;
315321

316-
} elseif ($meta['fixity'] === self::CONSTANT) {
322+
} elseif ($meta[self::FIXITY] === self::CONSTANT) {
317323
return null; // missing or wrong parameter '$name'
318324
}
319325
}
320326

321-
if (is_scalar($params[$name]) && isset($meta['filterTable2'][$params[$name]])) {
322-
$params[$name] = $meta['filterTable2'][$params[$name]];
327+
if (is_scalar($params[$name]) && isset($meta[self::FILTER_TABLE_FLIP][$params[$name]])) {
328+
$params[$name] = $meta[self::FILTER_TABLE_FLIP][$params[$name]];
323329

324-
} elseif (isset($meta['filterTable2']) && !empty($meta[self::FILTER_STRICT])) {
330+
} elseif (isset($meta[self::FILTER_TABLE_FLIP]) && !empty($meta[self::FILTER_STRICT])) {
325331
return null;
326332

327333
} elseif (isset($meta[self::FILTER_OUT])) {
@@ -369,11 +375,11 @@ public function constructUrl(Application\Request $appRequest, Nette\Http\Url $re
369375
$url = $params[$name] . $url;
370376
unset($params[$name]);
371377

372-
} elseif (isset($metadata[$name]['fixity'])) { // has default value?
378+
} elseif (isset($metadata[$name][self::FIXITY])) { // has default value?
373379
if ($required === null && !$brackets) { // auto-optional
374380
$url = '';
375381
} else {
376-
$url = $metadata[$name]['defOut'] . $url;
382+
$url = $metadata[$name][self::DEFAULT] . $url;
377383
}
378384

379385
} else {
@@ -477,11 +483,11 @@ private function setMask(string $mask, array $metadata): void
477483
$meta = ($metadata[$name] ?? []) + (static::$styles['?' . $name] ?? static::$styles['?#']);
478484

479485
if (array_key_exists(self::VALUE, $meta)) {
480-
$meta['fixity'] = self::OPTIONAL;
486+
$meta[self::FIXITY] = self::OPTIONAL;
481487
}
482488

483-
unset($meta['pattern']);
484-
$meta['filterTable2'] = empty($meta[self::FILTER_TABLE]) ? null : array_flip($meta[self::FILTER_TABLE]);
489+
unset($meta[self::PATTERN]);
490+
$meta[self::FILTER_TABLE_FLIP] = empty($meta[self::FILTER_TABLE]) ? null : array_flip($meta[self::FILTER_TABLE]);
485491

486492
$metadata[$name] = $meta;
487493
if ($param !== '') {
@@ -542,19 +548,19 @@ private function setMask(string $mask, array $metadata): void
542548

543549
if ($default !== '') {
544550
$meta[self::VALUE] = substr($default, 1);
545-
$meta['fixity'] = self::PATH_OPTIONAL;
551+
$meta[self::FIXITY] = self::PATH_OPTIONAL;
546552
}
547553

548-
$meta['filterTable2'] = empty($meta[self::FILTER_TABLE]) ? null : array_flip($meta[self::FILTER_TABLE]);
554+
$meta[self::FILTER_TABLE_FLIP] = empty($meta[self::FILTER_TABLE]) ? null : array_flip($meta[self::FILTER_TABLE]);
549555
if (array_key_exists(self::VALUE, $meta)) {
550-
if (isset($meta['filterTable2'][$meta[self::VALUE]])) {
551-
$meta['defOut'] = $meta['filterTable2'][$meta[self::VALUE]];
556+
if (isset($meta[self::FILTER_TABLE_FLIP][$meta[self::VALUE]])) {
557+
$meta[self::DEFAULT] = $meta[self::FILTER_TABLE_FLIP][$meta[self::VALUE]];
552558

553559
} elseif (isset($meta[self::VALUE], $meta[self::FILTER_OUT])) {
554-
$meta['defOut'] = $meta[self::FILTER_OUT]($meta[self::VALUE]);
560+
$meta[self::DEFAULT] = $meta[self::FILTER_OUT]($meta[self::VALUE]);
555561

556562
} else {
557-
$meta['defOut'] = $meta[self::VALUE];
563+
$meta[self::DEFAULT] = $meta[self::VALUE];
558564
}
559565
}
560566
$meta[self::PATTERN] = "#(?:$pattern)\\z#A";
@@ -564,15 +570,15 @@ private function setMask(string $mask, array $metadata): void
564570
$re = '(?P<p' . $i . '>(?U)' . $pattern . ')' . $re;
565571
if ($brackets) { // is in brackets?
566572
if (!isset($meta[self::VALUE])) {
567-
$meta[self::VALUE] = $meta['defOut'] = null;
573+
$meta[self::VALUE] = $meta[self::DEFAULT] = null;
568574
}
569-
$meta['fixity'] = self::PATH_OPTIONAL;
575+
$meta[self::FIXITY] = self::PATH_OPTIONAL;
570576

571-
} elseif (isset($meta['fixity'])) {
577+
} elseif (isset($meta[self::FIXITY])) {
572578
if ($autoOptional) {
573579
$re = '(?:' . $re . ')?';
574580
}
575-
$meta['fixity'] = self::PATH_OPTIONAL;
581+
$meta[self::FIXITY] = self::PATH_OPTIONAL;
576582

577583
} else {
578584
$autoOptional = false;
@@ -608,7 +614,7 @@ public function getDefaults(): array
608614
{
609615
$defaults = [];
610616
foreach ($this->metadata as $name => $meta) {
611-
if (isset($meta['fixity'])) {
617+
if (isset($meta[self::FIXITY])) {
612618
$defaults[$name] = $meta[self::VALUE];
613619
}
614620
}
@@ -643,14 +649,14 @@ public function getTargetPresenters(): ?array
643649
$module = '';
644650

645651
if (isset($m[self::MODULE_KEY])) {
646-
if (($m[self::MODULE_KEY]['fixity'] ?? null) === self::CONSTANT) {
652+
if (($m[self::MODULE_KEY][self::FIXITY] ?? null) === self::CONSTANT) {
647653
$module = $m[self::MODULE_KEY][self::VALUE] . ':';
648654
} else {
649655
return null;
650656
}
651657
}
652658

653-
if (($m[self::PRESENTER_KEY]['fixity'] ?? null) === self::CONSTANT) {
659+
if (($m[self::PRESENTER_KEY][self::FIXITY] ?? null) === self::CONSTANT) {
654660
return [$module . $m[self::PRESENTER_KEY][self::VALUE]];
655661
}
656662
return null;

0 commit comments

Comments
 (0)