2017 © Pedro Peláez
 

library phpdt

PHP data type validation: primitives (int, double, bool, array, string) and objects

image

stingus/phpdt

PHP data type validation: primitives (int, double, bool, array, string) and objects

  • Friday, October 21, 2016
  • by stingus
  • Repository
  • 1 Watchers
  • 0 Stars
  • 27 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PHPdt

This library implements strict data type validation for PHP., (*1)

Build Status Code Climate Test Coverage, (*2)

Supported data types

  • Primitives: integer, double, boolean, string, array
  • User-defined classes and interfaces

Installation

Install via composer:, (*3)

php composer.phar require stingus/phpdt

Tests

You can run the test suite using:, (*4)

vendor/bin/phpunit

Documentation

You can generate PHPDoc, which will create a phpdoc directory containing HTML API documentation., (*5)

php phpDocumentor.phar

Usage

Primitives

use PHPdt\DataType\Exceptions\InvalidDataTypeException;
use PHPdt\DataType\IntDataType;

class Foo
{
    private $dataType;

    private $data;

    public function __construct($dataType)
    {
        $this->dataType = $dataType;
    }

    // Set data after validating it has the strict type $this->dataType;
    public function setData($data)
    {
        try {
            $dataType = $this->dataType;
            $dataType::validateType($data);
            $this->data = $data;
            return true;
        } catch (InvalidDataTypeException $e) {
            return false;
        }
    }
}

// Build Foo with Integer data validation
// You can choose from 5 predefined primitives: integer, double, string, bool, array
$foo = new Foo(IntDataType::class);
$foo->setData(1); // True
$foo->setData('1'); // False

User-defined objects


use PHPdt\DataType\DataTypeInterface; use PHPdt\DataType\Exceptions\InvalidDataTypeException; // User-defined class must implement DataTypeInterface class Bar implements DataTypeInterface { public static function validateType($value) { if ($value instanceof Bar) { return true; } return false; } } class Foo { private $dataType; private $data; public function __construct($dataType) { $this->dataType = $dataType; } public function setData($data) { // $data is the validator if ($data instanceof DataTypeInterface) { try { $data::validateType($this->dataType); $this->data = $data; return true; } catch (InvalidDataTypeException $e) { return false; } } return false; } } // Build Foo with Bar data validation $foo = new Foo(Bar::class); $foo->setData(new Bar()); // True $foo->setData(new stdClass()); // False

Traits usage

There are two traits that can be used as helpers in user-defined classes:, (*6)

ObjectValidationTrait

This trait implements the validateType() method from DataTypeInterface. It helps building a validation model based on parent classes and interfaces. Suppose you want to validate that an object is implementing a certain interface or is a child of a concrete or abstract class. Using this trait makes validation a breeze:, (*7)


use PHPdt\DataType\DataTypeInterface; use PHPdt\DataType\Exceptions\InvalidDataTypeException; use PHPdt\DataType\ObjectValidationTrait; interface Qux { } abstract class Baz implements Qux, DataTypeInterface { use ObjectValidationTrait; } class Bar extends Baz { } class Foo { private $dataType; private $data; public function __construct($dataType) { $this->dataType = $dataType; } public function setData($data) { // Now $data is the validator if ($data instanceof DataTypeInterface) { try { $data::validateType($this->dataType); $this->data = $data; return true; } catch (InvalidDataTypeException $e) { return false; } } return false; } } // Validation on interface $foo = new Foo(Qux::class); $foo->setData(new Bar()); // True, Bar implements Qux // Validation on abstract $foo = new Foo(Baz::class); $foo->setData(new Bar()); // True, Bar extends Baz

ValidationTypeTrait

This trait helps implementing the validation method in the client objects. You don't have to worry about implementing the correct validation for primitives or used-defined classes, just use the validateType() method from this trait:, (*8)

use PHPdt\DataType\DataTypeInterface;
use PHPdt\DataType\Exceptions\DataTypeImplementationException;
use PHPdt\DataType\Exceptions\InvalidDataTypeException;
use PHPdt\DataType\ObjectValidationTrait;
use PHPdt\DataType\ValidationTypeTrait;

class Baz
{
}

class Bar implements DataTypeInterface
{
    use ObjectValidationTrait;
}

class Foo
{
    use ValidationTypeTrait;

    private $dataType;

    private $data;

    public function __construct($dataType)
    {
        $this->dataType = $dataType;
    }

    public function setData($data)
    {
        try {
            self::validateType($data, $this->dataType);
            $this->data = $data;
            return true;
        } catch (InvalidDataTypeException $e) {
            // Data type mismatch
            return false;
        } catch (DataTypeImplementationException $e) {
            // $dataType (for primitives) or $data (for user-defined classes) is not implementing DataTypeInterface
            return false;
        }
    }
}

$foo = new Foo(Bar::class);
$foo->setData(new Bar()); // True

$foo = new Foo(Baz::class);
$foo->setData(new Baz()); // False, Baz is not implementing DataTypeInterface

The Versions

21/10 2016

dev-master

9999999-dev https://github.com/stingus/phpdt

PHP data type validation: primitives (int, double, bool, array, string) and objects

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Arthur Kerpician

array object string type double boolean integer strict

07/10 2016

0.1.0

0.1.0.0 https://github.com/stingus/phpdt

PHP data type validation: primitives (int, double, bool, array, string) and objects

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Arthur Kerpician

array object string type double boolean integer strict