Completed
Push — master ( c80620...0cd8d9 )
by Martijn
03:25
created

AssignTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveAssignCallbacks() 0 4 2
A assignTo() 0 7 1
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Output;
4
5
/**
6
 *
7
 * @author Martijn
8
 */
9
trait AssignTrait
10
{
11
12
    /**
13
     * List of result names to assign the matched text to.
14
     * @var callable[]
15
     */
16
    private $assignCallbacks = [];
17
18
    /**
19
     * Resolve all callbacks registered to this trait
20
     *
21
     * @param $text
22
     */
23
    private function resolveAssignCallbacks($text)
24
    {
25
        foreach ($this->assignCallbacks as $callback) {
26
            $callback($text);
27
        }
28
    }
29
30
    /**
31
     * After parsing, assign the matched input to the specified local variable.
32
     * Only assign if successfully matched entire parent up to root.
33
     *
34
     * @param mixed $variable
35
     * @return $this
36
     */
37
    public function assignTo(&$variable)
38
    {
39
        $this->assignCallbacks[] = function ($text) use (&$variable) {
40
            $variable = $text;
41
        };
42
43
        return $this;
44
    }
45
46
}
47