Boolean Assertions
Boolean assertions validate boolean values and truthiness.
Available Assertions
Section titled “Available Assertions”true()
Section titled “true()”Assert that a value is boolean true.
use Cline\Assert\Assertions\Assertion;
Assertion::true($value);Assertion::true($isActive, 'Must be active');false()
Section titled “false()”Assert that a value is boolean false.
Assertion::false($value);Assertion::false($isDeleted, 'Must not be deleted');boolean()
Section titled “boolean()”Assert that a value is a boolean (true or false).
Assertion::boolean($value);Assertion::boolean($flag, 'Value must be boolean');Strict Type Checking
Section titled “Strict Type Checking”These assertions check for actual boolean values only, not truthy/falsy values:
// PassAssertion::true(true);Assertion::false(false);Assertion::boolean(true);
// Fail - These are NOT booleansAssertion::true(1); // integer, not booleanAssertion::false(0); // integer, not booleanAssertion::false(null); // null, not booleanChaining Boolean Assertions
Section titled “Chaining Boolean Assertions”use Cline\Assert\Assert;
Assert::that($isActive) ->boolean() ->true('User must be active');
Assert::that($isDeleted) ->boolean() ->false('Record must not be deleted');Common Patterns
Section titled “Common Patterns”Feature Flag Validation
Section titled “Feature Flag Validation”Assert::that($config['feature_enabled']) ->boolean() ->true('Feature must be enabled');State Validation
Section titled “State Validation”Assert::that($user->is_active) ->boolean('is_active must be boolean') ->true('User account must be active');Configuration Validation
Section titled “Configuration Validation”Assert::lazy() ->that($config['debug'], 'debug')->boolean() ->that($config['cache_enabled'], 'cache_enabled')->boolean() ->verifyNow();Access Control
Section titled “Access Control”Assert::that($user->hasPermission('admin')) ->boolean('Permission check must return boolean') ->true('User must have admin permission');Working with Truthy/Falsy Values
Section titled “Working with Truthy/Falsy Values”If you need to accept truthy/falsy values, convert them first:
$isActive = (bool) $value;Assertion::boolean($isActive);Database Boolean Fields
Section titled “Database Boolean Fields”Many databases store booleans as integers:
// Database returns 1/0$row['is_active'] = 1;
// Convert first$isActive = (bool) $row['is_active'];Assertion::boolean($isActive);Best Practices
Section titled “Best Practices”Type Safety First
Section titled “Type Safety First”function setActive(bool $value) { Assertion::boolean($value); $this->isActive = $value;}Clear Error Messages
Section titled “Clear Error Messages”Assertion::true($isVerified, 'Email address must be verified before proceeding');Next Steps
Section titled “Next Steps”- Type Assertions - Type checking
- Comparison Assertions - Comparing values
- Custom Assertions - Custom validators