-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 4341734
Showing
7 changed files
with
114 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,2 @@ | ||
vendor/ | ||
composer.lock |
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,25 @@ | ||
# PHP Castable | ||
|
||
Basic groundwork for type-casting in PHP. | ||
|
||
## Features / Functionality | ||
|
||
- Works with simple types and objects | ||
- `cast($value, $type)` function that converts a value to a target type. | ||
- `Castable` interface, exposes function called whenever cast() is called on objects implementing this interface. | ||
- Error handling - all errors routed to `NotCastableException` | ||
- Fixes type-hinting for PhpStorm | ||
- PHP 5.6+ (but seriously, stop using PHP 5 :)) | ||
|
||
While `cast()` is just a regular PHP function, it would be the equivalent to type-casting operators in other languages (e.g. `val as Type`, `(Type)val`, `val.to(Type)`, `CAST(val, TYPE)`...). | ||
|
||
## Behaviour | ||
|
||
1. If the value to be type-casted is not an object, PHP's `settype()` is used. | ||
2. If, instead, it is an object that implements `Castable` interface, `castTo()` is called and its value returned. | ||
3. Otherwise, if the object is the same or a subclass of the desired type, then it is returned unchanged. | ||
|
||
## Motivation | ||
|
||
In many cases, having specific `castToX()` methods in your classes is enough and the behaviour typically works adequately. | ||
However, sometimes this becomes too much or a more dynamic solution is needed. In this case, this package helps to avoid writing the boilerplate code. |
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,22 @@ | ||
{ | ||
"name": "uuf6429/php-castable", | ||
"description": "Type casting functionality for PHP", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Christian Sciberras", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"uuf6429\\Castable\\": "src/" | ||
}, | ||
"files": [ | ||
"src/functions.php" | ||
] | ||
}, | ||
"require": { | ||
"php": "^5.6 || ^7 || ^8" | ||
} | ||
} |
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,6 @@ | ||
<?php | ||
|
||
namespace PHPSTORM_META { | ||
override(\uuf6429\Castable\cast(1), map(['' => '@'])); | ||
override(\uuf6429\Castable\Castable::castTo(0), map(['' => '@'])); | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace uuf6429\Castable; | ||
|
||
interface Castable | ||
{ | ||
/** | ||
* @param string $type | ||
* @return mixed | ||
*/ | ||
public function castTo($type); | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace uuf6429\Castable; | ||
|
||
use RuntimeException; | ||
|
||
class NotCastableException extends RuntimeException | ||
{ | ||
|
||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace uuf6429\Castable; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
function cast($value, $type) | ||
{ | ||
if (!is_object($value)) { | ||
if (settype($value, $type)) { | ||
return $value; | ||
} | ||
throw new NotCastableException( | ||
sprintf('Value of type %s cannot be cast to %s', gettype($value), $type) | ||
); | ||
} | ||
|
||
if ($value instanceof Castable) { | ||
try { | ||
return $value->castTo($type); | ||
} catch (Exception $exception) { | ||
} catch (Throwable $exception) { | ||
} | ||
throw new NotCastableException( | ||
sprintf('Castable object could not be cast to %s', $type), 0, $exception | ||
); | ||
} | ||
|
||
if (!is_a($value, $type)) { | ||
throw new NotCastableException( | ||
sprintf('Object of class %s is not compatible with class %s', get_class($value), $type) | ||
); | ||
} | ||
|
||
return $value; | ||
} |