PHP simple library for managing of data types.
This library is supported by PHP 7.4 version.
The preferred way to install this extension is through Composer.
To install PHP Validate library, simply:
composer require Josantonius/Validate
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
composer require Josantonius/Validate --prefer-source
You can also clone the complete repository with Git:
$ git clone https://github.com/Josantonius/PHP-Validate.git
Or install it manually:
wget https://raw.githubusercontent.com/Josantonius/PHP-Validate/master/src/Validate.php
Available methods in this library:
Validate::asArray($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asObject($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asJson($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asString($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asInteger($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asFloat($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asBoolean($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asIp($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asUrl($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
Validate::asEmail($data, $default);| Attribute | Description | Type | Required | Default |
|---|---|---|---|---|
| $data | Data to convert. | mixed | Yes | |
| $default | Default value in error case. | mixed | No | ´null´ |
# Return (mixed|null) → value, null or customized return value
To use this library with Composer:
require__DIR__ . '/vendor/autoload.php';
useJosantonius\Validate\Validate;Or if you installed it manually, use it:
require_once__DIR__ . '/Validate.php';
useJosantonius\Validate\Validate;Example of use for this library:
var_dump(Validate::asArray(['foo', 'bar'])); // ['foo', 'bar']var_dump(Validate::asArray('["foo", "bar"]')); // ['foo', 'bar']$data = new \StdClass;
$data->foo = 'bar';
var_dump(Validate::asArray($data)); // ['foo' => 'bar']var_dump(Validate::asArray('{"foo": "bar"}')); // ['foo' => 'bar']var_dump(Validate::asArray(false)); // nullvar_dump(Validate::asArray(false, ['foo', 'bar'])); // ['foo', 'bar']$data = new \StdClass;
$data->foo = 'bar';
$object = Validate::asObject($data);
echo$object->foo; // 'bar'$object = Validate::asObject('{"foo": "bar"}');
echo$object->foo; // 'bar'$object = Validate::asObject(['foo' => 'bar']));
echo$object->foo; // 'bar'var_dump(Validate::asObject(false)); // null$object = Validate::asObject(false, ['foo' => 'bar']);
echo$object->foo; // 'bar'echo Validate::asJson('{"foo": "bar"}'); // '{"foo": "bar"}'echo Validate::asJson(['foo' => 'bar']); // '{"foo":"bar"}'$data = new \StdClass;
$data->foo = 'bar';
echo Validate::asJson($data); // '{"foo":"bar"}'var_dump(Validate::asJson(false)); // nullecho Validate::asJson(false, '["foo", "bar"]'); // '["foo", "bar"]'echo Validate::asString('foo'); // 'foo'echo Validate::asString(221104); // '221104'var_dump(Validate::asString(false)); // nullecho Validate::asString(false, 'foo'); // 'foo'echo Validate::asInteger(8); // 8echo Validate::asInteger('8'); // 8var_dump(Validate::asInteger(false)); // nullecho Validate::asInteger(false, 8); // 8echo Validate::asFloat(8.8); // 8.8echo Validate::asFloat('8.8'); // 8.8var_dump(Validate::asFloat(false)); // nullecho Validate::asFloat(false, 8.8); // 8.8var_dump(Validate::asBoolean(true)); // truevar_dump(Validate::asBoolean('true')); // truevar_dump(Validate::asBoolean(1)); // truevar_dump(Validate::asBoolean('1')); // truevar_dump(Validate::asBoolean(false)); // falsevar_dump(Validate::asBoolean('false')); // falsevar_dump(Validate::asBoolean(0)); // falsevar_dump(Validate::asBoolean('0')); // falsevar_dump(Validate::asBoolean(null)); // nullecho Validate::asBoolean(null, true); // trueecho Validate::asIp('255.255.255.0'); // '255.255.255.0'var_dump(Validate::asIp(null)); // nullecho Validate::asIp(null, '255.255.255.0'); // '255.255.255.0'echo Validate::asUrl('https://josantonius.com'); // 'https://josantonius.com'var_dump(Validate::asUrl(null)); // nullecho Validate::asUrl(null, 'https://josantonius.com'); // 'https://josantonius.com'echo Validate::asEmail('hello@josantonius.com'); // 'hello@josantonius.com'var_dump(Validate::asEmail(null)); // nullecho Validate::asEmail(null, 'hello@josantonius.com'); // 'hello@josantonius.com'To run tests you just need composer and to execute the following:
git clone https://github.com/Josantonius/PHP-Validate.git
cd PHP-Validate
composer install
Run unit tests with PHPUnit:
composer phpunit
Run PSR2 code standard tests with PHPCS:
composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmd
Run all previous tests:
composer tests
If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊
This repository is licensed under the MIT License.
Copyright © 2018-2022, Josantonius