| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Yoanm\JsonRpcServer\App\Serialization; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcExceptionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class JsonRpcResponseNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class JsonRpcResponseNormalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const KEY_JSON_RPC = 'jsonrpc'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const KEY_ID = 'id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const KEY_RESULT = 'result'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const KEY_ERROR = 'error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const SUB_KEY_ERROR_CODE = 'code'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const SUB_KEY_ERROR_MESSAGE = 'message'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const SUB_KEY_ERROR_DATA = 'data'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var JsonRpcResponseErrorNormalizer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $responseErrorNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 52 |  |     public function __construct(?JsonRpcResponseErrorNormalizer $responseErrorNormalizer = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 52 |  |         $this->responseErrorNormalizer = $responseErrorNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param JsonRpcResponse $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return array|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 48 |  |     public function normalize(JsonRpcResponse $response) : ?array | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         // Notifications must not have a response, even if they are on error | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 48 |  |         if ($response->isNotification()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 2 |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 46 |  |         $data = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 46 |  |             self::KEY_JSON_RPC => $response->getJsonRpc(), | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 46 |  |             self::KEY_ID => $response->getId() | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 46 |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 46 |  |         if ($response->getError()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 38 |  |             $data[self::KEY_ERROR] = $this->normalizeError( | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 38 |  |                 $response->getError() | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 38 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 9 |  |             $data[self::KEY_RESULT] = $response->getResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 46 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param JsonRpcExceptionInterface $error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 38 |  |     private function normalizeError(JsonRpcExceptionInterface $error) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 38 |  |         $normalizedError = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 38 |  |             self::SUB_KEY_ERROR_CODE => $error->getErrorCode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 38 |  |             self::SUB_KEY_ERROR_MESSAGE => $error->getErrorMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 38 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 38 |  |         $errorData = $error->getErrorData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 38 |  |         if (null !== $this->responseErrorNormalizer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |             $errorData += $this->responseErrorNormalizer->normalize($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 38 |  |         if (!empty($errorData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 5 |  |             $normalizedError[self::SUB_KEY_ERROR_DATA] = $errorData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 38 |  |         return $normalizedError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  |