AbstractSegmentFactory::getDelimiter()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
/**
3
 * Dash
4
 *
5
 * @link      http://github.com/DASPRiD/Dash For the canonical source repository
6
 * @copyright 2013-2015 Ben Scholzen 'DASPRiD'
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Dash\Parser;
11
12
use Interop\Container\ContainerInterface;
13
14
/**
15
 * Abstract factory for segments.
16
 *
17
 * The factory creates a segment parser. Parsers which share the same pattern and constraints will be cached and
18
 * re-used.
19
 */
20
abstract class AbstractSegmentFactory
21
{
22
    /**
23
     * @var Segment[]
24
     */
25
    protected $cache = [];
26
27
    /**
28
     * @var string
29
     */
30
    private $patternOptionKey;
31
32
    /**
33
     * @var string
34
     */
35
    private $delimiter;
36
37
    /**
38
     * Caches the abstract getter results to eliminate performance impact.
39
     */
40
    public function __construct()
41
    {
42
        $this->patternOptionKey = $this->getPatternOptionKey();
43
        $this->delimiter = $this->getDelimiter();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @return Segment
50
     */
51
    public function __invoke(ContainerInterface $container, $resolvedName, array $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resolvedName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        $pattern     = (isset($options[$this->patternOptionKey]) ? $options[$this->patternOptionKey] : '');
54
        $constraints = (isset($options['constraints']) ? $options['constraints'] : []);
55
        $key         = serialize([$pattern, $constraints]);
56
57
        if (!isset($this->cache[$key])) {
58
            $this->cache[$key] = new Segment($this->delimiter, $pattern, $constraints);
59
        }
60
61
        return $this->cache[$key];
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    abstract protected function getPatternOptionKey();
68
69
    /**
70
     * @return string
71
     */
72
    abstract protected function getDelimiter();
73
}
74