| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sensorario\Resources; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     extends ContainerBase | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $resources; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $rewrites; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $allowed = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         'allowed', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         'allowedRanges', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         'allowedValues', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         'defaults', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'mandatory', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'rules', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function __construct(array $resources) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         if (!isset($resources['resources'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 'resources element is not defined' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->rewrites = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->globals  = []; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->ensureValidConstraints($resources); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         foreach ($resources['resources'] as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             if (isset($item['rewrite'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 foreach ($item['rewrite'] as $field => $rewriteRule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                     $this->rewrites[$field] = $rewriteRule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (isset($resources['globals'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->globals = $resources['globals']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->resources = $resources; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private function ensureValidConstraints($resources) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         foreach ($resources['resources'] as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             if (isset($item['constraints'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 foreach ($item['constraints'] as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     if (!in_array($name, $this->allowed)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                         throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                             'Invalid constraint: ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                             . 'name ' . $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                             . '; value ' . $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function countResources() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return count( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this->resources['resources'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     public function create($resource, array $constraints) | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         foreach ($constraints as $name => $value) { | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 78 |  | View Code Duplication |             if (!isset($this->resources['resources'][$resource]['constraints']['allowed'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 throw new RuntimeException( | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                     'Not allowed `' . $name . '` constraint with value `' . $value . '`' | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 84 |  | View Code Duplication |             if (!in_array($name, $this->resources['resources'][$resource]['constraints']['allowed'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                 throw new RuntimeException( | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |                     'Not allowed `' . $name . '` constraint with value `' . $value . '`' | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function allowed($resource) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $allowed = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         foreach ($this->allowed as $item) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 97 |  | View Code Duplication |             if (isset($this->resources['resources'][$resource]['constraints'][$item])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $allowed = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     $allowed, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     $this->resources['resources'][$resource]['constraints'][$item] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $allowed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function getConstraints( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $constraintName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 112 |  | View Code Duplication |         if (isset($this->resources['resources'][$resource]['constraints'][$constraintName])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return $this->resources['resources'][$resource]['constraints'][$constraintName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function rewrites() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return $this->rewrites; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function globals() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return $this->globals; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |