GetStringDuckTypes::from()
Description
GetStringDuckTypes::from() returns a list of all possible PHP types for a given string. The list is ordered with the most specific match first.
use GanbaroDigital\MissingBits\TypeInspectors\GetStringDuckTypes;
public static array GetStringDuckTypes::from(mixed $item);
Parameters
The input parameters are:
mixed $item- the item to examine
Return Value
GetStringDuckTypes::from() returns an array.
- If
$itemis an object that implements::__toString(), the list[ 'string' ]is returned. - Otherwise, if
$itemis not a string, an empty list[]is returned. - We check
$itemto see if it can be automatically coerced into anintegeror adouble. If it can, we returnintegerordouble, and we also returnnumeric. - We check
$itemto see if it contains a class name or an interface name. If it does, we return the full class hierarchy, plus alsoclassorinterfaceas appropriate.
The resulting list is a complete list of string-related duck types for $item.
PHP does not automatically convert 'true' or 'false' into booleans.
That's why GetStringDuckTypes doesn't add 'boolean' to the list of returned types if a string contains the text of a boolean value.
Example Return Values
Here's a list of examples of accepted input values:
var_dump(GetStringDuckTypes::from("true"));
// outputs
//
// array(1) {
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from("false"));
// outputs
//
// array(1) {
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from("0.0"));
// outputs
//
// array(3) {
// ["double"]=>
// string(6) "double"
// ["numeric"]=>
// string(7) "numeric"
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from("3.1415927"));
// outputs
//
// array(3) {
// ["double"]=>
// string(6) "double"
// ["numeric"]=>
// string(7) "numeric"
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from("0"));
// outputs
//
// array(3) {
// ["integer"]=>
// string(7) "integer"
// ["numeric"]=>
// string(7) "numeric"
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from("100"));
// outputs
//
// array(3) {
// ["integer"]=>
// string(7) "integer"
// ["numeric"]=>
// string(7) "numeric"
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from(new Exception(__FILE__)));
// outputs
//
// array(1) {
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from("hello, world!"));
// outputs
//
// array(1) {
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from(ArrayObject::class));
// outputs
//
// array(8) {
// ["ArrayObject"]=>
// string(11) "ArrayObject"
// ["IteratorAggregate"]=>
// string(17) "IteratorAggregate"
// ["Traversable"]=>
// string(11) "Traversable"
// ["ArrayAccess"]=>
// string(11) "ArrayAccess"
// ["Serializable"]=>
// string(12) "Serializable"
// ["Countable"]=>
// string(9) "Countable"
// ["class"]=>
// string(5) "class"
// ["string"]=>
// string(6) "string"
// }
var_dump(GetStringDuckTypes::from(Traversable::class));
// outputs
//
// array(3) {
// ["Traversable"]=>
// string(11) "Traversable"
// ["interface"]=>
// string(9) "interface"
// ["string"]=>
// string(6) "string"
// }
Here's a list of examples of ingored input values:
var_dump(GetStringDuckTypes::from(null));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from([1,2,3]));
// outputs
//
// array(0) {
// }
use GanbaroDigital\MissingBits\TypeInspectors\GetStrictTypes;
var_dump(GetStringDuckTypes::from([GetStrictTypes::class, "from"]));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(function(){}));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(true));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(false));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(0.0));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(3.1415927));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(0));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(100));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(-100));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(new ArrayObject));
// outputs
//
// array(0) {
// }
use GanbaroDigital\MissingBits\TypeInspectors\GetStrictTypes;
var_dump(GetStringDuckTypes::from(new GetStrictTypes));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from((object)[]));
// outputs
//
// array(0) {
// }
var_dump(GetStringDuckTypes::from(STDIN));
// outputs
//
// array(0) {
// }
Throws
GetStringDuckTypes::from() does not throw any exceptions.
Works With
GetStringDuckTypes::from() is supported on these versions of PHP:
| PHP Version | Works? |
|---|---|
| 5.5 | Yes |
| 5.6 | Yes |
| 7.0 | Yes |
| HHVM | Yes |