| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Author: Nil Portugués Calderó <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Date: 8/18/15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Time: 11:19 PM. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace NilPortugues\Api\JsonApi\Server\Actions\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use NilPortugues\Api\JsonApi\Http\PaginatedResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use NilPortugues\Api\JsonApi\Http\Response\BadRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use NilPortugues\Api\JsonApi\Http\Response\ResourceConflicted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use NilPortugues\Api\JsonApi\Http\Response\ResourceCreated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use NilPortugues\Api\JsonApi\Http\Response\ResourceDeleted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use NilPortugues\Api\JsonApi\Http\Response\ResourceNotFound; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use NilPortugues\Api\JsonApi\Http\Response\ResourceProcessing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use NilPortugues\Api\JsonApi\Http\Response\ResourceUpdated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use NilPortugues\Api\JsonApi\Http\Response\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use NilPortugues\Api\JsonApi\Http\Response\UnprocessableEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use NilPortugues\Api\JsonApi\Http\Response\UnsupportedAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | trait ResponseTrait | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param ErrorBag $errorBag | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function errorResponse(ErrorBag $errorBag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new BadRequest($errorBag))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @param ResponseInterface $response | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @return ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected function addHeaders(ResponseInterface $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $response; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $json | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected function resourceCreated($json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new ResourceCreated($json))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     protected function resourceDeleted() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new ResourceDeleted())); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @param ErrorBag $errorBag | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     protected function resourceNotFound(ErrorBag $errorBag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new ResourceNotFound($errorBag))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @param ErrorBag $errorBag | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     protected function resourceConflicted(ErrorBag $errorBag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new ResourceConflicted($errorBag))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @param string $json | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     protected function resourceProcessing($json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new ResourceProcessing($json))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * @param string $json | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected function resourceUpdated($json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new ResourceUpdated($json))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * @param string|PaginatedResource $json | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     protected function response($json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         if ($json instanceof PaginatedResource) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $json = json_encode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new Response($json))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      * @param ErrorBag $errorBag | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     protected function unsupportedAction(ErrorBag $errorBag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new UnsupportedAction($errorBag))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * @param ErrorBag $errorBag | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     protected function unprocessableEntity(ErrorBag $errorBag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return (new HttpFoundationFactory())->createResponse($this->addHeaders(new UnprocessableEntity($errorBag))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |  |