Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Apr 23, 2022
0 parents commit 55450f7
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
25 changes: 25 additions & 0 deletions README.md
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.
22 changes: 22 additions & 0 deletions composer.json
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"
}
}
6 changes: 6 additions & 0 deletions src/.phpstorm.meta.php
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(['' => '@']));
}
12 changes: 12 additions & 0 deletions src/Castable.php
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);
}
10 changes: 10 additions & 0 deletions src/NotCastableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace uuf6429\Castable;

use RuntimeException;

class NotCastableException extends RuntimeException
{

}
35 changes: 35 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use uuf6429\Castable\Castable;
use uuf6429\Castable\NotCastableException;

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;
}

0 comments on commit 55450f7

Please sign in to comment.