|
| 1 | +Squirrel Types |
| 2 | +===================== |
| 3 | + |
| 4 | +[](LICENSE) [](https://packagist.org/packages/squirrelphp/types) [](https://packagist.org/packages/squirrelphp/types) [](LICENSE) |
| 5 | + |
| 6 | +Explicit handling of type coercions and enforcement in PHP in order to deal with unknown data in a more predictable and safe way. |
| 7 | + |
| 8 | +Installation |
| 9 | +------------ |
| 10 | + |
| 11 | + composer require squirrelphp/types |
| 12 | + |
| 13 | +Table of contents |
| 14 | +----------------- |
| 15 | + |
| 16 | +- [Introduction](#introduction) |
| 17 | +- [Coercion behavior overview](#coercion-behavior-overview) |
| 18 | +- [Test if value can be coerced](#test-if-value-can-be-coerced) |
| 19 | +- [Coerce a value](#coerce-a-value) |
| 20 | +- [Enforce a type for a value](#enforce-a-type-for-a-value) |
| 21 | + |
| 22 | +Introduction |
| 23 | +------------ |
| 24 | + |
| 25 | +When getting values from a database or an API (or even just another PHP component you do not know) you have an expectation of what type of value you will get, but you might get something more or less different. |
| 26 | + |
| 27 | +A database might give you the string "1" for a boolean, or the integer 1, and that would often be fine (many databases have no boolean type, so some coercion is often necessary). But getting the string "hello" or the integer -30 should not be fine for a boolean and most likely points to a mistake - maybe the expected type is wrong, or the wrong field in the database was retrieved. |
| 28 | + |
| 29 | +This small component coerces and enforces types and is fully tested to behave in a predictable way. This should often be better than using other coercion methods like explicit casts `(bool)` / `boolval()`, as these will coerce any value, while this component will reject unreasonable values and throw a TypeError. It coerces less values than PHP does in coercive typing mode, as PHP accepts quite a few questionable values for legacy/BC reasons. |
| 30 | + |
| 31 | +Coercion behavior overview |
| 32 | +-------------------------- |
| 33 | + |
| 34 | +All argument flags mentioned below are set to false by default for a more conservative coercion behavior. Enable them only if it is necessary for your use case. |
| 35 | + |
| 36 | +### toBool |
| 37 | + |
| 38 | +- Always accepts bools |
| 39 | +- Accepts "0" and "1" strings |
| 40 | +- Accepts 0 and 1 ints |
| 41 | +- Only allows "" string if the argument `$allowEmptyString` is set to true |
| 42 | +- Only allows 0.0 and 1.0 floats if the argument `$allowFloat` is set to true |
| 43 | + |
| 44 | +### toInt |
| 45 | + |
| 46 | +- Always accepts ints |
| 47 | +- Accepts floats and numeric strings without a fractional part |
| 48 | +- Only allows bools if the argument `$allowBool` is set to true |
| 49 | + |
| 50 | +### toFloat |
| 51 | + |
| 52 | +- Always accepts ints and floats |
| 53 | +- Accepts numeric strings |
| 54 | +- Only allows bools if the argument `$allowBool` is set to true |
| 55 | + |
| 56 | +### toString |
| 57 | + |
| 58 | +- Always accepts strings |
| 59 | +- Accepts any ints and floats |
| 60 | +- Only allows bools if the argument `$allowBool` is set to true |
| 61 | +- Only allows Stringable objects if the argument `$allowStringable` is set to true |
| 62 | + |
| 63 | +Test if value can be coerced |
| 64 | +---------------------------- |
| 65 | + |
| 66 | +All these functions have the mixed `$value` as their first argument and return true or false: |
| 67 | + |
| 68 | +### Coerceable::toBool |
| 69 | + |
| 70 | +Returns true if `$value` is one of the following: |
| 71 | + |
| 72 | +- A boolean |
| 73 | +- A string with value '0' or '1' |
| 74 | +- An int with value 0 or 1 |
| 75 | +- An empty string - only if the argument `$allowEmptyString` is set to true |
| 76 | +- A float with value 0 or 1 - only if the `$allowFloat` argument is set to true |
| 77 | + |
| 78 | +For any other values it returns false. |
| 79 | + |
| 80 | +### Coerceable::toInt |
| 81 | + |
| 82 | +Returns true if `$value` is one of the following: |
| 83 | + |
| 84 | +- An integer |
| 85 | +- A float without fractional part |
| 86 | +- A numeric string without fractional part |
| 87 | +- A boolean - only if the `$allowBool` argument is set to true |
| 88 | + |
| 89 | +For any other values it returns false. |
| 90 | + |
| 91 | +### Coerceable::toFloat |
| 92 | + |
| 93 | +Returns true if `$value` is one of the following: |
| 94 | + |
| 95 | +- An integer or float |
| 96 | +- A numeric string |
| 97 | +- A boolean - only if the `$allowBool` argument is set to true |
| 98 | + |
| 99 | +For any other values it returns false. |
| 100 | + |
| 101 | +### Coerceable::toString |
| 102 | + |
| 103 | +Returns true if `$value` is one of the following: |
| 104 | + |
| 105 | +- A string |
| 106 | +- An integer or float |
| 107 | +- A boolean - only if the `$allowBool` argument is set to true |
| 108 | +- A Stringable object - only if the `$allowStringable` argument is set to true |
| 109 | + |
| 110 | +For any other values it returns false. |
| 111 | + |
| 112 | +Coerce a value |
| 113 | +-------------- |
| 114 | + |
| 115 | +All these functions have the mixed `$value` as their first argument and return the type they are coercing (or throw a TypeError). |
| 116 | + |
| 117 | +### Coerce::toBool |
| 118 | + |
| 119 | +Returns a boolean if the given value is coerceable, see [Coerceable::toBool for valid values](#coerceabletobool), or a TypeError if the value is not coerceable. |
| 120 | + |
| 121 | +### Coerce::toInt |
| 122 | + |
| 123 | +Returns an integer if the given value is coerceable, see [Coerceable::toInt for valid values](#coerceabletoint), or a TypeError if the value is not coerceable. |
| 124 | + |
| 125 | +### Coerce::toFloat |
| 126 | + |
| 127 | +Returns a float if the given value is coerceable, see [Coerceable::toFloat for valid values](#coerceabletofloat), or a TypeError if the value is not coerceable. |
| 128 | + |
| 129 | +### Coerce::toString |
| 130 | + |
| 131 | +Returns a string if the given value is coerceable, see [Coerceable::toString for valid values](#coerceabletoint), or a TypeError if the value is not coerceable. |
| 132 | + |
| 133 | +Enforce a type for a value |
| 134 | +-------------------------- |
| 135 | + |
| 136 | +All these functions have the mixed `$value` as their only argument and return the type they are enforcing, according to the same logic as strict mode in PHP. |
| 137 | + |
| 138 | +### Enforce::asBool |
| 139 | + |
| 140 | +Returns `$value` as a boolean if it is a boolean. Throws a TypeError otherwise. |
| 141 | + |
| 142 | +### Enforce::asInt |
| 143 | + |
| 144 | +Returns `$value` as an integer if it is an integer. Throws a TypeError otherwise. |
| 145 | + |
| 146 | +### Enforce::asFloat |
| 147 | + |
| 148 | +Returns `$value` as a float if it is an integer or a float. Throws a TypeError otherwise. |
| 149 | + |
| 150 | +### Enforce::asString |
| 151 | + |
| 152 | +Returns `$value` as a string if it is a string. Throws a TypeError otherwise. |
0 commit comments