Boundaries.php
2 years ago
Cast.php
2 years ago
Comparison.php
1 year ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
1 month ago
DeprecatedProperties.php
2 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
2 years ago
Localization.php
1 month ago
Macro.php
2 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
2 years ago
Mutability.php
2 years ago
ObjectInitialisation.php
2 years ago
Options.php
1 year ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
1 month ago
Timestamp.php
1 month ago
ToStringFormat.php
2 years ago
Units.php
2 years ago
Week.php
2 years ago
Cast.php
41 lines
| 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 | */ |
| 11 | namespace IAWPSCOPED\Carbon\Traits; |
| 12 | |
| 13 | use IAWPSCOPED\Carbon\Exceptions\InvalidCastException; |
| 14 | use DateTimeInterface; |
| 15 | /** |
| 16 | * Trait Cast. |
| 17 | * |
| 18 | * Utils to cast into an other class. |
| 19 | * @internal |
| 20 | */ |
| 21 | trait Cast |
| 22 | { |
| 23 | /** |
| 24 | * Cast the current instance into the given class. |
| 25 | * |
| 26 | * @param string $className The $className::instance() method will be called to cast the current object. |
| 27 | * |
| 28 | * @return DateTimeInterface |
| 29 | */ |
| 30 | public function cast(string $className) |
| 31 | { |
| 32 | if (!\method_exists($className, 'instance')) { |
| 33 | if (\is_a($className, DateTimeInterface::class, \true)) { |
| 34 | return new $className($this->rawFormat('Y-m-d H:i:s.u'), $this->getTimezone()); |
| 35 | } |
| 36 | throw new InvalidCastException("{$className} has not the instance() method needed to cast the date."); |
| 37 | } |
| 38 | return $className::instance($this); |
| 39 | } |
| 40 | } |
| 41 |