Download the PHP package techins/parse-bool without Composer
On this page you can find all versions of the php package techins/parse-bool. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package parse-bool
ParseBool
ParseBool is a PHP class designed to facilitate the parsing and validation of boolean values from various input types. It simplifies the process of interpreting values that can be represented as true or false, making your code cleaner and more reliable.
Purpose
The primary purpose of the ParseBool class is to convert different data types—such as strings, numbers, and booleans—into a consistent boolean output (true or false). This is particularly useful when dealing with user input or data from external sources, where the representation of boolean values can vary.
Installation
You can include the ParseBool class in your project by copying the ParseBool.php file into your desired directory.
Usage
Parsing and Casting value into Boolean
To use the ParseBool class, simply call the parseBool method with your value as an argument.
Example
Strict checking
Upon strinct checking if the provided value cannot be interpreted as a boolean, a ValueNotBooleanOneException will be thrown. You should wrap your calls in a try-catch block to handle this exception gracefully:
Input validation
You can also validate a boolean value (for example if value is a checked checkbox)
Valid Boolean Values
The following values are considered valid and will be converted to true:
1true'on''yes''true'
The following values are considered valid and will be converted to false:
0false'off''no''false'f
Any other values will result in a ValueNotBooleanOneException.
Usage upon checkboxes
Assuming we have thius form
You can use validateBooleableValue to validate whether checkbox is checked.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please submit a pull request or open an issue for any enhancements or bug fixes.