Skip to content

Commit 4341734

Browse files
committed
Initial commit
0 parents  commit 4341734

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
composer.lock

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# PHP Castable
2+
3+
Basic groundwork for type-casting in PHP.
4+
5+
## Features / Functionality
6+
7+
- Works with simple types and objects
8+
- `cast($value, $type)` function that converts a value to a target type.
9+
- `Castable` interface, exposes function called whenever cast() is called on objects implementing this interface.
10+
- Error handling - all errors routed to `NotCastableException`
11+
- Fixes type-hinting for PhpStorm
12+
- PHP 5.6+ (but seriously, stop using PHP 5 :))
13+
14+
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)`...).
15+
16+
## Behaviour
17+
18+
1. If the value to be type-casted is not an object, PHP's `settype()` is used.
19+
2. If, instead, it is an object that implements `Castable` interface, `castTo()` is called and its value returned.
20+
3. Otherwise, if the object is the same or a subclass of the desired type, then it is returned unchanged.
21+
22+
## Motivation
23+
24+
In many cases, having specific `castToX()` methods in your classes is enough and the behaviour typically works adequately.
25+
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.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "uuf6429/php-castable",
3+
"description": "Type casting functionality for PHP",
4+
"type": "library",
5+
"authors": [
6+
{
7+
"name": "Christian Sciberras",
8+
"email": "christian@sciberras.me"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"uuf6429\\Castable\\": "src/"
14+
},
15+
"files": [
16+
"src/functions.php"
17+
]
18+
},
19+
"require": {
20+
"php": "^5.6 || ^7 || ^8"
21+
}
22+
}

src/.phpstorm.meta.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace PHPSTORM_META {
4+
override(\uuf6429\Castable\cast(1), map(['' => '@']));
5+
override(\uuf6429\Castable\Castable::castTo(0), map(['' => '@']));
6+
}

src/Castable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace uuf6429\Castable;
4+
5+
interface Castable
6+
{
7+
/**
8+
* @param string $type
9+
* @return mixed
10+
*/
11+
public function castTo($type);
12+
}

src/NotCastableException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace uuf6429\Castable;
4+
5+
use RuntimeException;
6+
7+
class NotCastableException extends RuntimeException
8+
{
9+
10+
}

src/functions.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace uuf6429\Castable;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
function cast($value, $type)
9+
{
10+
if (!is_object($value)) {
11+
if (settype($value, $type)) {
12+
return $value;
13+
}
14+
throw new NotCastableException(
15+
sprintf('Value of type %s cannot be cast to %s', gettype($value), $type)
16+
);
17+
}
18+
19+
if ($value instanceof Castable) {
20+
try {
21+
return $value->castTo($type);
22+
} catch (Exception $exception) {
23+
} catch (Throwable $exception) {
24+
}
25+
throw new NotCastableException(
26+
sprintf('Castable object could not be cast to %s', $type), 0, $exception
27+
);
28+
}
29+
30+
if (!is_a($value, $type)) {
31+
throw new NotCastableException(
32+
sprintf('Object of class %s is not compatible with class %s', get_class($value), $type)
33+
);
34+
}
35+
36+
return $value;
37+
}

0 commit comments

Comments
 (0)