| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Class for exclusion strategies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Graviton\RestBundle\ExclusionStrategy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use JMS\Serializer\Exclusion\ExclusionStrategyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use JMS\Serializer\Metadata\ClassMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use JMS\Serializer\Metadata\PropertyMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use JMS\Serializer\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * In this Strategy we skip all properties on first level who are not selected if there is a select in rql. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @license  http://opensource.org/licenses/gpl-license.php GNU Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @link     http://swisscom.ch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class SelectExclusionStrategy implements ExclusionStrategyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @InheritDoc: Whether the class should be skipped. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param ClassMetadata $metadata         blabla | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param Context       $navigatorContext blabla | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @InheritDoc: Whether the property should be skipped. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Skipping properties on first level who are not selected if there is a select in rql. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param PropertyMetadata $property blabla | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param Context          $context  blabla | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return boolean | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |     public function shouldSkipProperty(PropertyMetadata $property, Context $context) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         // we are only dealing with the first level of the JSON here | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         if ($context->getDepth() > 1) { | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |             return false; | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         $select = false; | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         $selectVars=[]; | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         foreach ($_GET as $getVar => $val) { | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |             if (strstr($getVar, 'select(')) { | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |                 preg_match('/\((.*?)\)/', $getVar, $match); | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                 $select = true; | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |                 $selectVars = explode(',', $match[1]); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         if ($select && ! in_array($property->name, $selectVars)) { | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |             return true; | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             return false; | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                        
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: