generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d70e8a
commit 8ff9118
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Casts; | ||
|
||
use Spatie\LaravelData\Support\Creation\CreationContext; | ||
use Spatie\LaravelData\Support\DataProperty; | ||
|
||
class BuiltinTypeCast implements Cast, IterableItemCast | ||
{ | ||
/** | ||
* @param 'bool'|'int'|'float'|'array'|'string' $type | ||
*/ | ||
public function __construct( | ||
protected string $type, | ||
) { | ||
} | ||
|
||
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed | ||
{ | ||
return $this->runCast($value); | ||
} | ||
|
||
public function castIterableItem(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed | ||
{ | ||
return $this->runCast($value); | ||
} | ||
|
||
protected function runCast(mixed $value): mixed | ||
{ | ||
return match ($this->type) { | ||
'bool' => (bool) $value, | ||
'int' => (int) $value, | ||
'float' => (float) $value, | ||
'array' => (array) $value, | ||
'string' => (string) $value, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters