File: D:/HostingSpaces/SBogers10/momsecurity.komma.nl/vendor/komma/kms/src/Core/Attributes/DatePicker.php
<?php
namespace Komma\KMS\Core\Attributes;
use Komma\KMS\Core\Attributes\Interfaces\HasLabelInterface;
use Komma\KMS\Core\Attributes\Traits\ExplanationTrait;
use Komma\KMS\Core\Attributes\Traits\LabelTrait;
use Komma\KMS\Core\Attributes\Traits\PlaceholderTextTrait;
use Komma\KMS\Core\Attributes\Traits\ReadOnlyTrait;
use Carbon\Carbon;
use InvalidArgumentException;
/**
* Class TextField
* @package App\Kms\Core\Attributes
*/
class DatePicker extends Attribute implements HasLabelInterface
{
use LabelTrait;
use PlaceholderTextTrait;
use ReadOnlyTrait;
use ExplanationTrait;
public const ANIMATION_SLIDEDOWN = 'slideDown';
public const ANIMATION_FADEIN = 'fadeIn';
public const ANIMATION_BLIND = 'blind';
public const ANIMATION_BOUNCE = 'bounce';
public const ANIMATION_CLIP = 'clip';
public const ANIMATION_DROP = 'drop';
public const ANIMATION_FOLD = 'fold';
public const ANIMATION_SLIDE = 'slide';
public const DATE_FORMAT_EUROPEAN = 'dd/mm/yy';
public const DATE_FORMAT_AMERICAN = 'mm/dd/yy';
public const TIME_FORMAT_12 = 12;
public const TIME_FORMAT_24 = 24;
public const LANGUAGE_ENGLISH = 'en';
public const LANGUAGE_DUTCH = 'nl';
/** Whether or not you can see dates from other months in the current month and select them*/
private bool $showAndSelectOtherMonths = true;
/** The Animation to use when opening and closing the picker*/
private ?string $animation;
/** Shows the button bar or not. This is the bar below the calendar that has a today button and a done button*/
private bool $buttonBar = false;
/** Enables the user to change the month via a select menu */
private bool $changeMonth = true;
/** Enables the user to change the year via a select menu */
private bool $changeYear = true;
/** DatePicker format constant */
private string $dateFormat = DatePicker::DATE_FORMAT_EUROPEAN;
/** DatePicker format constant */
private int $timeFormat = DatePicker::TIME_FORMAT_24;
/**
* If true, shows the month names in the long form.
* If false shows the short form of month names.
*/
private bool $showLongMonthNames = true;
private string $language = DatePicker::LANGUAGE_DUTCH;
private bool $timeEnabled = true;
/**
* Returns a view that visually represents this attribute
*
* @return string
* @throws \Throwable
*/
public function render(): string
{
return view('KMS::attributes.datePicker', [
'attribute' => $this
])->render();
}
/**
* @return bool
*/
public function getShowAndSelectOtherMonths(): bool
{
return $this->showAndSelectOtherMonths;
}
/**
* @param bool $showAndSelectOtherMonths
*/
public function setShowAndSelectOtherMonths(bool $showAndSelectOtherMonths): void
{
$this->showAndSelectOtherMonths = $showAndSelectOtherMonths;
}
/**
* @param string $animationConstant
* @return DatePicker
*/
public function setAnimation(string $animationConstant): DatePicker
{
switch($animationConstant)
{
case DatePicker::ANIMATION_SLIDEDOWN:
$this->animation = DatePicker::ANIMATION_SLIDEDOWN;
break;
case DatePicker::ANIMATION_FADEIN:
$this->animation = DatePicker::ANIMATION_FADEIN;
break;
case DatePicker::ANIMATION_BLIND:
$this->animation = DatePicker::ANIMATION_BLIND;
break;
case DatePicker::ANIMATION_BOUNCE:
$this->animation = DatePicker::ANIMATION_BOUNCE;
break;
case DatePicker::ANIMATION_CLIP:
$this->animation = DatePicker::ANIMATION_CLIP;
break;
case DatePicker::ANIMATION_DROP:
$this->animation = DatePicker::ANIMATION_DROP;
break;
case DatePicker::ANIMATION_FOLD:
$this->animation = DatePicker::ANIMATION_FOLD;
break;
case DatePicker::ANIMATION_SLIDE:
$this->animation = DatePicker::ANIMATION_SLIDE;
break;
default:
$this->animation = null;
break;
}
return $this;
}
/**
* @return string
*/
public function getAnimation(): ?string
{
return $this->animation ?? '';
}
/**
* @return bool
*/
public function getButtonBar(): bool
{
return $this->buttonBar;
}
/**
* @param bool $buttonBar
*/
public function setButtonBar(bool $buttonBar): void
{
$this->buttonBar = $buttonBar;
}
/**
* @return bool
*/
public function getChangeMonth(): bool
{
return $this->changeMonth;
}
/**
* @param bool $changeMonth
*/
public function setChangeMonth(bool $changeMonth): void
{
$this->changeMonth = $changeMonth;
}
/**
* @return bool
*/
public function getShowLongMonthNames(): bool
{
return $this->showLongMonthNames;
}
/**
* @param bool $showLongMonthNames
*/
public function setShowLongMonthNames(bool $showLongMonthNames): void
{
$this->showLongMonthNames = $showLongMonthNames;
}
/**
* @return bool
*/
public function getChangeYear(): bool
{
return $this->changeYear;
}
/**
* @param bool $changeYear
*/
public function setChangeYear(bool $changeYear): void
{
$this->changeYear = $changeYear;
}
/**
* @return bool
*/
public function getTimeEnabled(): bool
{
return $this->timeEnabled;
}
/**
* @param bool $timeEnabled
* @return DatePicker
*/
public function setTimeEnabled(bool $timeEnabled): DatePicker
{
$this->timeEnabled = $timeEnabled;
return $this;
}
/**
* @param bool $changeYearAndMonth
* @return DatePicker
*/
public function setChangeYearAndMonth(bool $changeYearAndMonth): DatePicker
{
$this->changeYear = $changeYearAndMonth;
$this->changeMonth = $changeYearAndMonth;
return $this;
}
/**
* @return mixed
*/
public function getDateFormat():string
{
return $this->dateFormat;
}
/**
* @param mixed $format
* @return DatePicker
*/
public function setDateFormat($format): DatePicker
{
switch ($format)
{
case DatePicker::DATE_FORMAT_AMERICAN:
$this->dateFormat = DatePicker::DATE_FORMAT_AMERICAN;
break;
case DatePicker::DATE_FORMAT_EUROPEAN:
default:
$this->dateFormat = DatePicker::DATE_FORMAT_EUROPEAN;
break;
}
return $this;
}
/**
* @return mixed
*/
public function getTimeFormat():int
{
return $this->timeFormat;
}
/**
* @param mixed $format
* @return DatePicker
*/
public function setTimeFormat($format): DatePicker
{
switch ($format)
{
case DatePicker::TIME_FORMAT_12:
throw new \RuntimeException('Not implemented');
$this->timeFormat = DatePicker::TIME_FORMAT_12;
break;
case DatePicker::TIME_FORMAT_24:
default:
$this->timeFormat = DatePicker::TIME_FORMAT_24;
break;
}
return $this;
}
/**
* @return string
*/
public function getLanguage():string
{
return $this->language;
}
/**
* @param string $language
* @return DatePicker
*/
public function setLanguage($language): DatePicker
{
switch ($language)
{
case DatePicker::LANGUAGE_ENGLISH:
$this->language = DatePicker::LANGUAGE_ENGLISH;
break;
case DatePicker::LANGUAGE_DUTCH:
$this->language = DatePicker::LANGUAGE_DUTCH;
break;
}
return $this;
}
/**
* @param string $value The value associated with the attribute. Specified in this format:
* 2018-02-23 00:00:00 (Y-m-d H:i:s) or this format: {"year":2018,"month":4,"day":14,"hour":18,"minute":2,"second":0}.
* Internally always stores it as as the json format
* @throws InvalidArgumentException
* @return DatePicker
*/
public function setValue(string $value): DatePicker
{
$decodedData = json_decode($value, true);
if($decodedData === null) {
//Value is a database date time string like this: 2018-02-23 00:00:00
$dateTime = Carbon::createFromFormat('Y-m-d H:i:s', $value, 'Europe/Amsterdam');
$dateArray = [
'year' => $dateTime->year,
'month' => $dateTime->month,
'day' => $dateTime->day,
'hour' => $dateTime->hour,
'minute' => $dateTime->minute,
'second' => $dateTime->second,
];
$valueForParent = json_encode($dateArray);
parent::setValue($valueForParent);
} else {
if(count(array_diff(['year', 'month', 'day', 'hour', 'minute', 'second'], array_keys($decodedData))) > 0) throw new \InvalidArgumentException('When you specify the value as a json string it must have these properties: year, month, day, hour, minute and second');
parent::setValue($value);
}
return $this;
}
/**
* @return string Returns the date in the format of 2018-02-23 00:00:00 (Y-m-d H:i:s)
*/
public function getValue(): string
{
if($this->value == "") throw new \RuntimeException('The value of the datepicker never was set');
$dateArray = json_decode($this->value, true);
$value = Carbon::create(intval($dateArray['year']), intval($dateArray['month']), intval($dateArray['day']), intval($dateArray['hour']), intval($dateArray['minute']), intval($dateArray['second']), 'Europe/Amsterdam');
return $value->toDateTimeString();
}
/**
* @return string the value as json like this: {"year":2018,"month":4,"day":14,"hour":18,"minute":2,"second":0}
*/
public function getValueAsJson():string
{
return $this->value;
}
}