blob: 561c867d0f1f78e724e0bd72d80b3b8625574d2f [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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 Benkard1ba53812022-12-27 17:32:58 +010011
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020012namespace Carbon\Traits;
13
14use Carbon\Carbon;
15use Carbon\CarbonImmutable;
16
17/**
18 * Trait Mutability.
19 *
20 * Utils to know if the current object is mutable or immutable and convert it.
21 */
22trait 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}