| 1 |
<?php |
| 2 |
|
| 3 |
namespace ILJ\Type; |
| 4 |
|
| 5 |
/** |
| 6 |
* Ruleset Datatype |
| 7 |
* |
| 8 |
* Provides an iterable container for every ruleset datatype |
| 9 |
* |
| 10 |
* @package ILJ\Type |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
class Ruleset { |
| 14 |
|
| 15 |
/** |
| 16 |
* Ruleset |
| 17 |
* |
| 18 |
* @var int $ruleset |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
public $ruleset = array(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Ruleset pointer |
| 25 |
* |
| 26 |
* @var int $rule_pointer |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
private $ruleset_pointer = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* Adds a new rule entry to the ruleset container. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* @param string $pattern The condition for applying the rule |
| 36 |
* @param string $value The value that gets applied |
| 37 |
* @param string $type Type for the rule (optional) |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
public function addRule($pattern, $value, $type = '') { |
| 41 |
if ('' != $pattern && '' != $value) { |
| 42 |
$rule = new \stdClass(); |
| 43 |
$rule->pattern = $pattern; |
| 44 |
$rule->value = $value; |
| 45 |
$rule->type = $type; |
| 46 |
$this->ruleset[] = $rule; |
| 47 |
return true; |
| 48 |
} |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Checks if the container has elements left to iterate. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public function hasRule() { |
| 59 |
return isset($this->ruleset[$this->ruleset_pointer]); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the rule entry from a specific index within the ruleset container. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* @param int $index The index of ruleset bag to retrieve a specific ruleset (optional) |
| 67 |
* @return null|object |
| 68 |
*/ |
| 69 |
public function getRule($index = -1) { |
| 70 |
if (!is_numeric($index)) { |
| 71 |
return null; |
| 72 |
} |
| 73 |
$index = (0 <= $index) ? $index : $this->ruleset_pointer; |
| 74 |
if (isset($this->ruleset[$index])) { |
| 75 |
return $this->ruleset[$index]; |
| 76 |
} |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Increments the position of the ruleset_pointer |
| 82 |
* |
| 83 |
* @since 1.0.0 |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function nextRule() { |
| 87 |
$this->ruleset_pointer++; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns the count of entries in ruleset |
| 92 |
* |
| 93 |
* @since 1.0.0 |
| 94 |
* @return int |
| 95 |
*/ |
| 96 |
public function getRuleCount() { |
| 97 |
return count($this->ruleset); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Resets the ruleset pointer |
| 102 |
* |
| 103 |
* @since 1.0.0 |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function reset() { |
| 107 |
$this->ruleset_pointer = 0; |
| 108 |
} |
| 109 |
} |
| 110 |
|