| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Symplify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Symplify\PHP7_CodeSniffer\Sniff\Xml\Extractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SimpleXMLElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | final class SniffPropertyValuesExtractor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var bool[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $stringToBoolMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         'true' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'TRUE' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'false' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'FALSE' => false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 9 |  |     public function extractFromRuleXmlElement(SimpleXMLElement $ruleXmlElement) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 9 |  |         if (!isset($ruleXmlElement->properties)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 8 |  |         $propertyValues = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 8 |  |         foreach ($ruleXmlElement->properties->property as $propertyXmlElement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 8 |  |             $name = (string) $propertyXmlElement['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 8 |  |             $value = $this->normalizeValue((string) $propertyXmlElement['value'], $propertyXmlElement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 8 |  |             $propertyValues[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 8 |  |         return $propertyValues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 8 |  |     private function normalizeValue(string $value, SimpleXMLElement $propertyXmlElement) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 8 |  |         $value = trim($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 8 |  |         if (is_numeric($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |             return (int) $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 7 |  |         if ($this->isArrayValue($propertyXmlElement)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |             return $this->normalizeArrayValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 6 |  |         return $this->normalizeBoolValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 7 |  |     private function isArrayValue(SimpleXMLElement $property) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 7 |  |         return isset($property['type']) === true && (string) $property['type'] === 'array'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 6 |  |     private function normalizeBoolValue(string $value) | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 6 |  |         if (isset($this->stringToBoolMap[$value])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 4 |  |             return $this->stringToBoolMap[$value]; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 6 |  |         return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 5 |  |     private function normalizeArrayValue(string $value) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 5 |  |         $values = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 5 |  |         foreach (explode(',', $value) as $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 5 |  |             $v = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 5 |  |             list($key, $v) = explode('=>', $val . '=>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 5 |  |             if ($v !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |                 $values[$key] = $v; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 5 |  |                 $values[] = $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 5 |  |         return $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |