| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Version package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (c) Nikola Posa <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For full copyright and license information, please refer to the LICENSE file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * located at the package root folder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Version\Constraint\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Version\Constraint\ConstraintInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Version\Constraint\Constraint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Version\Version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Version\Exception\InvalidConstraintStringException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Version\Exception\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author Nikola Posa <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | abstract class AbstractParser implements ParserInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $constraintString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 12 |  |     public function parse($constraintString) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 12 |  |         if (!is_string($constraintString)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |             throw InvalidConstraintStringException::forInvalidType($constraintString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 11 |  |         $constraintString = trim($constraintString); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 11 |  |         if ($constraintString == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |             throw InvalidConstraintStringException::forEmptyCostraintString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 10 |  |         $this->constraintString = $constraintString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 10 |  |         return $this->doParse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return ConstraintInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     abstract protected function doParse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 4 |  |     protected function error() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |         throw InvalidConstraintStringException::forConstraintString($this->constraintString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $constraintStringUnit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return Constraint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 10 |  |     protected function buildConstraintFromStringUnit($constraintStringUnit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 10 |  |         list($operator, $operandString) = array_values( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 10 |  |             $this->parseConstraintStringUnit($constraintStringUnit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 10 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 10 |  |         if (empty($operandString)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |             $this->error(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 9 |  |             return Constraint::fromProperties( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 9 |  |                 $operator ?: Constraint::OPERATOR_EQ, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 9 |  |                 Version::fromString($operandString) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 7 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |         } catch (Exception $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |             $this->error(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param string $constraintStringUnit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 88 | 10 |  |     protected function parseConstraintStringUnit($constraintStringUnit) | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 | 10 |  |         $operator = $operand = ''; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 92 | 10 |  |         $i = 0; | 
            
                                                        
            
                                    
            
            
                | 93 | 10 |  |         while (isset($constraintStringUnit[$i]) && !ctype_digit($constraintStringUnit[$i])) { | 
            
                                                        
            
                                    
            
            
                | 94 | 9 |  |             $i++; | 
            
                                                        
            
                                    
            
            
                | 95 | 9 |  |         } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 | 10 |  |         $operator = substr($constraintStringUnit, 0, $i); | 
            
                                                        
            
                                    
            
            
                | 98 | 10 |  |         $operand = substr($constraintStringUnit, $i); | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         return [ | 
            
                                                        
            
                                    
            
            
                | 101 | 10 |  |             'operator' => $operator, | 
            
                                                        
            
                                    
            
            
                | 102 | 10 |  |             'operand' => $operand, | 
            
                                                        
            
                                    
            
            
                | 103 | 10 |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |