| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\Traits; |
| 15 |
|
| 16 |
use Carbon\Exceptions\InvalidCastException; |
| 17 |
use DateTimeInterface; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
trait Cast |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function cast(string $className): mixed |
| 36 |
{ |
| 37 |
if (!method_exists($className, 'instance')) { |
| 38 |
if (is_a($className, DateTimeInterface::class, true)) { |
| 39 |
return $className::createFromFormat('U.u', $this->rawFormat('U.u')) |
| 40 |
->setTimezone($this->getTimezone()); |
| 41 |
} |
| 42 |
|
| 43 |
throw new InvalidCastException("$className has not the instance() method needed to cast the date."); |
| 44 |
} |
| 45 |
|
| 46 |
return $className::instance($this); |
| 47 |
} |
| 48 |
} |