2017 © Pedro Peláez
 

library validation-traits

Simple validation library

image

webcore/validation-traits

Simple validation library

  • Friday, June 10, 2016
  • by chimneysweep13
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Travis SensioLabs Insight Scrutinizer Code Quality Code Coverage Dependency Status license, (*1)

Validation traits

A PHP library/collection of traits aimed to simplify cration of immutable value objects with validation of input value. Basic idea was also inspired by Laravel's Validation trait for Eloquent models and redesign more towards nicolopignatelli/valueobjects, (*2)

Version 1.0 has less complex attitude on value objects and provide mainly validation via one trait., (*3)

Installation

Via Composer, (*4)

composer require webcore/validation-traits

Example

SingleValueObjectInterface define getter method to retrieve value and method for comparing with another value objects implementing this interface., (*5)

interface SingleValueObjectInterface
{
    /**
     * @return mixed
     */
    public function getValue();

    /**
     * Compare two SingleValueObject and tells whether they can be considered equal
     *
     * @param  SingleValueObjectInterface $object
     * @return bool
     */
    public function sameValueAs(SingleValueObjectInterface $object);
}

Let's define simple Token class with 3 rules:, (*6)

//example/Token.php
<?php
class Token implements SingleValueObjectInterface
{
    use SingleValueObjectTrait, NotEmptyTrait, Base64Trait, LengthTrait;

    protected function validation($value)
    {
        $this->validateNotEmpty($value);
        $this->validateBase64($value);
        $this->validateLength($value, 64);
    }
}

And try to create instance of Token with valid and invalid values and compare each other:, (*7)

//example/example.php
<?php
//nette/tester
use Tester\Assert;

//valid value
$value = str_repeat('BeerNowThere0sATemporarySolution', 2);
$tokenFoo = new Token($value);
Assert::same("BeerNowThere0sATemporarySolutionBeerNowThere0sATemporarySolution", $tokenFoo->getValue());

//compare with another object of same value
$tokenBar = new Token("BeerNowThere0sATemporarySolutionBeerNowThere0sATemporarySolution");
$areSame = $tokenBar->sameValueAs($tokenFoo);
Assert::true($areSame);

//compare with another object of different value
$value = str_repeat('CraftBeerLovers0', 4); //
$tokenPub = new Token($value);
Assert::same("CraftBeerLovers0CraftBeerLovers0CraftBeerLovers0CraftBeerLovers0", $tokenPub->getValue());
$areSame = $tokenPub->sameValueAs($tokenBar);
Assert::false($areSame);

//invalid values
Assert::exception(
    function () {
        new Token(null);
    },
    InvalidArgumentException::class,
    "Token must be not empty"
);

Assert::exception(
    function () {
        new Token("InvalidTokenLength123456789");
    },
    InvalidArgumentException::class,
    "Token must be 64 characters long"
);

Assert::exception(
    function () {
        $value = str_repeat('?!@#$%^&', 8);
        new Token($value);
    },
    InvalidArgumentException::class,
    "Token must be valid base_64"
);

MIT license

Copyright (c) 2016, Štefan Peťovský, (*8)

The Versions

10/06 2016

dev-master

9999999-dev

Simple validation library

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Štefan Peťovský

10/06 2016

v2.0.0

2.0.0.0

Simple validation library

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Štefan Peťovský

08/06 2016

v1.1.0

1.1.0.0

Simple validation library

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Štefan Peťovský

08/06 2016

v1.0.1

1.0.1.0

Simple validation library

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Štefan Peťovský

08/06 2016

v1.0

1.0.0.0

Simple validation library

  Sources   Download

MIT

The Development Requires

by Štefan Peťovský