PHP Classes

PHP Validate: Validate data of several types

Recommend this page to a friend!
  Info   View files Documentation   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 128 This week: 1All time: 9,368 This week: 560Up
Version License PHP version Categories
php-validate 1.0.0MIT/X Consortium ...7Data types, Validation, PHP 7
Description 

Author

This is a simple library validate data of several types.

It provides static functions that can take data values of several types and determine if they are valid.

Currently it provides functions to validate arrays, objects, JSON strings, regular string, integer, float number, boolean, IP address, URL, and email address.

Picture of Josantonius
  Performance   Level  
Name: Josantonius is available for providing paid consulting. Contact Josantonius .
Classes: 31 packages by
Country: Spain Spain
Age: ???
All time rank: 132526 in Spain Spain
Week rank: 24 Up3 in Spain Spain Up
Innovation award
Innovation award
Nominee: 11x

Documentation

PHP Validate library

Latest Stable Version Latest Unstable Version License Codacy Badge Total Downloads Travis PSR2 PSR4 codecov

Versión en espaņol

PHP simple library for managing of data types.

Requirements

This library is supported by PHP versions 7.0 or higher and is compatible with HHVM versions 3.0 or higher.

Installation

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:

Download Validate.php:

$ wget https://raw.githubusercontent.com/Josantonius/PHP-Validate/master/src/Validate.php

Available Methods

Available methods in this library:

- Parameter return as array:

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

- Parameter return as object:

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

- Parameter return as JSON:

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

- Parameter return as string:

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

- Parameter return as integer:

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

- Parameter return as float:

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

- Parameter return as boolean:

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

- Parameter return as IP:

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

- Parameter return as URL:

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

- Parameter return as URL:

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

Quick Start

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;

Usage

Example of use for this library:

- ARRAY:

- When an array is passed:

var_dump(Validate::asArray(['foo', 'bar'])); // ['foo', 'bar']

- When an JSON array is passed:

var_dump(Validate::asArray('["foo", "bar"]')); // ['foo', 'bar']

- When an object is passed:

$data = new \StdClass;

$data->foo = 'bar';

var_dump(Validate::asArray($data)); // ['foo' => 'bar']

- When an JSON object is passed:

var_dump(Validate::asArray('{"foo": "bar"}')); // ['foo' => 'bar']

- Parameter return default value when there's no a correct array:

var_dump(Validate::asArray(false)); // null

var_dump(Validate::asArray(false, ['foo', 'bar'])); // ['foo', 'bar']

- OBJECT:

- When an object is passed:

$data = new \StdClass;

$data->foo = 'bar';

$object = Validate::asObject($data);

echo $object->foo; // 'bar'

- When an JSON object is passed:

$object = Validate::asObject('{"foo": "bar"}');

echo $object->foo; // 'bar'

- When an array is passed:

$object = Validate::asObject(['foo' => 'bar']));

echo $object->foo; // 'bar'

- Parameter return default value when there's no a correct object:

var_dump(Validate::asObject(false)); // null

$object = Validate::asObject(false, ['foo' => 'bar']);

echo $object->foo; // 'bar'

- JSON:

- When an JSON object is passed:

echo Validate::asJson('{"foo": "bar"}'); // '{"foo": "bar"}'

- When an array is passed:

echo Validate::asJson(['foo' => 'bar']); // '{"foo":"bar"}'

- When an object is passed:

$data = new \StdClass;

$data->foo = 'bar';

echo Validate::asJson($data); // '{"foo":"bar"}'

- Parameter return default value when there's no a correct JSON:

var_dump(Validate::asJson(false)); // null

echo Validate::asJson(false, '["foo", "bar"]'); // '["foo", "bar"]'

- STRING:

- When an string is passed:

echo Validate::asString('foo'); // 'foo'

- When an integer is passed:

echo Validate::asString(221104); // '221104'

- Parameter return default value when there's no a correct string:

var_dump(Validate::asString(false)); // null

echo Validate::asString(false, 'foo'); // 'foo'

- INTEGER:

- When an integer is passed:

echo Validate::asInteger(8); // 8

- When an string is passed:

echo Validate::asInteger('8'); // 8

- Parameter return default value when there's no a correct integer:

var_dump(Validate::asInteger(false)); // null

echo Validate::asInteger(false, 8); // 8

- FLOAT:

- When an float is passed:

echo Validate::asFloat(8.8); // 8.8

- When an string is passed:

echo Validate::asFloat('8.8'); // 8.8

- Parameter return default value when there's no a correct float:

var_dump(Validate::asFloat(false)); // null

echo Validate::asFloat(false, 8.8); // 8.8

- BOOLEAN:

- When an boolean true is passed:

var_dump(Validate::asBoolean(true)); // true

- When an string true is passed:

var_dump(Validate::asBoolean('true')); // true

- When an integer one is passed:

var_dump(Validate::asBoolean(1)); // true

- When an string one is passed:

var_dump(Validate::asBoolean('1')); // true

- When an boolean false is passed:

var_dump(Validate::asBoolean(false)); // false

- When an string false is passed:

var_dump(Validate::asBoolean('false')); // false

- When an integer zero is passed:

var_dump(Validate::asBoolean(0)); // false

- When an string zero is passed:

var_dump(Validate::asBoolean('0')); // false

- Parameter return default value when there's no a correct boolean:

var_dump(Validate::asBoolean(null)); // null

echo Validate::asBoolean(null, true); // true

- IP:

- When an IP is passed:

echo Validate::asIp('255.255.255.0'); // '255.255.255.0'

- Parameter return default value when there's no a correct IP:

var_dump(Validate::asIp(null)); // null

echo Validate::asIp(null, '255.255.255.0'); // '255.255.255.0'

- URL:

- When an URL is passed:

echo Validate::asUrl('https://josantonius.com'); // 'https://josantonius.com'

- Parameter return default value when there's no a correct URL:

var_dump(Validate::asUrl(null)); // null

echo Validate::asUrl(null, 'https://josantonius.com'); // 'https://josantonius.com'

- Email:

- When an email is passed:

echo Validate::asEmail('hello@josantonius.com'); // 'hello@josantonius.com'

- Parameter return default value when there's no a correct email:

var_dump(Validate::asEmail(null)); // null

echo Validate::asEmail(null, 'hello@josantonius.com'); // 'hello@josantonius.com'

Tests

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

? TODO

  • [ ] Add new feature.
  • [ ] Improve tests.
  • [ ] Improve documentation.

Contribute

If you would like to help, please take a look at the list of issues or the To Do checklist.

Pull requests

  • Fork and clone.
  • Run the command `composer install` to install the dependencies. This will also install the dev dependencies.
  • Run the command `composer fix` to excute code standard fixers.
  • Run the tests.
  • Create a branch, commit, push and send me a pull request.

Repository

The file structure from this repository was created with PHP-Skeleton.

License

This project is licensed under MIT license. See the LICENSE file for more info.

Copyright

2018 Josantonius, josantonius.com

If you find it useful, let me know :wink:

You can contact me on Twitter or through my email.


  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php_cs.dist Example Example script
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONDUCT.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpmd.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README-ES.md Doc. Documentation
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Validate.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file ValidateTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:128
This week:1
All time:9,368
This week:560Up