diff --git a/CHANGELOG.md b/CHANGELOG.md index d22df0a..d5ec65e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). -## [Unreleased] +## [1.2.0] - 2023-03-23 ### Added * [#38](https://github.com/shlinkio/shlink-php-sdk/issues/38) Add support for bot and non-bot visits in summary. * [#39](https://github.com/shlinkio/shlink-php-sdk/issues/39) Add support for bot and non-bot visits in tags with stats. +* [#37](https://github.com/shlinkio/shlink-php-sdk/issues/37) Add support for device-specific long URLs. ### Changed * Replace references to `doma.in` by `s.test`. diff --git a/src/ShortUrls/Model/Device.php b/src/ShortUrls/Model/Device.php new file mode 100644 index 0000000..0d81578 --- /dev/null +++ b/src/ShortUrls/Model/Device.php @@ -0,0 +1,10 @@ +visitsCount = $visitsCount; } @@ -48,6 +49,7 @@ public static function fromArray(array $payload): self tags: $payload['tags'] ?? [], meta: ShortUrlMeta::fromArray($payload['meta'] ?? []), visitsSummary: VisitsCount::fromArrayWithFallback($payload['visitsSummary'] ?? [], $visitsCount), + deviceLongUrls: DeviceLongUrls::fromArray($payload['deviceLongUrls'] ?? []), ); } } diff --git a/src/ShortUrls/Model/ShortUrlEdition.php b/src/ShortUrls/Model/ShortUrlEdition.php index ef6aee6..f74c0fd 100644 --- a/src/ShortUrls/Model/ShortUrlEdition.php +++ b/src/ShortUrls/Model/ShortUrlEdition.php @@ -20,6 +20,14 @@ public function withLongUrl(string $longUrl): self return $this->cloneWithProp('longUrl', $longUrl); } + public function removingDeviceLongUrl(Device $device): self + { + $deviceLongUrls = $this->payload['deviceLongUrls'] ?? []; + $deviceLongUrls[$device->value] = null; + + return $this->cloneWithProp('deviceLongUrls', $deviceLongUrls); + } + public function removingValidSince(): self { return $this->cloneWithProp('validSince', null); diff --git a/src/ShortUrls/Model/ShortUrlPayloadTrait.php b/src/ShortUrls/Model/ShortUrlPayloadTrait.php index 48c20ca..9882d50 100644 --- a/src/ShortUrls/Model/ShortUrlPayloadTrait.php +++ b/src/ShortUrls/Model/ShortUrlPayloadTrait.php @@ -12,6 +12,22 @@ private function __construct(private array $payload = []) { } + public function withDeviceLongUrl(Device $device, string $longUrl): self + { + $deviceLongUrls = $this->payload['deviceLongUrls'] ?? []; + $deviceLongUrls[$device->value] = $longUrl; + + return $this->cloneWithProp('deviceLongUrls', $deviceLongUrls); + } + + public function withoutDeviceLongUrl(Device $device): self + { + $deviceLongUrls = $this->payload['deviceLongUrls'] ?? []; + unset($deviceLongUrls[$device->value]); + + return $this->cloneWithProp('deviceLongUrls', $deviceLongUrls); + } + public function validSince(DateTimeInterface $validSince): self { return $this->cloneWithProp('validSince', $validSince->format(DateTimeInterface::ATOM)); diff --git a/test/ShortUrls/Model/ShortUrlTest.php b/test/ShortUrls/Model/ShortUrlTest.php index 8db6cb4..37eda14 100644 --- a/test/ShortUrls/Model/ShortUrlTest.php +++ b/test/ShortUrls/Model/ShortUrlTest.php @@ -9,6 +9,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use Shlinkio\Shlink\SDK\ShortUrls\Model\DeviceLongUrls; use Shlinkio\Shlink\SDK\ShortUrls\Model\ShortUrl; use Shlinkio\Shlink\SDK\ShortUrls\Model\ShortUrlMeta; use Shlinkio\Shlink\SDK\Visits\Model\VisitsCount; @@ -30,6 +31,7 @@ public function properObjectIsCreatedFromArray( array $expectedTags, ShortUrlMeta $expectedMeta, VisitsCount $expectedVisitsSummary, + DeviceLongUrls $expectedDeviceLongUrls, ): void { $shortUrl = ShortUrl::fromArray($payload); @@ -45,6 +47,7 @@ public function properObjectIsCreatedFromArray( self::assertEquals($expectedTags, $shortUrl->tags); self::assertEquals($expectedMeta, $shortUrl->meta); self::assertEquals($expectedVisitsSummary, $shortUrl->visitsSummary); + self::assertEquals($expectedDeviceLongUrls, $shortUrl->deviceLongUrls); } public static function providePayloads(): iterable @@ -66,6 +69,7 @@ public static function providePayloads(): iterable [], ShortUrlMeta::fromArray([]), VisitsCount::fromArrayWithFallback([], 0), + DeviceLongUrls::fromArray([]), ]; yield 'all values' => [ [ @@ -100,6 +104,7 @@ public static function providePayloads(): iterable ['foo', 'bar'], ShortUrlMeta::fromArray($meta), VisitsCount::fromArrayWithFallback($visitsSummary, 5), + DeviceLongUrls::fromArray([]), ]; yield 'visits total fallback' => [ ['dateCreated' => $formattedDate, 'visitsCount' => 35], @@ -115,6 +120,26 @@ public static function providePayloads(): iterable [], ShortUrlMeta::fromArray([]), VisitsCount::fromArrayWithFallback([], 35), + DeviceLongUrls::fromArray([]), + ]; + yield 'device long URLs' => [ + ['dateCreated' => $formattedDate, 'deviceLongUrls' => $rawDeviceLongUrls = [ + 'android' => 'foo', + 'desktop' => 'bar', + ]], + '', + '', + '', + $now, + 0, + null, + null, + false, + false, + [], + ShortUrlMeta::fromArray([]), + VisitsCount::fromArrayWithFallback([], 0), + DeviceLongUrls::fromArray($rawDeviceLongUrls), ]; } }