Hoa central
Ruler.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Ruler;
38 
39 use Hoa\Compiler;
40 use Hoa\Core;
41 use Hoa\File;
42 use Hoa\Visitor as HVisitor;
43 
52 class Ruler
53 {
59  protected static $_compiler = null;
60 
66  protected static $_interpreter = null;
67 
73  protected $_asserter = null;
74 
80  protected static $_defaultAsserter = null;
81 
82 
83 
92  public function assert($rule, Context $context = null)
93  {
94  if (is_string($rule)) {
95  $rule = static::interprete($rule);
96  }
97 
98  if (null === $context) {
99  $context = new Context();
100  }
101 
102  return $this->getAsserter($context)->visit($rule);
103  }
104 
112  public static function interpret($rule)
113  {
114  return static::getInterpreter()->visit(
115  static::getCompiler()->parse($rule)
116  );
117  }
118 
127  public static function interprete($rule)
128  {
129  return static::interpret($rule);
130  }
131 
137  public static function getInterpreter()
138  {
139  if (null === static::$_interpreter) {
140  static::$_interpreter = new Visitor\Interpreter();
141  }
142 
143  return static::$_interpreter;
144  }
145 
152  public function setAsserter(HVisitor\Visit $visitor)
153  {
154  $old = $this->_asserter;
155  $this->_asserter = $visitor;
156 
157  return $old;
158  }
159 
166  public function getAsserter(Context $context = null)
167  {
168  if (null === $asserter = $this->_asserter) {
169  return static::getDefaultAsserter($context);
170  }
171 
172  if (null !== $context) {
173  $asserter->setContext($context);
174  }
175 
176  return $asserter;
177  }
178 
185  public static function getDefaultAsserter(Context $context = null)
186  {
187  if (null === static::$_defaultAsserter) {
188  static::$_defaultAsserter = new Visitor\Asserter($context);
189  }
190 
191  if (null !== $context) {
192  static::$_defaultAsserter->setContext($context);
193  }
194 
195  return static::$_defaultAsserter;
196  }
197 
203  public static function getCompiler()
204  {
205  if (null === static::$_compiler) {
206  static::$_compiler = Compiler\Llk::load(
207  new File\Read('hoa://Library/Ruler/Grammar.pp')
208  );
209  }
210 
211  return static::$_compiler;
212  }
213 }
214 
218 Core\Consistency::flexEntity('Hoa\Ruler\Ruler');
static $_interpreter
Definition: Ruler.php:66
static getInterpreter()
Definition: Ruler.php:137
static $_compiler
Definition: Ruler.php:59
static interpret($rule)
Definition: Ruler.php:112
assert($rule, Context $context=null)
Definition: Ruler.php:92
static $_defaultAsserter
Definition: Ruler.php:80
static getDefaultAsserter(Context $context=null)
Definition: Ruler.php:185
setAsserter(HVisitor\Visit $visitor)
Definition: Ruler.php:152
static getCompiler()
Definition: Ruler.php:203
static interprete($rule)
Definition: Ruler.php:127
getAsserter(Context $context=null)
Definition: Ruler.php:166