Skip to content

Commit

Permalink
Merge pull request #2 from webcore/feature/redesign-to-value-object-srp
Browse files Browse the repository at this point in the history
Feature/redesign to value object srp
  • Loading branch information
speto committed Jun 10, 2016
2 parents 43007c3 + 717ba83 commit a1763f9
Show file tree
Hide file tree
Showing 45 changed files with 941 additions and 551 deletions.
4 changes: 4 additions & 0 deletions .sensiolabs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global_exclude_dirs:
- vendor
- example
- tests
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ install:
- composer install --no-interaction

script:
- phpunit --coverage-clover=coverage.clover --coverage-text
- composer test-ci

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
- composer upload-code-coverage
120 changes: 81 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
# Validation traits

[![Travis](https://img.shields.io/travis/webcore/validation-traits.svg?maxAge=2592000)](https://travis-ci.org/webcore/validation-traits)
[![SensioLabs Insight](https://img.shields.io/sensiolabs/i/6a166fab-ddf0-4d5c-b605-09f6681f8795.svg?maxAge=2592000)](https://insight.sensiolabs.com/projects/6a166fab-ddf0-4d5c-b605-09f6681f8795)
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/webcore/validation-traits.svg?maxAge=2592000)](https://scrutinizer-ci.com/g/webcore/validation-traits/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/webcore/validation-traits/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/webcore/validation-traits/?branch=master)
[![Dependency Status](https://www.versioneye.com/user/projects/575827ac7757a0004a1de479/badge.svg?style=plastic)](https://www.versioneye.com/user/projects/575827ac7757a0004a1de479)
[![license](https://img.shields.io/github/license/webcore/validation-traits.svg?maxAge=2592000)](https://opensource.org/licenses/MIT)

# 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](https://github.com/dwightwatson/validating) and redesign more towards [nicolopignatelli/valueobjects](https://github.com/nicolopignatelli/valueobjects)

[Version 1.0](https://github.com/webcore/validation-traits/tree/v1.0) has less complex attitude on value objects and provide mainly validation via one trait.

## Installation

Via Composer

```
composer require webcore/validation-traits
```

### Example

Let's define simple token class with 3 rules:
`SingleValueObjectInterface` define getter method to retrieve value and method for comparing with another value objects implementing this interface.

```php
<?php
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);
}
```

use Webcore\Validation\ValidationTrait;
use Webcore\Validation\ValidationValueBoxTrait;
Let's define simple `Token` class with 3 rules:

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

protected function validation($value)
{
Expand All @@ -30,45 +59,58 @@ class Token
}
```

And try to create instance of `Token` with valid and invalid values and compare each other:

```php
//example/example.php
<?php

require_once __DIR__."/../vendor/autoload.php";
require_once __DIR__."/Token.php";
//nette/tester
use Tester\Assert;

//valid value
$value = str_repeat('1234wxyz', 8);
$token = new Token($value);
echo $token->getValue(); // 1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz1234wxyz
$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
try {
$value = null;
$token = new Token($value);
} catch (InvalidArgumentException $e) {
echo $e->getMessage(); //Token must be not empty
}

try {
$value = "InvalidTokenLength123456789";
$token = new Token($value);
} catch (InvalidArgumentException $e) {
echo $e->getMessage(); //Token must be 64 characters long
}

try {
$value = str_repeat('?!@#$%^&', 8);;
$token = new Token($value);
} catch (InvalidArgumentException $e) {
echo $e->getMessage(); //Token must be valid base_64
}

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"
);
```

### TODO

- divide into single traits for each validation

### MIT license

Copyright (c) 2016, Štefan Peťovský
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "^5.4.2"
"phpunit/phpunit": "^5.4.2",
"nette/tester": "^1.7"
},
"autoload": {
"psr-4": {
Expand All @@ -22,5 +23,14 @@
},
"autoload-dev": {
"psr-4": { "Webcore\\Validation\\Tests\\": "tests/" }
},
"scripts": {
"test": "vendor/bin/phpunit --coverage-text",
"test-html": "vendor/bin/phpunit --coverage-html coverage tests",
"test-ci": "vendor/bin/phpunit --coverage-clover=coverage.clover --coverage-text",
"upload-code-coverage": [
"wget https://scrutinizer-ci.com/ocular.phar",
"php ocular.phar code-coverage:upload --format=php-clover coverage.clover"
]
}
}
Loading

0 comments on commit a1763f9

Please sign in to comment.