Download the PHP package amsify42/php-typestruct without Composer

On this page you can find all versions of the php package amsify42/php-typestruct. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-typestruct

PHP TypeStruct

PHP package for validating data against the structure defined.

Installation

Table of Contents

  1. Introduction
  2. Validation
  3. Data
  4. Class Validation
  5. Rules
  6. Custom Rules
  7. Complex Example

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

Let's say we have data in array format.

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

Notice that the structure we defined does not completely look like a PHP syntax but it will work as a structure against 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

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

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

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

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

3. Data

The data you can pass for validation are

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

Object(stdClass)

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

Json

XML

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

4. Class Validation

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

Since we already set TypeStruct class name and data in protected properties. We can create instance of this class directly and validate

You can also set the data before validating against the typestruct like this

and we can also use these protected properties which extends Amsify42\TypeStruct\Validator

5. Rules

Basic

These are the basic types we can use for elements. It will check whether key exist and its type.

numeric work just like php is_numeric() method which allows numbers even in quotes. tinyInt expects the value to be either 0 or 1 and type any means that element value could be of any type.

Optional

To make the element optional, we simply prefix it with question mark ?

Optional can also be applied to child dictionary

Length

We can also set the limits to the length of these types like this

Array

These are the array types we can use

External as Child

We can also use the other external TypeStruct file as a element

Now we can use Category as type like this

or as array of this type

More Rules

You can also attach more rules to the input like this

As you can see, we have added rule email to the email element which will check for valid email address. You can add more rules to the element separated by dot . like this

These are the pre defined rules you can use

6. Custom Rules

We can also write method to perform cutom validation but this can only be achieved when we create class and extends it to Amsify42\TypeStruct\Validator

Now we can write method checkName in our validator class like this

We can use $this->name() to get the name of current element and $this->value() to get the value of the current element which is applicable to the rule. To get the other element value, we already have $this->data accessible from these custom rule methods.
If you want to access data from custom method more easily, you can also use the method $this->path() which will directly get the element from multi level path.

Note: $this->path expects parameters to be key name separated by dot(if multiple keys) and will either return NULL(if key does not exist) or the target key value.

7. Complex Example

The above complex and multi level typestruct example file will be validated with the data:


All versions of php-typestruct with dependencies

PHP Build Version
Package Version
Requires php Version >=7.0.0
amsify42/php-vars-data Version dev-master
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package amsify42/php-typestruct contains the following files

Loading the files please wait ....