Assertion library done right for modern PHP and static analyzer friendly.
The library can be installed via composer.
composer require xynha/php-assert
The main reason is I need a flexible assertion library that can be used:
- to throw custom exception and/or custom message,
- without throwing an exception, and
- to execute a callback (if needed).
// will throw InvalidArgumentException and default message
Assert::isTrue($condition);
// will throw InvalidArgumentException and custom message
Assert::isTrue($condition, null, 'Custom message');
// will throw UnexpectedValueException and default message
Assert::isTrue($condition, UnexpectedValueException::class);
// will throw UnexpectedValueException and custom message
Assert::isTrue($condition, UnexpectedValueException::class, 'Custom message');
if (Assert::isTrue($condition, false)) {
return;
}
Features are still under development. Feel free to request some features.
Note:
$handler
is optional and can acceptnull
,throwable
, orfalse
value (see usage above).$msg
is optional custom exception message.
isBool($value, $handler, string $msg) : bool;
isInt($value, $handler, string $msg) : bool;
isFloat($value, $handler, string $msg) : bool;
isNumeric($value, $handler, string $msg) : bool;
isString($value, $handler, string $msg) : bool;
isArray($value, $handler, string $msg) : bool;
isIterable($value, $handler, string $msg) : bool;
isObject($value, $handler, string $msg) : bool;
isCallable($value, $handler, string $msg) : bool;
isResource($value, $handler, string $msg) : bool;
isNull($value, $handler, string $msg) : bool;
isNotNull($value, $handler, string $msg) : bool;
isScalar($value, $handler, string $msg) : bool;
NOTE:
is_countable
currently is not supported (requirePHP >= 7.3
).
isTrue(bool $value, $handler, string $msg) : bool;
isFalse(bool $value, $handler, string $msg) : bool;
All form of contributions are welcome. You can report issues, fork the repo and submit pull request.
For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Released under Apache-2.0 License. See LICENSE file for more details.
Copyright 2020 Asis Pattisahusiwa
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.