Skip to content

Commit f908a6d

Browse files
committed
TASK: Implement ValueReferenceParser
1 parent 2f5a0a1 commit f908a6d

4 files changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* PackageFactory.ComponentEngine - Universal View Components for PHP
5+
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PackageFactory\ComponentEngine\Domain\VariableName;
24+
25+
final class VariableName
26+
{
27+
private function __construct(
28+
public readonly string $value
29+
) {
30+
}
31+
32+
public static function from(string $string): self
33+
{
34+
return new self($string);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* PackageFactory.ComponentEngine - Universal View Components for PHP
5+
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PackageFactory\ComponentEngine\Language\AST\Node\ValueReference;
24+
25+
use PackageFactory\ComponentEngine\Domain\VariableName\VariableName;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\Node;
27+
use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes;
28+
29+
final class ValueReferenceNode extends Node
30+
{
31+
public function __construct(
32+
public readonly NodeAttributes $attributes,
33+
public readonly VariableName $name
34+
) {
35+
}
36+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* PackageFactory.ComponentEngine - Universal View Components for PHP
5+
* Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PackageFactory\ComponentEngine\Language\Parser\ValueReference;
24+
25+
use PackageFactory\ComponentEngine\Domain\VariableName\VariableName;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\ValueReference\ValueReferenceNode;
27+
use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes;
28+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Scanner;
29+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Token;
30+
use PackageFactory\ComponentEngine\Parser\Tokenizer\TokenType;
31+
32+
final class ValueReferenceParser
33+
{
34+
/**
35+
* @param \Iterator<mixed,Token> $tokens
36+
* @return ValueReferenceNode
37+
*/
38+
public function parse(\Iterator $tokens): ValueReferenceNode
39+
{
40+
Scanner::assertType($tokens, TokenType::STRING);
41+
42+
$token = $tokens->current();
43+
44+
Scanner::skipOne($tokens);
45+
46+
return new ValueReferenceNode(
47+
attributes: new NodeAttributes(
48+
rangeInSource: $token->boundaries
49+
),
50+
name: VariableName::from($token->value)
51+
);
52+
}
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* PackageFactory.ComponentEngine - Universal View Components for PHP
5+
* Copyright (C) 2022 Contributors of PackageFactory.ComponentEngine
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PackageFactory\ComponentEngine\Test\Unit\Language\Parser\ValueReference;
24+
25+
use PackageFactory\ComponentEngine\Domain\VariableName\VariableName;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\ValueReference\ValueReferenceNode;
27+
use PackageFactory\ComponentEngine\Language\Parser\ValueReference\ValueReferenceParser;
28+
use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes;
29+
use PackageFactory\ComponentEngine\Parser\Source\Range;
30+
use PackageFactory\ComponentEngine\Parser\Source\Position;
31+
use PackageFactory\ComponentEngine\Parser\Source\Source;
32+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Tokenizer;
33+
use PHPUnit\Framework\TestCase;
34+
35+
final class ValueReferenceParserTest extends TestCase
36+
{
37+
/**
38+
* @test
39+
*/
40+
public function parsesValueReference(): void
41+
{
42+
$valueReferenceParser = new ValueReferenceParser();
43+
$tokens = Tokenizer::fromSource(Source::fromString('foo'))->getIterator();
44+
45+
$expectedValueReferenceNode = new ValueReferenceNode(
46+
attributes: new NodeAttributes(
47+
rangeInSource: Range::from(
48+
new Position(0, 0),
49+
new Position(0, 2)
50+
)
51+
),
52+
name: VariableName::from('foo')
53+
);
54+
55+
$this->assertEquals(
56+
$expectedValueReferenceNode,
57+
$valueReferenceParser->parse($tokens)
58+
);
59+
}
60+
}

0 commit comments

Comments
 (0)