1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the licence along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto; |
12
|
|
|
|
13
|
|
|
class Dicto { |
14
|
|
|
private function __construct() {} |
15
|
|
|
|
16
|
|
|
static private $rt = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Discard the current definition if there is any. |
20
|
|
|
*/ |
21
|
288 |
|
public static function discardDefinition() { |
22
|
288 |
|
self::$rt = null; |
23
|
288 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Start the definition of a new ruleset. |
27
|
|
|
* |
28
|
|
|
* @throws \RuntimeException if definition was already started |
29
|
|
|
*/ |
30
|
287 |
|
public static function startDefinition() { |
31
|
287 |
|
if (self::$rt !== null) { |
32
|
1 |
|
throw new \RuntimeException("Already started a rule definition"); |
33
|
|
|
} |
34
|
287 |
|
self::$rt = new Definition\RuleDefinitionRT(); |
35
|
287 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Discard the current definition if there is any. |
39
|
|
|
* |
40
|
|
|
* @throws \RuntimeException if definition was not started or already ended |
41
|
|
|
* @return array containing Definition\RuleSet and config-array |
42
|
|
|
*/ |
43
|
281 |
|
public static function endDefinition() { |
44
|
281 |
|
if (self::$rt === null) { |
45
|
1 |
|
throw new \RuntimeException("Already ended or not even started the rule definition"); |
46
|
|
|
} |
47
|
281 |
|
$rule_set = self::$rt->ruleset(); |
48
|
281 |
|
$config = self::$rt->configuration(); |
49
|
281 |
|
self::discardDefinition(); |
50
|
281 |
|
return array($rule_set, $config ? $config : array()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Define a only-rule. |
55
|
|
|
* |
56
|
|
|
* @throws \RuntimeException if previous variable declaration has not finished |
57
|
|
|
* @throws \RuntimeException |
58
|
|
|
* @return Fluid\Only |
59
|
|
|
*/ |
60
|
36 |
|
public static function only() { |
61
|
36 |
|
if (self::$rt === null) { |
62
|
|
|
throw new \RuntimeException( |
63
|
|
|
"No variable definition allowed outside ruleset definition."); |
64
|
|
|
} |
65
|
36 |
|
return self::$rt->only(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Define the configuration for the project according to App\Config. |
70
|
|
|
* |
71
|
|
|
* @param array |
72
|
|
|
* @return null |
73
|
|
|
*/ |
74
|
3 |
|
public static function configuration(array $config) { |
75
|
3 |
|
self::$rt->configuration($config); |
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Define a new variable or reference an already defined variable to define |
80
|
|
|
* a rule. |
81
|
|
|
* |
82
|
|
|
* @throws \InvalidArgumentException if $arguments are passed |
83
|
|
|
* @throws \RuntimeException if previous variable declaration was not finished |
84
|
|
|
* @throws \RuntimeException if definition was not started |
85
|
|
|
* @return NewVar|RuleVar |
86
|
|
|
*/ |
87
|
284 |
|
public static function __callStatic($name, $arguments) { |
88
|
|
|
# ToDo: This is used in Definition\Fluid\Means as well. |
89
|
284 |
|
if (count($arguments) != 0) { |
90
|
1 |
|
throw new \InvalidArgumentException( |
91
|
|
|
"No arguments are allowed for definition of ". |
92
|
1 |
|
"or reference to variable."); |
93
|
|
|
} |
94
|
283 |
|
if (self::$rt === null) { |
95
|
1 |
|
throw new \RuntimeException( |
96
|
1 |
|
"No variable definition allowed outside ruleset definition."); |
97
|
|
|
} |
98
|
282 |
|
return self::$rt->variable($name); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|