GetStringDuckTypes::getStringDuckTypes()

Not yet in a tagged release

Description

GetStringDuckTypes::getStringDuckTypes() 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 array GetStringDuckTypes::getStringDuckTypes(mixed $item);

Parameters

The input parameters are:

Return Value

GetStringDuckTypes::getStringDuckTypes() returns an array.

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::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:

$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes("true"));

// outputs
//
// array(1) {
//   ["string"]=>
//   string(6) "string"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes("false"));

// outputs
//
// array(1) {
//   ["string"]=>
//   string(6) "string"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes("0.0"));

// outputs
//
// array(3) {
//   ["double"]=>
//   string(6) "double"
//   ["numeric"]=>
//   string(7) "numeric"
//   ["string"]=>
//   string(6) "string"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes("3.1415927"));

// outputs
//
// array(3) {
//   ["double"]=>
//   string(6) "double"
//   ["numeric"]=>
//   string(7) "numeric"
//   ["string"]=>
//   string(6) "string"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes("0"));

// outputs
//
// array(3) {
//   ["integer"]=>
//   string(7) "integer"
//   ["numeric"]=>
//   string(7) "numeric"
//   ["string"]=>
//   string(6) "string"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes("100"));

// outputs
//
// array(3) {
//   ["integer"]=>
//   string(7) "integer"
//   ["numeric"]=>
//   string(7) "numeric"
//   ["string"]=>
//   string(6) "string"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(new Exception(__FILE__)));

// outputs
//
// array(1) {
//   ["string"]=>
//   string(6) "string"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes("hello, world!"));

// outputs
//
// array(1) {
//   ["string"]=>
//   string(6) "string"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(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"
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(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:

$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(null));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes([1,2,3]));

// outputs
//
// array(0) {
// }
use GanbaroDigital\MissingBits\TypeInspectors\GetStrictTypes;

$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes([GetStrictTypes::class, "from"]));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(function(){}));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(true));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(false));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(0.0));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(3.1415927));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(0));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(100));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(-100));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(new ArrayObject));

// outputs
//
// array(0) {
// }
use GanbaroDigital\MissingBits\TypeInspectors\GetStrictTypes;

$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(new GetStrictTypes));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes((object)[]));

// outputs
//
// array(0) {
// }
$inspector = new GetStringDuckTypes;
var_dump($inspector->getStringDuckTypes(STDIN));

// outputs
//
// array(0) {
// }

Throws

GetStringDuckTypes::getStringDuckTypes() does not throw any exceptions.

Works With

GetStringDuckTypes::getStringDuckTypes() is supported on these versions of PHP:

PHP Version Works?
5.5 Yes
5.6 Yes
7.0 Yes
HHVM Yes