ResultTrait::resolveResultCallbacks()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Output;
4
5
/**
6
 * @author Martijn
7
 */
8
trait ResultTrait
9
{
10
    /**
11
     * List of result names to assign the matched text to.
12
     *
13
     * @var callable[]
14
     */
15
    private $resultCallbacks = [];
16
17 145
    private function resolveResultCallbacks(&$results, $text)
18
    {
19 145
        foreach ($this->resultCallbacks as $callback) {
20 31
            $callback($results, $text);
21
        }
22 145
    }
23
24
    /**
25
     * After parsing, assign the matched input of this parser to the named result.
26
     * Only assign if successfully matched entire parent up to root.
27
     *
28
     * @param string|int $key
29
     * @param null|callable|string $value
30
     *
31
     * @return $this
32
     */
33
    public function setResult($key = null, $value = null)
34
    {
35 10
        $this->resultCallbacks[] = function (&$results, $text) use (&$key, &$value) {
36 10
            if (is_callable($value)) {
37 1
                $text = $value($text);
38 9
            } elseif ($value !== null) {
39 1
                $text = $value;
40
            }
41
42 10
            $results[$key] = $text;
43 10
        };
44
45 10
        return $this;
46
    }
47
48
    /**
49
     * If result exists, concatenate the matched text as a string, otherwise
50
     * create it. If result is an array, concat to the last entry.
51
     *
52
     * @param null|string $key
53
     * @param null|callable|string $value
54
     *
55
     * @return $this
56
     */
57
    public function concatResult($key = null, $value = null)
58
    {
59 4
        $this->resultCallbacks[] = function (&$results, $text) use (&$key, &$value) {
60 4
            if (is_callable($value)) {
61 1
                $text = $value($text);
62 4
            } elseif ($value !== null) {
63 1
                $text = $value;
64
            }
65
66 4
            if (!isset($results[$key])) {
67 1
                $results[$key] = (string)$text;
68 4
            } elseif (is_array($results[$key])) {
69 1
                $results[$key][] = array_pop($results[$key]) . $text;
70
            } else {
71 3
                $results[$key] .= $text;
72
            }
73 4
        };
74
75 4
        return $this;
76
    }
77
78
    /**
79
     * Turn the result into an array and start a new entry.
80
     *
81
     * @param null|string $key
82
     * @param null|callable|string $value
83
     *
84
     * @return $this
85
     */
86
    public function pushResult($key = null, $value = null)
87
    {
88 21
        $this->resultCallbacks[] = function (&$results, $text) use (&$key, &$value) {
89 21
            if (is_callable($value)) {
90 1
                $text = $value($text);
91 21
            } elseif ($value !== null) {
92 19
                $text = $value;
93
            }
94
95 21
            if (!isset($results[$key])) {
96 20
                $results[$key] = [$text];
97 20
            } elseif (is_array($results[$key])) {
98 19
                $results[$key][] = $text;
99
            } else {
100 1
                $results[$key] = [$results[$key], $text];
101
            }
102 21
        };
103
104 3
        return $this;
105
    }
106
}
107