| 1 |  |  | <?php namespace Neomerx\Limoncello\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright 2015 [email protected] (www.neomerx.com) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use \Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use \Neomerx\JsonApi\Factories\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use \Neomerx\JsonApi\Codec\CodecMatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use \Neomerx\JsonApi\Responses\Responses; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use \Neomerx\JsonApi\Decoders\ArrayDecoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use \Neomerx\Limoncello\Config\Config as C; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use \Neomerx\JsonApi\Encoder\EncoderOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use \Neomerx\Limoncello\Errors\ExceptionThrower; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use \Neomerx\Limoncello\Contracts\IntegrationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use \Neomerx\JsonApi\Contracts\Schema\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use \Neomerx\JsonApi\Contracts\Factories\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use \Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use \Neomerx\JsonApi\Contracts\Responses\ResponsesInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use \Neomerx\JsonApi\Contracts\Parameters\Headers\MediaTypeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use \Neomerx\JsonApi\Contracts\Integration\ExceptionThrowerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @package Neomerx\Limoncello | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | trait AppServiceProviderTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param IntegrationInterface $integration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |     public function registerResponses(IntegrationInterface $integration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         $integration->setInContainer(ResponsesInterface::class, new Responses($integration)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param IntegrationInterface $integration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |     public function registerExceptionThrower(IntegrationInterface $integration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         $integration->setInContainer(ExceptionThrowerInterface::class, new ExceptionThrower()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param IntegrationInterface $integration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |     public function registerCodecMatcher(IntegrationInterface $integration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         // register factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         $factory = $this->createFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         $integration->setInContainer(FactoryInterface::class, $factory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         // register config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         $config  = $integration->getConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         $integration->setInContainer(C::class, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // register schemas | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         $schemaContainer = $this->createSchemaContainer($config, $factory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         $integration->setInContainer(ContainerInterface::class, $schemaContainer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // register codec matcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         $codecMatcher = $this->createCodecMatcher($config, $factory, $schemaContainer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         $integration->setInContainer(CodecMatcherInterface::class, $codecMatcher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return FactoryInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |     protected function createFactory() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |         return new Factory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param array            $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param FactoryInterface $factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return ContainerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |     protected function createSchemaContainer(array $config, FactoryInterface $factory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |         $schemas         = isset($config[C::SCHEMAS]) === true ? $config[C::SCHEMAS] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 2 |  |         $schemaContainer = $factory->createContainer($schemas); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 2 |  |         return $schemaContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param array              $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param FactoryInterface   $factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param ContainerInterface $schemaContainer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return CodecMatcherInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |     protected function createCodecMatcher(array $config, FactoryInterface $factory, ContainerInterface $schemaContainer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |         $options         = $this->getValue($config, C::JSON, C::JSON_OPTIONS, C::JSON_OPTIONS_DEFAULT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         $depth           = $this->getValue($config, C::JSON, C::JSON_DEPTH, C::JSON_DEPTH_DEFAULT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         $urlPrefix       = $this->getValue($config, C::JSON, C::JSON_URL_PREFIX, null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         $encoderOptions  = new EncoderOptions($options, $urlPrefix, $depth); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         $decoderClosure  = $this->getDecoderClosure(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |         $encoderClosure  = $this->getEncoderClosure($factory, $schemaContainer, $encoderOptions, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         $codecMatcher    = new CodecMatcher(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |         $jsonApiType     = $factory->createMediaType( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |             MediaTypeInterface::JSON_API_TYPE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             MediaTypeInterface::JSON_API_SUB_TYPE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |         $jsonApiTypeUtf8 = $factory->createMediaType( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |             MediaTypeInterface::JSON_API_TYPE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |             MediaTypeInterface::JSON_API_SUB_TYPE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |             ['charset' => 'UTF-8'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |         $codecMatcher->registerEncoder($jsonApiType, $encoderClosure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |         $codecMatcher->registerDecoder($jsonApiType, $decoderClosure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |         $codecMatcher->registerEncoder($jsonApiTypeUtf8, $encoderClosure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |         $codecMatcher->registerDecoder($jsonApiTypeUtf8, $decoderClosure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |         return $codecMatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @return Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 2 |  |     protected function getDecoderClosure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 1 |  |             return new ArrayDecoder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 2 |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param FactoryInterface   $factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param ContainerInterface $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param EncoderOptions     $encoderOptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param array              $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @return Closure | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |     private function getEncoderClosure( | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         FactoryInterface $factory, | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         ContainerInterface $container, | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         EncoderOptions $encoderOptions, | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         array $config | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 2 |  |         return function () use ($factory, $container, $encoderOptions, $config) { | 
            
                                                                        
                            
            
                                    
            
            
                | 158 | 1 |  |             $isShowVer   = $this->getValue($config, C::JSON, C::JSON_IS_SHOW_VERSION, C::JSON_IS_SHOW_VERSION_DEFAULT); | 
            
                                                                        
                            
            
                                    
            
            
                | 159 | 1 |  |             $versionMeta = $this->getValue($config, C::JSON, C::JSON_VERSION_META, null); | 
            
                                                                        
                            
            
                                    
            
            
                | 160 | 1 |  |             $encoder     = $factory->createEncoder($container, $encoderOptions); | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 162 | 1 |  |             $isShowVer === false ?: $encoder->withJsonApiVersion($versionMeta); | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 164 | 1 |  |             return $encoder; | 
            
                                                                        
                            
            
                                    
            
            
                | 165 | 2 |  |         }; | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @param array  $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param string $key1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param string $key2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @param mixed  $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 2 |  |     private function getValue(array $array, $key1, $key2, $default) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 2 |  |         return isset($array[$key1][$key2]) === true ? $array[$key1][$key2] : $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |  | 
            
                        
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.