| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Yoanm\JsonRpcServer\App\Serialization; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidRequestException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcParseErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCall; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCallResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class JsonRpcCallSerializer | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class JsonRpcCallSerializer | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** @var JsonRpcCallDenormalizer */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     private $callDenormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** @var JsonRpcCallResponseNormalizer */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     private $callResponseNormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @param JsonRpcCallDenormalizer       $callDenormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @param JsonRpcCallResponseNormalizer $callResponseNormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 85 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         JsonRpcCallDenormalizer $callDenormalizer, | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         JsonRpcCallResponseNormalizer $callResponseNormalizer | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 85 |  |         $this->callDenormalizer = $callDenormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 85 |  |         $this->callResponseNormalizer = $callResponseNormalizer; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @param string $content | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @return JsonRpcCall | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @throws JsonRpcInvalidRequestException | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @throws JsonRpcParseErrorException | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 50 |  |     public function deserialize(string $content) : JsonRpcCall | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 50 |  |         return $this->denormalize( | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 50 |  |             $this->decode($content) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 50 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param JsonRpcCallResponse $jsonRpcCallResponse | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 50 |  |     public function serialize(JsonRpcCallResponse $jsonRpcCallResponse) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 50 |  |         return $this->encode( | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 | 50 |  |             $this->normalize($jsonRpcCallResponse) | 
            
                                                                        
                                                                
            
                                    
            
            
                | 56 | 50 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param mixed $normalizedContent Could be an array or null for instance | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 51 |  |     public function encode($normalizedContent) : string | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 51 |  |         return json_encode($normalizedContent); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @param string $requestContent | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * @return array Decoded content | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * @throws JsonRpcParseErrorException | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @throws JsonRpcInvalidRequestException | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 63 |  |     public function decode(string $requestContent) : array | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 63 |  |         $decodedContent = \json_decode($requestContent, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         // Check if parsing is ok => Parse error | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 63 |  |         if (\JSON_ERROR_NONE !== \json_last_error()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 7 |  |             throw new JsonRpcParseErrorException($requestContent, \json_last_error(), json_last_error_msg()); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         // Content must be either an array (normal request) or an array of array (batch request) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         //  => so must be an array | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         // In case it's a batch call, at least one sub request must exist | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         // and in case not, some required properties must exist | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         // => array must have at least one child | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 56 |  |         if (!is_array($decodedContent) || count($decodedContent) === 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 10 |  |             throw new JsonRpcInvalidRequestException($requestContent); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 46 |  |         return $decodedContent; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @param array $decodedContent | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @return JsonRpcCall | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 46 |  |     public function denormalize(array $decodedContent) : JsonRpcCall | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 46 |  |         return $this->callDenormalizer->denormalize($decodedContent); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * @param JsonRpcCallResponse $jsonRpcCallResponse | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * @return array|null | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 57 |  |     public function normalize(JsonRpcCallResponse $jsonRpcCallResponse) : ?array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 | 57 |  |         return $this->callResponseNormalizer->normalize($jsonRpcCallResponse); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 119 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 120 |  |  |  |