resolveContextObjectTag()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.9666
c 1
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace LSB\NumberingBundle\Service;
5
6
7
use LSB\NumberingBundle\Entity\NumberingCounterData;
8
use LSB\NumberingBundle\Model\Tag;
9
use LSB\NumberingBundle\Model\TimeContext;
10
11
class NumberingPatternResolver
12
{
13
14
    /**
15
     * @param string $numberPatternString
16
     * @param NumberingCounterData $counterData
17
     * @param \DateTime|null $date
18
     * @return string
19
     * @throws \Exception
20
     */
21 1
    public function resolve(string $numberPatternString, NumberingCounterData $counterData, \DateTime $date = null): string
22
    {
23 1
        $resolvedString = $this->resolveNumberTag($numberPatternString, $counterData);
24 1
        $resolvedString = $this->resolveDateTags($resolvedString, $date);
25 1
        $resolvedString = $this->resolveContextObjectTag($resolvedString, $counterData);
26
27 1
        return $resolvedString;
28
    }
29
30
    /**
31
     * @param string $numberPatternString
32
     * @param NumberingCounterData $counterData
33
     * @return string
34
     * @throws \Exception
35
     */
36 1
    private function resolveNumberTag(string $numberPatternString, NumberingCounterData $counterData): string
37
    {
38 1
        $tag = Tag::NUMBER;
39 1
        $currentValue = $counterData->getCurrent();
40
41 1
        preg_match_all(Tag::REG_EXPS[$tag], $numberPatternString, $matches);
42
43 1
        if (!empty($matches[0])) {
44 1
            $countMatches = count($matches[0]);
45
46 1
            for ($i = 0; $i < $countMatches; $i++) {
47 1
                $replacement = $currentValue;
48
49
                // check for length modifier
50 1
                if (!empty($matches[1][$i])) {
51
                    $charsNumber = (int)str_replace('|', '', $matches[1][$i]);
52
                    $replacement = sprintf("%0{$charsNumber}d", $currentValue);
53
                }
54
55 1
                $numberPatternString = str_replace($matches[0][$i], $replacement, $numberPatternString);
56
            }
57
        }
58
59 1
        return $numberPatternString;
60
    }
61
62
    /**
63
     * @param string $numberPatternString
64
     * @param NumberingCounterData $counterData
65
     * @return string
66
     * @throws \Exception
67
     */
68 1
    private function resolveContextObjectTag(string $numberPatternString, NumberingCounterData $counterData): string
69
    {
70 1
        $tag = Tag::CONTEXT_OBJECT;
71 1
        $currentValue = $counterData->getContextObjectValue();
72
73 1
        preg_match_all(Tag::REG_EXPS[$tag], $numberPatternString, $matches);
74
75 1
        if (!empty($matches[0])) {
76 1
            $countMatches = count($matches[0]);
77 1
            for ($i = 0; $i < $countMatches; $i++) {
78 1
                $replacement = $currentValue;
79 1
                $numberPatternString = str_replace($matches[0][$i], $replacement, $numberPatternString);
80
            }
81
        }
82
83 1
        return $numberPatternString;
84
    }
85
86
    /**
87
     * @param string $numberPatternString
88
     * @param \DateTime|null $date
89
     * @return string
90
     * @throws \Exception
91
     */
92 1
    private function resolveDateTags(string $numberPatternString, ?\DateTime $date = null): string
93
    {
94 1
        foreach (Tag::DATE_TAGS as $tag) {
95 1
            $currentValue = TimeContext::getValueForTag($tag, $date);
96
97 1
            preg_match_all(Tag::REG_EXPS[$tag], $numberPatternString, $matches);
98 1
            if (!empty($matches[0])) {
99 1
                $countMatches = count($matches[0]);
100
101 1
                for ($i = 0; $i < $countMatches; $i++) {
102 1
                    $replacement = (string)$currentValue;
103
104
                    // check for length modifier
105 1
                    switch ($tag) {
106
                        case Tag::YEAR:
107 1
                            if (!empty($matches[1][$i])) {
108
                                $charsNumber = (int)str_replace('|', '', $matches[1][$i]);
109
110
                                if (in_array($charsNumber, Tag::LENGTHS[Tag::YEAR])) {
111
                                    $replacement = substr($replacement, -$charsNumber);
112
                                }
113
                            }
114 1
                            break;
115
116
                        case Tag::DAY:
117
                        case Tag::MONTH:
118
                            $replacement = sprintf("%02d", $currentValue);
119
                            break;
120
                    }
121
122 1
                    $numberPatternString = str_replace($matches[0][$i], $replacement, $numberPatternString);
123
                }
124
            }
125
126
        }
127
128
129 1
        return $numberPatternString;
130
    }
131
132
133
}
134