Skip to content

Commit

Permalink
Add DateModeOption enum and Date field type
Browse files Browse the repository at this point in the history
  • Loading branch information
tdwesten committed Feb 2, 2024
1 parent d03e872 commit 21cea86
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Enums/DateModeOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Tdwesten\StatamicBuilder\Enums;

enum DateModeOption: string
{
case Single = 'single';
case Range = 'range';
}
128 changes: 128 additions & 0 deletions src/FieldTypes/Date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace Tdwesten\StatamicBuilder\FieldTypes;

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Tdwesten\StatamicBuilder\Contracts\Makeble;
use Tdwesten\StatamicBuilder\Enums\DateModeOption;

class Date extends Field
{
use Makeble;

protected $type = 'date';

protected $mode = DateModeOption::Single;

protected $inline = false;

protected $fullWidth = false;

protected $columns = 1;

protected $rows = 1;

protected $timeEnabled = false;

protected $timeSecondsEnabled = false;

protected $earliestDate;

protected $latestDate;

protected $format;

protected $dateFormatForExport = 'Y-m-d';

public function __construct(string $handle)
{
parent::__construct($handle);
}

public function fieldToArray(): Collection
{
return collect([
'mode' => $this->mode->value,
'inline' => $this->inline,
'full_width' => $this->fullWidth,
'columns' => $this->columns,
'rows' => $this->rows,
'time_enabled' => $this->timeEnabled,
'time_seconds_enabled' => $this->timeSecondsEnabled,
'earliest_date' => $this->earliestDate ? $this->earliestDate->format($this->dateFormatForExport) : null,
'latest_date' => $this->latestDate ? $this->latestDate->format($this->dateFormatForExport) : null,
'format' => $this->format,
]);
}

public function mode(DateModeOption $mode)
{
$this->mode = $mode;

return $this;
}

public function inline()
{
$this->inline = true;

return $this;
}

public function fullWidth()
{
$this->fullWidth = true;

return $this;
}

public function columns(int $columns)
{
$this->columns = $columns;

return $this;
}

public function rows(int $rows)
{
$this->rows = $rows;

return $this;
}

public function timeEnabled()
{
$this->timeEnabled = true;

return $this;
}

public function timeSecondsEnabled()
{
$this->timeSecondsEnabled = true;

return $this;
}

public function earliestDate(Carbon $earliestDate)
{
$this->earliestDate = $earliestDate;

return $this;
}

public function latestDate(Carbon $latestDate)
{
$this->latestDate = $latestDate;

return $this;
}

public function format(string $format)
{
$this->format = $format;

return $this;
}
}
99 changes: 99 additions & 0 deletions tests/Unit/DateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

it('can render to a array', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->displayName('Display Name')
->instructions('Enter the title')
->visibility('hidden')
->required()
->instructionsPosition('below')
->listable()
->replicatorPreview(true)
->width(50);

expect($field->toArray()['field']['display'])->toBe('Display Name');

expect($field->toArray()['field']['instructions'])->toBe('Enter the title');

expect($field->toArray()['field']['visibility'])->toBe('hidden');

expect($field->toArray()['field']['validate'])->toBe(['required']);

expect($field->toArray()['field']['instructions_position'])->toBe('below');

expect($field->toArray()['field']['listable'])->toBe(true);

expect($field->toArray()['field']['replicator_preview'])->toBe(true);

expect($field->toArray()['field']['width'])->toBe(50);
});

it('can render to a array with mode', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->mode(\Tdwesten\StatamicBuilder\Enums\DateModeOption::Range);

expect($field->toArray()['field']['mode'])->toBe('range');
});

it('can render to a array with inline', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->inline();

expect($field->toArray()['field']['inline'])->toBe(true);
});

it('can render to a array with full width', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->fullWidth();

expect($field->toArray()['field']['full_width'])->toBe(true);
});

it('can render to a array with columns', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->columns(2);

expect($field->toArray()['field']['columns'])->toBe(2);
});

it('can render to a array with rows', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->rows(2);

expect($field->toArray()['field']['rows'])->toBe(2);
});

it('can render to a array with time enabled', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->timeEnabled();

expect($field->toArray()['field']['time_enabled'])->toBe(true);
});

it('can render to a array with time seconds enabled', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->timeSecondsEnabled();

expect($field->toArray()['field']['time_seconds_enabled'])->toBe(true);
});

it('can render to a array with earliest date', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->earliestDate(new \Carbon\Carbon('2021-01-01'));

expect($field->toArray()['field']['earliest_date'])->toBe('2021-01-01');
});

it('can render to a array with latest date', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->latestDate(new \Carbon\Carbon('2021-01-01'));

expect($field->toArray()['field']['latest_date'])->toBe('2021-01-01');
});

it('can render to a array with format', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Date('title');
$field->format('Y-m-d');

expect($field->toArray()['field']['format'])->toBe('Y-m-d');
});

0 comments on commit 21cea86

Please sign in to comment.