| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Highlighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (C) 2016, Some right reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author Kacper "Kadet" Donat <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Contact with author: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Xmpp: [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * E-mail: [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * From Kadet with love. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace Kadet\Highlighter\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Kadet\Highlighter\Exceptions\NameConflictException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Kadet\Highlighter\Exceptions\NoSuchElementException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Kadet\Highlighter\Language\Language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Kadet\Highlighter\Parser\Validator\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class Rules extends \ArrayObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var Language Default language assigned to added rules. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $_language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var Validator Default validator used in added rules. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public $validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 34 |  |     private function _getName($name, $prefix) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 34 |  |         if (is_int($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 34 |  |             return $prefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 34 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             return $prefix ? "$prefix.$name" : $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 40 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 40 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 25 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Rules constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 25 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 24 |  |      * @param Language $language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 25 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 6 |  |     public function __construct($language) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $this->_language = $language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 39 |  |         $this->validator = new Validator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 39 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 25 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Adds array of rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 25 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |      * @param array       $rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string|null $prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 25 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @throws \LogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function addMany(array $rules, $prefix = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 25 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         foreach ($rules as $type => $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 25 |  |             $type = $this->_getName($type, $prefix); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 23 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 23 |  |             if ($rule instanceof Rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $this->add($type, $rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 25 |  |             } elseif (is_array($rule)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 16 |  |                 $this->addMany($rule, $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 16 |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 throw new \LogicException('Array values has to be either arrays of rules or rules.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 25 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 16 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 16 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 25 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 25 |  |      * Adds one rule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 25 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param Rule   $rule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @throws NameConflictException When there is already defined rule with given name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function add($type, Rule $rule) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         if (!isset($this[$type])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this[$type] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if ($rule->language === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $rule->language = $this->_language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if ($rule->validator === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $rule->validator = $this->validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         $rule->factory->setBase($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if ($rule->name !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             if (isset($this[$type][$rule->name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |                 throw new NameConflictException("Rule with '{$rule->name}' is already defined, name has to be unique!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |             $this[$type][$rule->name] = $rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         $this[$type][] = $rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Return reference to rule of given type and index. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 22 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 22 |  |      * @param mixed  $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 22 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return \Kadet\Highlighter\Parser\Rule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 22 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function &rule($type, $index = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return $this[$type][$index]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Retrieves all rules of given type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @return Rule[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @throws NoSuchElementException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |     public function rules($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |         if (!isset($this[$type])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |             throw new NoSuchElementException("There isn't any rule of '$type' type."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return $this[$type]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * Replaces rule of given type and index with provided one. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param Rule $replacement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param      $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param int  $index | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |     public function replace(Rule $replacement, $type, $index = 0) | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         $current = $this->rule($type, $index); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         if ($current->name !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |             $replacement->name = $current->name; | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         $this[$type][$index] = $replacement; | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * Removes rule of given type and index. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param mixed  $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @throws NoSuchElementException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     public function remove($type, $index = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         if ($index === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             unset($this[$type]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         if(!isset($this[$type][$index])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             throw new NoSuchElementException("There is no rule '$type' type indexed by '$index'."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         unset($this[$type][$index]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * Retrieves all rule set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * @return Rule[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     public function all() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         $items = $this->getArrayCopy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         if(empty($items)) return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         return call_user_func_array('array_merge', $items); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * @return Language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     public function getLanguage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         return $this->_language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      * @param Language $language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     public function setLanguage(Language $language = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         $this->_language = $language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 209 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 210 |  |  |  |