| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @link        http://github.com/seboettg/citeproc-php for the source repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright   Copyright (c) 2019 Sebastian Böttger. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license     https://opensource.org/licenses/MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Seboettg\CiteProc\Constraint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Seboettg\Collection\ArrayList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | abstract class AbstractConstraint implements Constraint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $match; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var ArrayList\ArrayListInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $conditionVariables; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param string $variable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param stdClass $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     abstract protected function matchForVariable(string $variable, stdClass $data): bool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Variable constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param string $variableValues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 69 |  |     public function __construct(string $variableValues, string $match = "any") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 69 |  |         $this->conditionVariables = new ArrayList(...explode(" ", $variableValues)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 69 |  |         $this->match = $match; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 69 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param stdClass $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param int|null $citationNumber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 62 |  |     public function validate(stdClass $data, int $citationNumber = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 62 |  |         switch ($this->match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             case Constraint::MATCH_ALL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 58 |  |                 return $this->matchAll($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             case Constraint::MATCH_NONE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 33 |  |                 return $this->matchNone($data); //no match for any value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             case Constraint::MATCH_ANY: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 45 |  |                 return $this->matchAny($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 45 |  |     private function matchAny(stdClass $data): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 45 |  |         return $this->conditionVariables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ->map(function (string $conditionVariable) use ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 45 |  |                 return $this->matchForVariable($conditionVariable, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 45 |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ->filter(function (bool $match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 45 |  |                 return $match === true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 45 |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 45 |  |             ->count() > 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 58 |  |     private function matchAll(stdClass $data): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 58 |  |         return $this->conditionVariables | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             ->map(function (string $conditionVariable) use ($data) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 58 |  |                 return $this->matchForVariable($conditionVariable, $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 58 |  |             }) | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             ->filter(function (bool $match) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 58 |  |                 return $match === true; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 58 |  |             }) | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 58 |  |             ->count() === $this->conditionVariables->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 33 |  |     private function matchNone(stdClass $data): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 33 |  |         return $this->conditionVariables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ->map(function (string $conditionVariable) use ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 33 |  |                 return $this->matchForVariable($conditionVariable, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 33 |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ->filter(function (bool $match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 33 |  |                 return $match === false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 33 |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 33 |  |             ->count() === $this->conditionVariables->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |