PHPackages                             twodee/phypes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. twodee/phypes

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

twodee/phypes
=============

Validation components for commonly used data with value objects.

2131[2 issues](https://github.com/2DSharp/Phypes/issues)PHP

Since Oct 29Pushed 6y ago2 watchersCompare

[ Source](https://github.com/2DSharp/Phypes)[ Packagist](https://packagist.org/packages/twodee/phypes)[ RSS](/packages/twodee-phypes/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependenciesVersions (2)Used By (0)

Phypes
======

[](#phypes)

[![Build Status](https://camo.githubusercontent.com/33cc786f5825bdfa9e2441cbe1dd0da41e85cc67b130e7575a680d03ff249b2a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f324453686172702f5068797065732f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/2DSharp/Phypes/build-status/master) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/e6fe320c46aaa8f4c474694b065fca7cc389ed4e5dad65b7aa0f78f4e22853a9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f324453686172702f5068797065732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/2DSharp/Phypes/?branch=master)

A Value Objects library with type validation for PHP.

Introduction
------------

[](#introduction)

Phypes is a fully extensible value objects library with commonly used types that allows for user input validation and data storage.

Phypes has 3 components:

- **Types**
- **Validators**
- **Rules**

A type is an immutable object created from user input and passed on to the services in the business layer to store/manipulate/use the data.

A validator checks the input of each type and throws an `InvalidArgumentException` upon validation failure along with the error message and code describing the error.

A validator is further assisted using "Rules". Rules are helper classes which do validation at an atomic level. Raw data with very simple specifications are processed and validated, for example: You might want to check if the length of a supplied string is more than your limit. A combination of rules define a validator.

A standard set of data types with validators have been provided in the library. For custom validation, user-defined validators can easily be supplied as an argument to the Type.

Installation
------------

[](#installation)

The recommended way to install Phypes is through [composer](https://getcomposer.org/):

```
composer require twodee/phypes

```

To get the latest version from github:

```
git clone git://github.com/2DSharp/phypes.git

```

Usage
-----

[](#usage)

Create entity classes requiring the types as dependencies:

**User.php**

```
class User
{
  private $email;
  private $password;

  public function __construct(Email $email, Password $password) {
    $this->email = $email;
    $this->password = $password;
  }

  public function getEmail() {
    return $this->email;
  }

  public function getPassword() {
    //hash at this point maybe?
    return $this->password;
  }
}
```

Retrieve the data from the browser and store it in their correct types:

**SignUpController.php**

```
try {
  // Get the user data
  $email = new Email($_POST['email']);
  $pass = new Password($_POST['password']);

  // Create a new user account based on the data
  $user = new User($email, $password);

  // Update the user database
  $userMapper = new UserMapper();
  $userMapper->register($user);

} catch(\InvalidArgumentException $e) {
  $this->displayError($e->getMessage());
}
```

If you do not like our validator implementations, you don't have to use them! You can plug in your own validators with custom rules, and Phypes will do the rest for you.

With that said, you need to be extra careful to not inject an irrelevant validator to a type. A password validator to an email type would never make sense. It would in fact change the entire meaning of the email type.

As a rule of thumb, use the bundled types and validators as long as you don't *really*need to make a custom validator for a given type.

To inject a custom validator, implement the `Validator` interface:

**CustomPasswordValidator.php**

```
