Skip to content

Commit

Permalink
Merge pull request #42 from acelaya-forks/feature/device-long-urls
Browse files Browse the repository at this point in the history
Feature/device long urls
  • Loading branch information
acelaya authored Mar 23, 2023
2 parents 4288294 + 30ceeaa commit 59c348e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
10 changes: 10 additions & 0 deletions src/ShortUrls/Model/Device.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Shlinkio\Shlink\SDK\ShortUrls\Model;

enum Device: string
{
case ANDROID = 'android';
case IOS = 'ios';
case DESKTOP = 'desktop';
}
24 changes: 24 additions & 0 deletions src/ShortUrls/Model/DeviceLongUrls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Shlinkio\Shlink\SDK\ShortUrls\Model;

final class DeviceLongUrls
{
public function __construct(
public readonly ?string $android,
public readonly ?string $ios,
public readonly ?string $desktop,
) {
}

public static function fromArray(array $payload): self
{
return new self(
$payload['android'] ?? null,
$payload['ios'] ?? null,
$payload['desktop'] ?? null,
);
}
}
2 changes: 2 additions & 0 deletions src/ShortUrls/Model/ShortUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private function __construct(
public readonly array $tags,
public readonly ShortUrlMeta $meta,
public readonly VisitsCount $visitsSummary,
public readonly DeviceLongUrls $deviceLongUrls,
) {
$this->visitsCount = $visitsCount;
}
Expand All @@ -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'] ?? []),
);
}
}
8 changes: 8 additions & 0 deletions src/ShortUrls/Model/ShortUrlEdition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/ShortUrls/Model/ShortUrlPayloadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
25 changes: 25 additions & 0 deletions test/ShortUrls/Model/ShortUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,7 @@ public function properObjectIsCreatedFromArray(
array $expectedTags,
ShortUrlMeta $expectedMeta,
VisitsCount $expectedVisitsSummary,
DeviceLongUrls $expectedDeviceLongUrls,
): void {
$shortUrl = ShortUrl::fromArray($payload);

Expand All @@ -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
Expand All @@ -66,6 +69,7 @@ public static function providePayloads(): iterable
[],
ShortUrlMeta::fromArray([]),
VisitsCount::fromArrayWithFallback([], 0),
DeviceLongUrls::fromArray([]),
];
yield 'all values' => [
[
Expand Down Expand Up @@ -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],
Expand All @@ -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),
];
}
}

0 comments on commit 59c348e

Please sign in to comment.