| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of Goodwix Doctrine JSON ODM. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Goodwix\DoctrineJsonOdm\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Doctrine\DBAL\Platforms\AbstractPlatform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Doctrine\DBAL\Types\JsonType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Goodwix\DoctrineJsonOdm\Exception\JsonOdmException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Serializer\Exception\ExceptionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Serializer\SerializerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | abstract class AbstractODMType extends JsonType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public $deserializationContext = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var SerializerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $format = 'json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $entityClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $serializationContext = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 20 |  |     public function getSerializer(): SerializerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 20 |  |         if (null === $this->serializer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |             throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |                     'An instance of "%s" must be available. Call the "setSerializer" method.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |                     SerializerInterface::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 19 |  |         return $this->serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 10 |  |     public function setSerializer(SerializerInterface $serializer): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 10 |  |         $this->serializer = $serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |     public function getFormat(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |         return $this->format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |     public function setFormat(string $format): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $this->format = $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 22 |  |     public function getEntityClass(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 22 |  |         if (null === $this->entityClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |                 'ODM entity class name must be available. Call the "setEntityClass" method.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 21 |  |         return $this->entityClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 9 |  |     public function setEntityClass(string $entityClass): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 9 |  |         $this->entityClass = $entityClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 16 |  |     public function getSerializationContext(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 16 |  |         return $this->serializationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |     public function setSerializationContext(array $serializationContext): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  |         $this->serializationContext = $serializationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 10 |  |     public function getDeserializationContext(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 10 |  |         return $this->deserializationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |     public function setDeserializationContext(array $deserializationContext): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |         $this->deserializationContext = $deserializationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 15 |  |     public function getName(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 15 |  |         return $this->getEntityClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @throws JsonOdmException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 17 |  |     public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 17 |  |         if (null === $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 14 |  |             $context = $this->getSerializationContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 14 |  |             $value   = $this->getSerializer()->serialize($value, $this->format, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         } catch (ExceptionInterface $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |             $message = sprintf('Serialization exception occurred for class "%s".', $this->getEntityClass()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |             throw new JsonOdmException($message, 0, $exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 13 |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @throws JsonOdmException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 12 |  |     public function convertToPHPValue($value, AbstractPlatform $platform) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 12 |  |         if (null === $value || '' === $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 4 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 8 |  |             $context = $this->getDeserializationContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 8 |  |             $value   = $this->getSerializer()->deserialize($value, $this->getEntityClass(), $this->format, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |         } catch (ExceptionInterface $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  |             $message = sprintf('Deserialization exception occurred for class "%s".', $this->getEntityClass()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 1 |  |             throw new JsonOdmException($message, 0, $exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 7 |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     abstract public static function registerODMType(string $entityClass, SerializerInterface $serializer): void; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |