RouteTypes   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 10
c 0
b 0
f 0
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addType() 0 5 1
A initDefaultTypes() 0 7 1
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router;
4
5
/**
6
 * Trait RouteTypes
7
 *
8
 * @package Router
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/08/15)
11
 * @copyright Copyright (c) 2019, http://aeon.su
12
 */
13
14
/**
15
 * Default types for router - integer, string, command, list if ids
16
 *
17
 * @author gdever
18
 */
19
trait RouteTypes
20
{
21
22
    /**
23
     * Supported types of URL parameters
24
     *
25
     * @var array<string, string>
26
     */
27
    // TODO make it private and cache the result of working of this client code:
28
    // foreach ($this->types as $typeClass) {
29
    // $compiledRouterPattern = preg_replace('/' . $typeClass::searchRegExp() . '/', '(' . $typeClass::parserRegExp() . ')', $compiledRouterPattern);
30
    // }
31
    // foreach (array_keys($this->types) as $typeName) {
32
    // $regExPattern[] = $typeName;
33
    // }
34
    // it will let to speedup _getRouteMatcherRegExPattern and _getParameterNames
35
    // plus removing duplicate code
36
    protected $types = [];
37
38
    /**
39
     * Init types
40
     */
41
    private function initDefaultTypes(): void
42
    {
43
        $this->types['i'] = '\Mezon\Router\Types\IntegerRouterType';
44
        $this->types['a'] = '\Mezon\Router\Types\CommandRouterType';
45
        $this->types['il'] = '\Mezon\Router\Types\IntegerListRouterType';
46
        $this->types['fp'] = '\Mezon\Router\Types\FixPointNumberRouterType';
47
        $this->types['s'] = '\Mezon\Router\Types\StringRouterType';
48
    }
49
50
    /**
51
     * Method adds custom type
52
     *
53
     * @param string $typeName
54
     *            type name
55
     * @param string $className
56
     *            name of the class wich represents custom type
57
     */
58
    public function addType(string $typeName, string $className): void
59
    {
60
        $this->types = array_merge([
61
            $typeName => $className
62
        ], $this->types);
63
    }
64
}
65