1. Go to this page and download the library: Download sabloger/php-strict 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/ */
sabloger / php-strict example snippets
use Php_Strict\BaseObject;
/**
* Class Book
* @package Php_Strict
* @method Book setTitle(string $value)
* @method Book setAuthor(string $value)
* @method Book setYear(int $value)
* @method Book setSome_Object(Object $value)
* @method string getTitle()
* @method string getAuthor()
* @method int getYear()
* @method Object getSome_Object()
*/
class Book extends BaseObject
{
/**
* @return array
*/
public function getFieldsStub()
{
return [
'title' => 'string', // All of types as you want!!
'author' => 'string',
'year' => 'int',
'some_object' => Object::class
];
}
/**
* @return array
*/
public function getRequiredFields()
{
return [
'title',
'year'
];
}
/**
* If you want to allow setting undefined properties, set it True!
* @return bool
*/
protected function isUndefinedSettingAllowed()
{
return false;
}
}
$book = new Book(["Title" => "Advanced PHP Programming"]); // Its case-insensitive and auto case-correcter!! :)
$book->setAuthor("George Schlossnagle");
$book["Year"] = 2004;
$obj = new Object();
$obj->Foo = "bar";
$obj["foO"] = "BAR"; // Yessss its case-insensitive and auto case-correcter!! ["Foo": "BAR"]
$book->set("some_object",$obj); // Several access methods!! Use these as you want!!
print $book; // It's echoable!!
//$book->toJson(); // Jsonable :)
//$book->toArray(); // Arrayable :)
foreach ($book as $key => $value) { // Iterateable :)
echo "key: $key , value: $value \n";
}
/*
Out:
{"title":"Advanced PHP Programming","author":"George Schlossnagle","year":2004,"some_object":{"Foo":"BAR"}}
key: title , value: Advanced PHP Programming
key: author , value: George Schlossnagle
key: year , value: 2004
key: some_object , value: {"Foo":"BAR"}
*/
//Scalar:
$strArr = new ArrayList('string');
$strArr[] = "correct!";
$strArr[] = 123; //PHP Fatal error: Uncaught exception 'Php_Strict\Exceptions\InvalidItemTypeException' with message 'Invalid item type exception: Expected type was "string" given "integer"!'
print $strArr->toJson();
//Object:
$obj = new Object();
$obj->Foo = "bar";
$obj["foO"] = "BAR";
$arr = new ArrayList(Object::class);
//$arr[0] = 11; //PHP Fatal error: Uncaught exception 'Php_Strict\Exceptions\InvalidItemTypeException' with message 'Invalid item type exception: Expected type was "Php_Strict\Object" given "integer"!'
$arr[0] = $obj;
$obj->foo = "bar";
$arr[1] = $obj;
$arr[0]->baz = "bag";
$arr[2] = $obj;
print $arr . "\n";
print $obj; // Its by-reference!
use Php_Strict\ArrayList;
class BookArrayList extends ArrayList
{
/**
* @param array $items
* @return BookArrayList
*/
public static function newLib(array $items = [])
{
return (new self($items));
}
/**
* BookArrayList constructor.
* @param array $items
*/
public function __construct(array $items = [])
{
parent::__construct(Book::class, $items);
}
/**
* Validate all of items using Object->validate() and ArrayList each()
* @throws \Exception
* @return null|true
*/
public function validate()
{
parent::each(function (Book $item, $key) { // ArrayList has each(closure) method! :)
try {
$item->validate();
} catch (\Exception $exception) {
throw new \Exception(sprintf('ArrayList validation failed at offset (%s) with message: %s' , $key, $exception->getMessage()));
}
});
return true;
}
}
$book = new Book(["Title" => "Advanced PHP Programming"]);
$book->setAuthor("George Schlossnagle");
$bookArr = BookArrayList::newLib();
$bookArr[] = $book;
$bookArr->validate(); // PHP Fatal error: Uncaught exception 'Exception' with message 'ArrayList validation failed at offset (0) with message: Required fields are not filled. unfilled
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.