Skip to content
This repository was archived by the owner on Sep 20, 2021. It is now read-only.

Adding possibility to return context keys for dynamically populate context. #13

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Ruler.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ public function assert ( $rule, Context $context = null ) {
* @throw \Hoa\Ruler\Exception
*/
public static function interprete ( $rule ) {

return static::getInterpreter()->visit(

$interpreter = static::getInterpreter();
//clear context keys
$interpreter->clearContextKeys();

return $interpreter->visit(
static::getCompiler()->parse($rule)
);
}
Expand Down Expand Up @@ -225,6 +229,17 @@ public static function getCompiler ( ) {

return static::$_compiler;
}

/**
* Get context keys.
*
* @access public
* @return array
*/
public function getContextKeys ( ) {

return static::getInterpreter()->getContextKeys();
}
}

}
Expand Down
37 changes: 33 additions & 4 deletions Visitor/Interpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ class Interpreter implements \Hoa\Visitor\Visit {
*/
protected $_current = null;


/**
* Context keys.
*
* @var array
*/
protected $_contextKeys = array();


/**
* Visit an element.
Expand Down Expand Up @@ -219,9 +225,11 @@ public function visit ( \Hoa\Visitor\Element $element, &$handle = null, $eldnah
switch($token) {

case 'identifier':
return true === $variable
? $this->_root->variable($value)
: $value;
if (true === $variable) {
array_push($this->_contextKeys, $value);
return $this->_root->variable($value);
}
return $value;

case 'true':
return true;
Expand Down Expand Up @@ -269,6 +277,27 @@ public function getRoot ( ) {

return $this->_root;
}

/**
* Get context keys.
*
* @access public
* @return array
*/
public function getContextKeys ( ) {

return $this->_contextKeys;
}

/**
* Clear context keys.
*
* @access public
*/
public function clearContextKeys ( ) {

$this->_contextKeys = array();
}
}

}