| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Yoanm\JsonRpcServer\Infra\Serialization; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Yoanm\JsonRpcServer\App\Serialization\ResponseNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Yoanm\JsonRpcServer\Infra\RawObject\JsonRpcRawResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class RawResponseSerializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class RawResponseSerializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const KEY_JSON_RPC = 'json-rpc'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const KEY_ID = 'id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const KEY_RESULT = 'result'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const KEY_ERROR = 'error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const SUB_KEY_ERROR_CODE = 'code'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const SUB_KEY_ERROR_MESSAGE = 'message'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const SUB_KEY_ERROR_DATA = 'data'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** @var ResponseNormalizer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $responseNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param ResponseNormalizer $responseNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 15 |  |     public function __construct(ResponseNormalizer $responseNormalizer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 15 |  |         $this->responseNormalizer = $responseNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param JsonRpcRawResponse $rawReponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 7 |  |     public function serialize(JsonRpcRawResponse $rawReponse) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 7 |  |         return $this->encode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 7 |  |             $this->normalize($rawReponse) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param mixed $normalizedContent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 8 |  |     public function encode($normalizedContent) : string | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 8 |  |         return json_encode($normalizedContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param JsonRpcRawResponse $rawResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 14 |  |     public function normalize(JsonRpcRawResponse $rawResponse) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 14 |  |         if ($rawResponse->isBatch()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |             return $this->normalizeBatchResponse($rawResponse->getResponseList()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 10 |  |             $responseList = $rawResponse->getResponseList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 10 |  |             return $this->responseNormalizer->normalize(array_shift($responseList)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param JsonRpcResponse[] $responseList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |     private function normalizeBatchResponse(array $responseList) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |         $resultList = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |         foreach ($responseList as $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             // Notifications must not have a response, even if they are on error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |             if (!$response->isNotification()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |                 $resultList[] = $this->responseNormalizer->normalize($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         // if no result, it means It was a batch call with only notifications => return null response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |         return count($resultList) > 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  |             ? $resultList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 4 |  |             : null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  |