| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Shlinkio\Shlink\Core\Transformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Shlinkio\Shlink\Core\Entity\ShortUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Shlinkio\Shlink\Core\Util\ShortUrlBuilderTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function Functional\invoke; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function Functional\invoke_if; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class ShortUrlDataTransformer implements DataTransformerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     use ShortUrlBuilderTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $domainConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 17 |  |     public function __construct(array $domainConfig) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 17 |  |         $this->domainConfig = $domainConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param ShortUrl $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 10 |  |     public function transform($value): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 10 |  |         $longUrl = $value->getLongUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 10 |  |         $shortCode = $value->getShortCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 10 |  |             'shortCode' => $shortCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 10 |  |             'shortUrl' => $this->buildShortUrl($this->domainConfig, $shortCode), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 10 |  |             'longUrl' => $longUrl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 10 |  |             'dateCreated' => $value->getDateCreated()->toAtomString(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 10 |  |             'visitsCount' => $value->getVisitsCount(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 10 |  |             'tags' => invoke($value->getTags(), '__toString'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 10 |  |             'meta' => $this->buildMeta($value), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             // Deprecated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 10 |  |             'originalUrl' => $longUrl, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 10 |  |     private function buildMeta(ShortUrl $shortUrl): array | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 10 |  |         $validSince = $shortUrl->getValidSince(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 10 |  |         $validUntil = $shortUrl->getValidUntil(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 10 |  |         $maxVisits = $shortUrl->getMaxVisits(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 10 |  |             'validSince' => invoke_if($validSince, 'toAtomString'), | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 10 |  |             'validUntil' => invoke_if($validUntil, 'toAtomString'), | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 10 |  |             'maxVisits' => $maxVisits, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  |