PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Josantonius   PHP Validate   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Validate
Validate data of several types
Author: By
Last change:
Date: 5 years ago
Size: 13,141 bytes
 

Contents

Class file image Download

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.