1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Stempler\Transform; |
6
|
|
|
|
7
|
|
|
use Spiral\Stempler\Node\HTML\Nil; |
8
|
|
|
use Spiral\Stempler\Node\Mixin; |
9
|
|
|
use Spiral\Stempler\Node\NodeInterface; |
10
|
|
|
use Spiral\Stempler\Node\Raw; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Carries value defined inside via attribute. |
14
|
|
|
*/ |
15
|
|
|
final class QuotedValue |
16
|
|
|
{ |
17
|
36 |
|
public function __construct( |
18
|
|
|
private readonly NodeInterface|string $value |
19
|
|
|
) { |
20
|
36 |
|
} |
21
|
|
|
|
22
|
18 |
|
public function getValue(): mixed |
23
|
|
|
{ |
24
|
18 |
|
return $this->value; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array<NodeInterface|string> |
29
|
|
|
*/ |
30
|
35 |
|
public function trimValue(): array |
31
|
|
|
{ |
32
|
35 |
|
$value = $this->value; |
33
|
35 |
|
if ($value instanceof Nil) { |
34
|
|
|
return []; |
35
|
|
|
} |
36
|
|
|
|
37
|
35 |
|
if (\is_string($value)) { |
38
|
20 |
|
return [new Raw(\trim($value, '\'"'))]; |
39
|
|
|
} |
40
|
|
|
|
41
|
16 |
|
if (!$value instanceof Mixin) { |
42
|
|
|
return [$value]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// trim mixin quotes |
46
|
16 |
|
$nodes = $value->nodes; |
47
|
|
|
|
48
|
16 |
|
if (\count($nodes) >= 3 && $nodes[0] instanceof Raw && $nodes[\count($nodes) - 1] instanceof Raw) { |
49
|
|
|
/** |
50
|
|
|
* TODO issue #767 |
51
|
|
|
* @link https://github.com/spiral/framework/issues/767 |
52
|
|
|
* @psalm-suppress InvalidArrayAccess |
53
|
|
|
*/ |
54
|
16 |
|
$quote = $nodes[0]->content[0]; |
55
|
16 |
|
if (!\in_array($quote, ['"', "'"])) { |
56
|
|
|
return $nodes; |
57
|
|
|
} |
58
|
|
|
|
59
|
16 |
|
$nodes[0] = new Raw(\ltrim($nodes[0]->content, $quote)); |
60
|
|
|
/** |
61
|
|
|
* TODO issue #767 |
62
|
|
|
* @link https://github.com/spiral/framework/issues/767 |
63
|
|
|
* @psalm-suppress NoInterfaceProperties |
64
|
|
|
*/ |
65
|
16 |
|
$content = $nodes[\count($nodes) - 1]->content; |
66
|
16 |
|
$nodes[\count($nodes) - 1] = new Raw( |
67
|
16 |
|
\is_string($content) ? \rtrim($content, $quote) : $content, |
68
|
16 |
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
16 |
|
foreach ($nodes as $index => $node) { |
72
|
16 |
|
if ($node instanceof Raw && $node->content === '') { |
73
|
16 |
|
unset($nodes[$index]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
16 |
|
return [new Mixin(\array_values($nodes))]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|