Passed
Pull Request — dev (#27)
by
unknown
03:10
created

MarkerParser::getVariableValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2017
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Property\Service;
18
19
use CuyZ\Notiz\Domain\Property\Marker;
20
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
21
22
/**
23
 * Helper class to parse markers and replace them in a given string.
24
 */
25
class MarkerParser
26
{
27
    /**
28
     * @param string $string
29
     * @param Marker[] $markers
30
     * @return string
31
     */
32
    public function replaceMarkers($string, array $markers)
33
    {
34
        if (empty($markers)) {
35
            return $string;
36
        }
37
38
        $markers = $this->keyMarkersByName($markers);
39
        $matches = $this->matchMarkers($string);
40
41
        if (empty($matches[0])) {
42
            return $string;
43
        }
44
45
        list($identifiers, $variables, $roots) = $matches;
46
47
        $replacePairs = [];
48
49
        foreach ($variables as $index => $variable) {
50
            $identifier = $identifiers[$index];
51
            $root = $roots[$index];
52
            $marker = $markers[$root];
53
54
            $value = $this->getVariableValue($variable, $root, $marker);
55
56
            $replacePairs[$identifier] = $value;
57
        }
58
59
        return strtr($string, $replacePairs);
60
    }
61
62
    /**
63
     * This will set each marker's name as the array key
64
     *
65
     * @param array $markers
66
     * @return array
67
     */
68
    private function keyMarkersByName(array $markers)
69
    {
70
        $aux = [];
71
72
        // This will avoid looping on markers for each variable
73
        foreach ($markers as $marker) {
74
            $aux[$marker->getName()] = $marker;
75
        }
76
77
        return $aux;
78
    }
79
80
    /**
81
     * This method will find all markers in the string
82
     *
83
     * @param $string
84
     * @return mixed
85
     */
86
    private function matchMarkers($string)
87
    {
88
        preg_match_all(
89
            '/{
90
                        (
91
                            ([a-z]+[a-z0-1]*)           # The root variable
92
                            (?:\.[a-z]+[a-z0-1]*)*      # The other parts
93
                        )
94
                    }/xi',
95
            $string,
96
            $matches
97
        );
98
99
        return $matches;
100
    }
101
102
    /**
103
     * @param string $variable
104
     * @param string $root
105
     * @param Marker $marker
106
     * @return mixed
107
     */
108
    protected function getVariableValue($variable, $root, Marker $marker)
109
    {
110
        // We need to have the root name to allow the ObjectAccess class to
111
        // retrieve the value.
112
        $target = [
113
            $root => $marker->getValue(),
114
        ];
115
116
        return ObjectAccess::getPropertyPath($target, $variable);
117
    }
118
}
119