|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Base\Invocations\Directive; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\SDL\Contracts\Invocations\Directive\HasDirectives; |
|
13
|
|
|
use Railt\SDL\Contracts\Invocations\DirectiveInvocation; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Trait BaseDirectivesContainer |
|
17
|
|
|
* |
|
18
|
|
|
* @mixin HasDirectives |
|
19
|
|
|
*/ |
|
20
|
|
|
trait BaseDirectivesContainer |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Internal structure will look like this: |
|
24
|
|
|
* <code> |
|
25
|
|
|
* protected $directives = [ |
|
26
|
|
|
* ['name' => DirectiveInvocation], |
|
27
|
|
|
* ['name' => DirectiveInvocation], |
|
28
|
|
|
* ['name' => DirectiveInvocation], |
|
29
|
|
|
* ]; |
|
30
|
|
|
* </code> |
|
31
|
|
|
* |
|
32
|
|
|
* @var array|DirectiveInvocation[] |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $directives = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string|null $name |
|
38
|
|
|
* @return iterable|DirectiveInvocation[] |
|
39
|
|
|
*/ |
|
40
|
179 |
|
public function getDirectives(string $name = null): iterable |
|
41
|
|
|
{ |
|
42
|
179 |
|
foreach ($this->directives as $definitions) { |
|
43
|
90 |
|
foreach ((array)$definitions as $found => $invocation) { |
|
44
|
90 |
|
if ($name === null || $name === $found) { |
|
45
|
90 |
|
yield $found => $invocation; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
173 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
30 |
|
public function hasDirective(string $name): bool |
|
56
|
|
|
{ |
|
57
|
30 |
|
foreach ($this->getDirectives($name) as $directive) { |
|
58
|
24 |
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
30 |
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @return null|DirectiveInvocation |
|
67
|
|
|
*/ |
|
68
|
60 |
|
public function getDirective(string $name): ?DirectiveInvocation |
|
69
|
|
|
{ |
|
70
|
60 |
|
foreach ($this->getDirectives($name) as $directive) { |
|
71
|
54 |
|
return $directive; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
36 |
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return int |
|
79
|
|
|
*/ |
|
80
|
48 |
|
public function getNumberOfDirectives(): int |
|
81
|
|
|
{ |
|
82
|
48 |
|
return \count($this->directives); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|