| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Cubiche package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (c) Cubiche | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Cubiche\Infrastructure\Geolocation\Doctrine\ODM\MongoDB\Types; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Cubiche\Domain\Geolocation\Coordinate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Cubiche\Infrastructure\Model\Doctrine\ODM\MongoDB\Types\ValueObjectType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Coordinate Type Class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Karel Osorio Ramírez <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class CoordinateType extends ValueObjectType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function convertToDatabaseValue($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         if ($value === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         if (!$value instanceof Coordinate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             throw new \InvalidArgumentException(\sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 'Expected %s instance, instance of %s given', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 Coordinate::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 \is_object($value) ? \gettype($value) : \get_class($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'type' => 'Point', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'coordinates' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 $value->longitude()->toNative(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $value->latitude()->toNative(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function convertToPHPValue($value) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         if ($value === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         return Coordinate::fromLatLng($value['coordinates'][1], $value['coordinates'][0]); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function closureToMongo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         '$return = $value === null ? null : array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             "type" => "Point", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             "coordinates" => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $value->longitude()->toNative(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $value->latitude()->toNative() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ));'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function closureToPHP() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         '$return = $value === null ? null :'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             Coordinate::class.'::fromLatLng( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $value["coordinates"][1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $value["coordinates"][0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         );'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  |