PHPackages                             amsify42/php-typestruct - 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. amsify42/php-typestruct

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

amsify42/php-typestruct
=======================

PHP package for validating data against the structure defined

1.1(3y ago)13411MITPHPPHP &gt;=7.0.0CI failing

Since Oct 27Pushed 3y agoCompare

[ Source](https://github.com/amsify42/php-typestruct)[ Packagist](https://packagist.org/packages/amsify42/php-typestruct)[ RSS](/packages/amsify42-php-typestruct/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependencies (2)Versions (3)Used By (1)

PHP TypeStruct
==============

[](#php-typestruct)

PHP package for validating data against the structure defined.

### Installation

[](#installation)

```
$ composer require amsify42/php-typestruct

```

Table of Contents
-----------------

[](#table-of-contents)

1. [Introduction](#1-introduction)
2. [Validation](#2-validation)
3. [Data](#3-data)
4. [Class Validation](#4-class-validation)
5. [Rules](#5-rules)
6. [Custom Rules](#6-custom-rules)
7. [Complex Example](#7-complex-example)

### 1. Introduction

[](#1-introduction)

The purpose of this php package is to make validation easy and the structure defined for validation should be readable. The data passed can be validated against the structure defined.

### 2. Validation

[](#2-validation)

Let's say we have data in array format.

```
$data = [
    'id' => 42,
    'name' => 'amsify',
    'price' => 4.2
];
```

and we want this to be strictly validated. Now we can define the structure against which this data to be validated.

```
namespace App\TypeStruct;

export typestruct Simple {
    id: int,
    name: string,
    price: float
}
```

Notice that the structure we defined does not completely look like a PHP syntax but it will work as a structure against data.

```
$data = [
    'id' => 42,
    'name' => 'amsify',
    'price' => 4.2
];
$typeStruct = new Amsify42\TypeStruct\TypeStruct();
$typeStruct->setClass(App\TypeStruct\Simple::class);
$result = $typeStruct->validate($data);
```

Note we are creating new instance of `Amsify42\TypeStruct\TypeStruct`, passing the full class name of typestruct `App\TypeStruct\Simple` and pasing data to **validate()** method.

The validate method will return with the info whether the data passed is validated against the structure and it will return

```
array(2) {
  ["is_validated"]=>
  bool(true)
  ["messages"]=>
  array(0) {
  }
}
```

The `is_validated` will have `true` or `false` based on whether data is validated or not and `messages` will have error messages in hierarchy based on elements which are not validated.

#### Helper method

[](#helper-method)

We can also use helper method to get the `Amsify42\TypeStruct\TypeStruct` new instance.

```
/**
 * If we have direct of the typestruct file
 */
$typeStruct = get_typestruct('/path/to/Simple.php');
$result = $typeStruct->validate($data);
```

```
/**
 * For class, we need to pass full class name and 2nd param as 'class'
 */
$typeStruct = get_typestruct(App\TypeStruct\Simple::class, 'class');
$result = $typeStruct->validate($data);
```

#### Autoloading

[](#autoloading)

Autoloading of the typestruct file will be done automatically if its name and path is based on *psr-4* standards else you need to use `setPath()` method with typestruct instance which expects direct path of the typestruct file.

#### Options

[](#options)

With Typestruct instance we can set these options before calling `validate()` method

```
/**
 * To tell the typestruct that data we are passing is of type object(stdClass)
 * default is false
 */
$typeStruct->isDataObject(true);
/**
 * If true, it will validate and collect all error messages else it will get the first error and exit
 * Default is true
 */
$typeStruct->validateFull(false);
/**
 * Default is empty string, you can either pass 'json' or 'xml' based on the type of data you are passing for validation.
 */
$typeStruct->contentType('json');
/**
 * Absolute path to the typestruct file
 */
$typeStruct->setPath('/path/to/Sample.php');
/**
 * Full class name of typestruct file
 */
$typeStruct->setClass(App\TypeStruct\Simple::class);
```

### 3. Data

[](#3-data)

The data you can pass for validation are

```
Array
Object(stdClass)
Json
XML
```

As we have already seen the array example, lets see the examples for the rest

#### Object(stdClass)

[](#objectstdclass)

```
$data        = new \stdClass();
$data->id    = 42;
$data->name  = 'amsify';
$data->price = 4.2;

$typeStruct = new Amsify42\TypeStruct\TypeStruct();
$typeStruct->isDataObject(true)->setClass(App\TypeStruct\Simple::class);
$result = $typeStruct->validate($data);
```

**Note:** We are passing `true` to method `isDataObject()` to tell **TypeStruct** that the data we are passing is of type **Object(stdClass)**.

#### Json

[](#json)

```
$jsonData = '{"id":42,"name":"amsify","price":4.2}';
$typeStruct = new TypeStruct();
$typeStruct->contentType('json')->setClass(App\TypeStruct\Simple::class);
$result = $typeStruct->validate($jsonData);
```

#### XML

[](#xml)

```
$xmlData = '  42 amsify 4.2 ';
$typeStruct = new TypeStruct();
$typeStruct->contentType('xml')->setClass(App\TypeStruct\Simple::class);
$result = $typeStruct->validate($xmlData);
```

**Note:** We are calling **contentType()** method to set its type for both `Json` and `XML`.

### 4. Class Validation

[](#4-class-validation)

We can do the validation by creating class and extending it to the `Amsify42\TypeStruct\Validator`

```
