1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace App\Casts;
9
10use Carbon\Carbon;
11use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
12
13/**
14 * For columns which use unix timestamp as its value and repurpose 0 as null.
15 */
16class TimestampOrZero implements CastsAttributes
17{
18 public function get($model, string $key, $value, array $attributes)
19 {
20 return $value === null || $value === 0 ? null : Carbon::createFromTimestamp($value);
21 }
22
23 public function set($model, string $key, $value, array $attributes)
24 {
25 return $value === null ? 0 : $value->getTimestamp();
26 }
27}