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';
use Josantonius\Validate\Validate;
Or if you installed it manually, use it:
require_once __DIR__ . '/Validate.php';
use Josantonius\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)); // null
var_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)); // null
echo Validate::asJson(false, '["foo", "bar"]'); // '["foo", "bar"]'
echo Validate::asString('foo'); // 'foo'
echo Validate::asString(221104); // '221104'
var_dump(Validate::asString(false)); // null
echo Validate::asString(false, 'foo'); // 'foo'
echo Validate::asInteger(8); // 8
echo Validate::asInteger('8'); // 8
var_dump(Validate::asInteger(false)); // null
echo Validate::asInteger(false, 8); // 8
echo Validate::asFloat(8.8); // 8.8
echo Validate::asFloat('8.8'); // 8.8
var_dump(Validate::asFloat(false)); // null
echo Validate::asFloat(false, 8.8); // 8.8
var_dump(Validate::asBoolean(true)); // true
var_dump(Validate::asBoolean('true')); // true
var_dump(Validate::asBoolean(1)); // true
var_dump(Validate::asBoolean('1')); // true
var_dump(Validate::asBoolean(false)); // false
var_dump(Validate::asBoolean('false')); // false
var_dump(Validate::asBoolean(0)); // false
var_dump(Validate::asBoolean('0')); // false
var_dump(Validate::asBoolean(null)); // null
echo Validate::asBoolean(null, true); // true
echo Validate::asIp('255.255.255.0'); // '255.255.255.0'
var_dump(Validate::asIp(null)); // null
echo 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)); // null
echo Validate::asUrl(null, 'https://josantonius.com'); // 'https://josantonius.com'
echo Validate::asEmail('[email protected]'); // '[email protected]'
var_dump(Validate::asEmail(null)); // null
echo Validate::asEmail(null, '[email protected]'); // '[email protected]'
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