AndExpression::getExpressions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PeacefulBit\Slate\Parser\Nodes;
4
5
use function Nerd\Common\Arrays\toHeadTail;
6
use function Nerd\Common\Functional\tail;
7
8
use PeacefulBit\Slate\Core\Frame;
9
10
class AndExpression extends Node
11
{
12
    /**
13
     * @var array
14
     */
15
    private $expressions;
16
17
    /**
18
     * @param array $expressions
19
     */
20
    public function __construct(array $expressions)
21
    {
22
        $this->expressions = $expressions;
23
    }
24
25
    /**
26
     * @return array
27
     */
28
    public function getExpressions(): array
29
    {
30
        return $this->expressions;
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    public function __toString()
37
    {
38
        return '(and ' . implode(' ', array_map('strval', $this->expressions)) . ')';
39
    }
40
41
    /**
42
     * @param Frame $frame
43
     * @return mixed
44
     */
45
    public function evaluate(Frame $frame)
46
    {
47
        $iter = tail(function ($rest) use (&$iter, $frame) {
48
            if (empty($rest)) {
49
                return true;
50
            }
51
52
            list ($head, $tail) = toHeadTail($rest);
53
54
            if (!$frame($head)) {
55
                return false;
56
            }
57
58
            return $iter($tail);
59
        });
60
61
        return $iter($this->getExpressions());
62
    }
63
}
64