Created factory method to build VisitParams from a raw dataset

This commit is contained in:
Alejandro Celaya
2018-11-28 19:58:45 +01:00
parent 45254606d4
commit b0f250ed8a
2 changed files with 15 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Model;
use Cake\Chronos\Chronos;
use Shlinkio\Shlink\Common\Util\DateRange;
final class VisitsParams
@@ -21,6 +22,19 @@ final class VisitsParams
$this->itemsPerPage = $itemsPerPage;
}
public static function fromRawData(array $query): self
{
$startDate = self::getDateQueryParam($query, 'startDate');
$endDate = self::getDateQueryParam($query, 'endDate');
return new self(new DateRange($startDate, $endDate), $query['page'] ?? 1, $query['itemsPerPage'] ?? null);
}
private static function getDateQueryParam(array $query, string $key): ?Chronos
{
return ! isset($query[$key]) || empty($query[$key]) ? null : Chronos::parse($query[$key]);
}
public function getDateRange(): DateRange
{
return $this->dateRange;