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

improve transforming date fields #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
stability: [prefer-stable]
php: [8.1, 8.2]
php: [8.1, 8.2, 8.3, 8.4]
laravel: [10]

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"api-ecosystem-for-laravel/dingo-api": "^4.1",
"php-open-source-saver/jwt-auth": "^2.1",
"illuminate/support": "^10.0",
"ramsey/uuid": "^4.3"
"ramsey/uuid": "^4.3",
"nesbot/carbon": "^2.0|^3.0"
},
"require-dev": {
"ext-json": "*",
Expand Down
40 changes: 37 additions & 3 deletions src/Transformers/RestfulTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Specialtactics\L5Api\Transformers;

use DateTimeInterface;
use Carbon\Carbon;
use League\Fractal\TransformerAbstract;
use Specialtactics\L5Api\APIBoilerplate;
use Specialtactics\L5Api\Models\RestfulModel;
use Illuminate\Database\Eloquent\Model as EloquentModel;

class RestfulTransformer extends TransformerAbstract
{
public const DATE_CAST_TYPES = ['date', 'datetime', 'immutable_date', 'immutable_datetime'];

/**
* @var RestfulModel The model to be transformed
*/
Expand Down Expand Up @@ -82,11 +86,19 @@ public function transformRestfulModel(EloquentModel $model)
}, ARRAY_FILTER_USE_KEY);

/*
* Format all dates as Iso8601 strings, this includes the created_at and updated_at columns
* Format all dates as Iso8601 strings, this includes timestamped columns
*/
foreach ($model->getDates() as $dateColumn) {
foreach ($this->getModelDateFields($model) as $dateColumn) {
if (! empty($model->$dateColumn) && ! in_array($dateColumn, $filterOutAttributes)) {
$transformed[$dateColumn] = $model->$dateColumn->toIso8601String();
if ($model->$dateColumn instanceof DateTimeInterface) {
$transformed[$dateColumn] = Carbon::instance($model->$dateColumn)->toIso8601String();
} else {
try {
$transformed[$dateColumn] = Carbon::parse($model->$dateColumn)->toIso8601String();
} catch (\Error $e) {
$transformed[$dateColumn] = $model->$dateColumn;
}
}
}
}

Expand Down Expand Up @@ -253,4 +265,26 @@ protected function transformRelations(array $transformed)

return $transformed;
}

protected function getModelDateFields(EloquentModel $model): array
{
$dateFields = [];

foreach ($model->getDates() as $dateColumn) {
$dateFields[] = $dateColumn;
}

// For previous versions of Eloquent, dates were stored as arrays
if ($model->dates && is_array($model->dates)) {
$dateFields = array_merge($model->dates, $dateFields);
}

foreach ($model->getCasts() as $field => $castType) {
if (in_array($castType, static::DATE_CAST_TYPES)) {
$dateFields[] = $field;
}
}

return array_unique($dateFields);
}
}