Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Strong typing for the view layer #881

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading