Passed
Push — master ( 49aecd...a6523a )
by Maarten de
03:49 queued 15s
created

PathParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parse() 0 12 2
A parseInner() 0 10 2
A parseValuePath() 0 24 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cloudstek\SCIM\FilterParser;
6
7
use Cloudstek\SCIM\FilterParser\Exception\UnexpectedValueException;
8
use Nette\Tokenizer;
9
10
/**
11
 * SCIM Path Parser.
12
 */
13
class PathParser extends AbstractParser implements PathParserInterface
14
{
15
    /**
16
     * SCIM Path Parser.
17
     */
18
    public function __construct()
19
    {
20
        parent::__construct(ParserMode::PATH());
21
    }
22
23
    /**
24
     * Parse path string.
25
     *
26
     * @param string $input Attribute path..
27
     *
28
     * @throws Tokenizer\Exception
29
     *
30
     * @return AST\Path
31
     */
32
    public function parse(string $input): AST\Path
33
    {
34
        $stream = $this->tokenizer->tokenize($input);
35
36
        // Attribute path
37
        $attributePath = $this->parseAttributePath($stream);
38
39
        if ($stream->nextToken() !== null) {
40
            throw new UnexpectedValueException($stream);
41
        }
42
43
        return $attributePath;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    protected function parseInner(Tokenizer\Stream $stream, bool $inValuePath = false): ?AST\Node
50
    {
51
        // @codeCoverageIgnoreStart
52
        if ($inValuePath === false) {
53
            throw new \LogicException('This method should only be called when parsing a value path.');
54
        }
55
56
        // @codeCoverageIgnoreEnd
57
58
        return parent::parseInner($stream, $inValuePath);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    protected function parseValuePath(Tokenizer\Stream $stream, AST\AttributePath $attributePath): AST\ValuePath
65
    {
66
        $valuePath = parent::parseValuePath($stream, $attributePath);
67
68
        // Sub attribute
69
        if ($stream->isNext()) {
70
            $subAttr = $stream->consumeToken(self::T_SUBATTR)->value;
71
72
            // Strip off the '.' at the start
73
            $subAttr = ltrim($subAttr, '.');
74
75
            // Append to attribute path.
76
            $names = $attributePath->getNames();
77
            $names[] = $subAttr;
78
79
            $valuePath->setAttributePath(
80
                new AST\AttributePath(
81
                    $attributePath->getSchema(),
82
                    $names
83
                )
84
            );
85
        }
86
87
        return $valuePath;
88
    }
89
}
90