1. Go to this page and download the library: Download stingus/phpdt 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/ */
stingus / phpdt example snippets
use PHPdt\DataType\Exceptions\InvalidDataTypeException;
use PHPdt\DataType\IntDataType;
class Foo
{
private $dataType;
private $data;
public function __construct($dataType)
{
$this->dataType = $dataType;
}
// Set data after validating it has the strict type $this->dataType;
public function setData($data)
{
try {
$dataType = $this->dataType;
$dataType::validateType($data);
$this->data = $data;
return true;
} catch (InvalidDataTypeException $e) {
return false;
}
}
}
// Build Foo with Integer data validation
// You can choose from 5 predefined primitives: integer, double, string, bool, array
$foo = new Foo(IntDataType::class);
$foo->setData(1); // True
$foo->setData('1'); // False
use PHPdt\DataType\DataTypeInterface;
use PHPdt\DataType\Exceptions\InvalidDataTypeException;
// User-defined class must implement DataTypeInterface
class Bar implements DataTypeInterface
{
public static function validateType($value)
{
if ($value instanceof Bar) {
return true;
}
return false;
}
}
class Foo
{
private $dataType;
private $data;
public function __construct($dataType)
{
$this->dataType = $dataType;
}
public function setData($data)
{
// $data is the validator
if ($data instanceof DataTypeInterface) {
try {
$data::validateType($this->dataType);
$this->data = $data;
return true;
} catch (InvalidDataTypeException $e) {
return false;
}
}
return false;
}
}
// Build Foo with Bar data validation
$foo = new Foo(Bar::class);
$foo->setData(new Bar()); // True
$foo->setData(new stdClass()); // False
use PHPdt\DataType\DataTypeInterface;
use PHPdt\DataType\Exceptions\InvalidDataTypeException;
use PHPdt\DataType\ObjectValidationTrait;
interface Qux
{
}
abstract class Baz implements Qux, DataTypeInterface
{
use ObjectValidationTrait;
}
class Bar extends Baz
{
}
class Foo
{
private $dataType;
private $data;
public function __construct($dataType)
{
$this->dataType = $dataType;
}
public function setData($data)
{
// Now $data is the validator
if ($data instanceof DataTypeInterface) {
try {
$data::validateType($this->dataType);
$this->data = $data;
return true;
} catch (InvalidDataTypeException $e) {
return false;
}
}
return false;
}
}
// Validation on interface
$foo = new Foo(Qux::class);
$foo->setData(new Bar()); // True, Bar implements Qux
// Validation on abstract
$foo = new Foo(Baz::class);
$foo->setData(new Bar()); // True, Bar extends Baz
use PHPdt\DataType\DataTypeInterface;
use PHPdt\DataType\Exceptions\DataTypeImplementationException;
use PHPdt\DataType\Exceptions\InvalidDataTypeException;
use PHPdt\DataType\ObjectValidationTrait;
use PHPdt\DataType\ValidationTypeTrait;
class Baz
{
}
class Bar implements DataTypeInterface
{
use ObjectValidationTrait;
}
class Foo
{
use ValidationTypeTrait;
private $dataType;
private $data;
public function __construct($dataType)
{
$this->dataType = $dataType;
}
public function setData($data)
{
try {
self::validateType($data, $this->dataType);
$this->data = $data;
return true;
} catch (InvalidDataTypeException $e) {
// Data type mismatch
return false;
} catch (DataTypeImplementationException $e) {
// $dataType (for primitives) or $data (for user-defined classes) is not implementing DataTypeInterface
return false;
}
}
}
$foo = new Foo(Bar::class);
$foo->setData(new Bar()); // True
$foo = new Foo(Baz::class);
$foo->setData(new Baz()); // False, Baz is not implementing DataTypeInterface
sh
php phpDocumentor.phar
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.