Download .zip |
Info | Documentation | View files (14) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2018-09-22 (26 days ago) | Not yet rated by the users | Total: 1 This week: 1 | All time: 9,392 This week: 409 |
Version | License | PHP version | Categories | |||
php-validate 1.0 | MIT/X Consortium ... | 7 | Data types, Validation, PHP 7 |
Description | Author | |
This is a simple library validate data of several types. |
PHP simple library for managing of data types.
This library is supported by PHP versions 7.0 or higher and is compatible with HHVM versions 3.0 or higher.
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('hello@josantonius.com'); // 'hello@josantonius.com'
var_dump(Validate::asEmail(null)); // null
echo 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 you would like to help, please take a look at the list of issues or the To Do checklist.
Pull requests
composer install
to install the dependencies.
This will also install the dev dependencies.composer fix
to excute code standard fixers.The file structure from this repository was created with PHP-Skeleton.
This project is licensed under MIT license. See the LICENSE file for more info.
2018 Josantonius, josantonius.com
If you find it useful, let me know :wink:
Files |
File | Role | Description | ||
---|---|---|---|---|
src (1 file) | ||||
tests (1 file) | ||||
.editorconfig | Data | Auxiliary data | ||
.php_cs.dist | Example | Example script | ||
.travis.yml | Data | Auxiliary data | ||
CHANGELOG.md | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
CONDUCT.md | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpcs.xml | Data | Auxiliary data | ||
phpmd.xml | Data | Auxiliary data | ||
phpunit.xml | Data | Auxiliary data | ||
README-ES.md | Doc. | Documentation | ||
README.md | Doc. | Documentation |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.