| 1 |  |  | <?php namespace Pz\Doctrine\Rest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  | class RestRequest extends Request | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |      * Json API type. | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     const JSON_API_CONTENT_TYPE = 'application/vnd.api+json'; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * Default limit for list. | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     const DEFAULT_LIMIT = 1000; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * Authorize rest request. | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * Entity will be object for get,update,delete actions. | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * Entity will be string for index,create action. | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @param object|string $entity | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 10 |  |     public function authorize($entity) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 10 |  |         return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 10 |  |     public function isAcceptJsonApi() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 | 10 |  |         return in_array(static::JSON_API_CONTENT_TYPE, $this->getAcceptableContentTypes()); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 2 |  |     public function isContentJsonApi() | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 2 |  |         return $this->headers->get('CONTENT_TYPE') === static::JSON_API_CONTENT_TYPE; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @return int|array | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 2 |  |     public function getId() | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 2 |  |         return $this->get('id'); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * Get jsonapi fieldsets. | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 10 |  |     public function getFields() | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 10 |  |         return $this->get('fields'); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return mixed|null | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 6 |  |     public function getFilter() | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 6 |  |         if ($query = $this->get('filter')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |             if (is_string($query) && (null !== ($json = json_decode($query, true)))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |                 return $json; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 1 |  |             return $query; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 5 |  |         return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @return array|null | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 6 |  |     public function getOrderBy() | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 6 |  |         if ($sort = $this->get('sort')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |             $fields = explode(',', $sort); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |             $orderBy = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 1 |  |             foreach ($fields as $field) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |                 if (empty($field)) continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  |                 $direction = 'ASC'; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |                 if ($field[0] === '-') { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |                     $field = substr($field, 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 1 |  |                     $direction = 'DESC'; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 1 |  |                 $orderBy[$field] = $direction; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 1 |  |             return $orderBy; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 5 |  |         return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @return int|null | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 6 |  |     public function getStart() | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 | 6 |  |         if (($page = $this->get('page')) && $this->getLimit() !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 114 | 2 |  |             if (isset($page['number']) && is_numeric($page['number'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 115 | 1 |  |                 return ($page['number'] - 1) * $this->getLimit(); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 | 2 |  |             return isset($page['offset']) && is_numeric($page['offset']) ? (int) $page['offset'] : 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 5 |  |         return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      * @return int|null | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 127 | 6 |  |     public function getLimit() | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 6 |  |         if ($page = $this->get('page')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 130 | 2 |  |             if (isset($page['number']) && is_numeric($page['number'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 1 |  |                 return isset($page['size']) && is_numeric($page['size']) ? | 
            
                                                                        
                            
            
                                    
            
            
                | 132 | 1 |  |                     (int) $page['size'] : $this->getDefaultLimit(); | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 135 | 2 |  |             return isset($page['limit']) && is_numeric($page['limit']) ? | 
            
                                                                        
                            
            
                                    
            
            
                | 136 | 2 |  |                 (int) $page['limit'] : $this->getDefaultLimit(); | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 5 |  |         return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * @return array|string|null | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 10 |  |     public function getInclude() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 | 10 |  |         return $this->get('include'); | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * @return array|string|null | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 10 |  |     public function getExclude() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 | 10 |  |         return $this->get('exclude'); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 159 |  |  |      * @return int | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 1 |  |     protected function getDefaultLimit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 | 1 |  |         return static::DEFAULT_LIMIT; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 166 |  |  |  | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.