1. Go to this page and download the library: Download amsify42/php-typestruct library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
/**
* 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);
/**
* 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);
/**
* Instead of setting typestruct class name, we can also set direct path of that typestruct file
*/
protected $tsPath;
/**
* This will decide whether validation will stop at first error itself or when completing all validation errors. Default is true
*/
protected $validateFull;
/**
* You can set to json or xml, default is empty string
*/
protected $contentType;
/**
* Tells the typestruct whether the data we setting/passing is of type Object(stdClass)
*/
protected $isDataObject;