Skip to content

Commit

Permalink
Merge pull request #881 from creative-commoners/pulls/8/strong-typing
Browse files Browse the repository at this point in the history
API Strong typing for the view layer
  • Loading branch information
emteknetnz authored Aug 27, 2024
2 parents 0f14408 + 15f2c03 commit 33a49b8
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions src/Model/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use IntlDateFormatter;
use InvalidArgumentException;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\View\ViewableData;

/**
* Stores dates in the same timezone as DBDateTime, but will format them
Expand All @@ -18,17 +19,14 @@
*/
class LocalDateTime extends DBDatetime
{
protected $timezone = null;
protected ?string $timezone = null;

public function __construct($name = null, $options = [], $timezone = null)
public function __construct(?string $name = null, array $options = [], ?string $timezone = null)
{
$this->setTimezone($timezone);
parent::__construct($name, $options);
}

/**
* @return string|null
*/
public function getTimezone(): ?string
{
return $this->timezone;
Expand All @@ -50,11 +48,11 @@ public function setTimezone(?string $timezone): LocalDateTime
}

public function getCustomFormatter(
$locale = null,
$pattern = null,
$dateLength = IntlDateFormatter::MEDIUM,
$timeLength = IntlDateFormatter::MEDIUM
) {
?string $locale = null,
?string $pattern = null,
int $dateLength = IntlDateFormatter::MEDIUM,
int $timeLength = IntlDateFormatter::MEDIUM
): IntlDateFormatter {
$formatter = parent::getCustomFormatter($locale, $pattern, $dateLength, $timeLength);
$timezone = $this->getTimezone();
if ($timezone) {
Expand All @@ -65,13 +63,8 @@ public function getCustomFormatter(

/**
* Assign value in server timezone
*
* @param mixed $value
* @param string $record
* @param bool $markChanged
* @return $this
*/
public function setValue($value, $record = null, $markChanged = true)
public function setValue(mixed $value, null|array|ViewableData $record = null, bool $markChanged = true): static
{
// Disable timezone when setting value (always stored in server timezone)
$timezone = $this->getTimezone();
Expand All @@ -85,8 +78,6 @@ public function setValue($value, $record = null, $markChanged = true)

/**
* Get ISO local value
*
* @return string
*/
public function getLocalValue(): string
{
Expand All @@ -95,12 +86,10 @@ public function getLocalValue(): string

/** Assign a value that's already in the current locale
*
* @param string $value
* @param string $timezone Timezone to assign to this date (defaults to current assigned timezone)
* @return $this
* @param string|null $timezone Timezone to assign to this date (defaults to current assigned timezone)
* @throws Exception
*/
public function setLocalValue($value, $timezone = null)
public function setLocalValue(string $value, ?string $timezone = null): static
{
// If assigning timezone, set first
if (func_num_args() >= 2) {
Expand Down

0 comments on commit 33a49b8

Please sign in to comment.