AbstractMapper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
8
namespace Zicht\Bundle\UrlBundle\Aliasing\Mapper;
9
10
use Zicht\Bundle\UrlBundle\Url\Rewriter;
11
12
/**
13
 * Base class for simple match mapping
14
 */
15
abstract class AbstractMapper implements UrlMapperInterface
16
{
17
    /**
18
     * @var array|\string[]
19
     */
20
    protected $contentTypes;
21
22
    /**
23
     * @var string
24
     */
25
    protected $pattern;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param string[] $contentTypes
31
     * @param string $pattern
32
     */
33 8
    public function __construct(array $contentTypes, $pattern)
34
    {
35 8
        $this->contentTypes = $contentTypes;
36 8
        $this->pattern = $pattern;
37 8
    }
38
39
    /**
40
     * Check if the mapper supports the given contentType
41
     *
42
     * @param string $contentType
43
     *
44
     * @return boolean
45
     */
46 3
    public function supports($contentType)
47
    {
48 3
        return in_array($contentType, $this->contentTypes);
49
    }
50
51
    /**
52
     * @{inheritDoc}
53
     */
54 5
    public function processAliasing($content, $mode, Rewriter $rewriter)
55
    {
56 5
        if (!preg_match_all($this->pattern, $content, $matches, PREG_SET_ORDER)) {
57
            // early return: if there are no matches, no need for the rest of the processing.
58
            return $content;
59
        }
60 5
        $groups = [];
61 5
        foreach ($matches as $match) {
62 5
            $groups[$match[2]][]= $match;
63
        }
64
65 5
        return $rewriter->rewriteMatches($content, $mode, $groups);
66
    }
67
}
68