Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the Carbon package. |
| 5 | * |
| 6 | * (c) Brian Nesbitt <brian@nesbot.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 11 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 12 | namespace Carbon\Traits; |
| 13 | |
| 14 | use Carbon\Carbon; |
| 15 | use Carbon\CarbonImmutable; |
| 16 | |
| 17 | /** |
| 18 | * Trait Mutability. |
| 19 | * |
| 20 | * Utils to know if the current object is mutable or immutable and convert it. |
| 21 | */ |
| 22 | trait Mutability |
| 23 | { |
| 24 | use Cast; |
| 25 | |
| 26 | /** |
| 27 | * Returns true if the current class/instance is mutable. |
| 28 | * |
| 29 | * @return bool |
| 30 | */ |
| 31 | public static function isMutable() |
| 32 | { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns true if the current class/instance is immutable. |
| 38 | * |
| 39 | * @return bool |
| 40 | */ |
| 41 | public static function isImmutable() |
| 42 | { |
| 43 | return !static::isMutable(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Return a mutable copy of the instance. |
| 48 | * |
| 49 | * @return Carbon |
| 50 | */ |
| 51 | public function toMutable() |
| 52 | { |
| 53 | /** @var Carbon $date */ |
| 54 | $date = $this->cast(Carbon::class); |
| 55 | |
| 56 | return $date; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Return a immutable copy of the instance. |
| 61 | * |
| 62 | * @return CarbonImmutable |
| 63 | */ |
| 64 | public function toImmutable() |
| 65 | { |
| 66 | /** @var CarbonImmutable $date */ |
| 67 | $date = $this->cast(CarbonImmutable::class); |
| 68 | |
| 69 | return $date; |
| 70 | } |
| 71 | } |