| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Groundskeeper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 16 |  |     public function __construct(array $options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 16 |  |         $resolver = new OptionsResolver(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 16 |  |         $this->configureOptions($resolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 16 |  |         $this->options = $resolver->resolve($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 11 |  |         $this->setDependentOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 11 |  |     public function has($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 11 |  |         return array_key_exists($key, $this->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 11 |  |     public function get($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 11 |  |         if (!$this->has($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             throw new \InvalidArgumentException('Invalid configuration key: ' . $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 11 |  |         return $this->options[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 10 |  |     public function isAllowedType($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 10 |  |         $disallowedTypeArray = explode(',', $this->options['remove-types']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 10 |  |         foreach ($disallowedTypeArray as $disallowedType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 10 |  |             if (strtolower(trim($disallowedType)) == $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 10 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 10 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 16 |  |     protected function configureOptions(OptionsResolver $resolver) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         // Set default options. | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 16 |  |         $resolver->setDefaults(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 16 |  |             'indent-spaces' => 4, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 16 |  |             'output' => 'compact', | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 16 |  |             'remove-types' => 'none', | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 16 |  |             'strategy' => 'standard', | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             'throw-on-error' => false | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 16 |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         // Validation | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         // indent-spaces | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 16 |  |         $resolver->setAllowedTypes('indent-spaces', 'int'); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $resolver->setAllowedValues('indent-spaces', function ($value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 15 |  |                 return $value >= 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 16 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         // output | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 16 |  |         $resolver->setAllowedTypes('output', 'string'); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 16 |  |         $resolver->setAllowedValues('output', array('compact', 'pretty')); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         // remove-types | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 16 |  |         $resolver->setAllowedTypes('remove-types', 'string'); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 12 |  |         $resolver->setAllowedValues('remove-types', function ($value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 12 |  |                 if ($value == 'none') { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 12 |  |                     return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 $acceptedValues = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                     'cdata', | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                     'comment', | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                     'doctype', | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                     'element', | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                     'text' | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 $valueArray = explode(',', $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 foreach ($valueArray as $val) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                     if (array_search(trim(strtolower($val)), $acceptedValues) === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 16 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         // strategy | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 16 |  |         $resolver->setAllowedTypes('strategy', 'string'); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 16 |  |         $resolver->setAllowedValues('strategy', array('none', 'standard')); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         // throw-on-error | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 16 |  |         $resolver->setAllowedTypes('throw-on-error', 'bool'); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 16 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 11 |  |     protected function setDependentOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 11 |  |         if ($this->options['output'] == 'compact') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 11 |  |             $this->options['indent-spaces'] = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 11 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 11 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |