<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jausions / php-typed-doctrine-collections example snippets
use Abacus11\Doctrine\Collections\CollectionOf;
$int_array = new CollectionOf([1, 2]); // Okay
$int_array = new CollectionOf([1, '2']); // Not okay - throws \TypeError
$int_array = new CollectionOf([null, 1]); // Not okay - throws \InvalidArgumentException
use Abacus11\Doctrine\Collections\CollectionOf;
$sample = 1;
$int_array = (new CollectionOf())->setElementTypeLike($sample);
$int_array[] = 2; // Okay
$int_array[] = true; // Not okay - throws \TypeError exception
class SomeClass {}
$sample = new SomeClass();
$some = (new CollectionOf())->setElementTypeLike($sample);
$some[] = new SomeClass(); // Okay
$some[] = new stdClass(); // Not okay - throws \TypeError exception
use Abacus11\Doctrine\Collections\CollectionOf;
// Use the setElementType() method
$positive_int = (new CollectionOf())->setElementType(function ($value) {
if (!is_integer($value)) {
return false;
}
return ($value >= 0);
});
$positive_int['apples'] = 0; // Okay
$positive_int['oranges'] = 10; // Okay
$positive_int['bananas'] = -5; // Not okay - throws \TypeError exception
// Or directly in the constructor
$negative_int = new CollectionOf(
function ($value) {
if (!is_integer($value)) {
return false;
}
return ($value <= 0);
}
);
$negative_int[] = -50; // Okay
$negative_int[] = 5; // Not okay - throws \TypeError exception
use Abacus11\Doctrine\Collections\CollectionOf;
class A {}
class B {}
class AA extends A {}
// Use the setElementType() method
$some_a = (new CollectionOf())->setElementType(A::class);
$some_a[] = new A(); // Okay
$some_a[] = new AA(); // Okay
$some_a[] = new B(); // Not okay - throws \TypeError exception
// Or directly in the constructor
$some_b = new CollectionOf(B::class);
$some_b[] = new B(); // Okay
$some_b[] = new A(); // Not okay - throws \TypeError exception
use Abacus11\Doctrine\Collections\CollectionOf;
// Use the setElementType() method
$int_array = (new CollectionOf())->setElementType('integer');
$int_array[] = 1; // Okay
$int_array[] = '1'; // Not okay - throws \TypeError exception
// Or directly in the constructor
$int_array = new CollectionOf('integer');
$int_array[] = 20; // Okay
$int_array[] = true; // Not okay - throws \TypeError exception
$integers = new \Abacus11\Doctrine\Collections\Integers([1, 2, 3, 0, -1]);
use Abacus11\Doctrine\Collections\CollectionOf;
class Vehicle
{
}
class Car extends Vehicle
{
public $make;
public $model;
public $color;
public $license_plate_number;
}
class Submarine extends Vehicle
{
public $name;
}
class Cars extends CollectionOf
{
/**
* @param Car[] $cars
*/
public function __construct(array $cars = []) {
parent::__construct(Car::class, $cars);
// - or -
//$this->setElementType(Car::class);
//parent::__construct($cars);
}
}
class Parking
{
/**
* @var Cars
*/
protected $lot;
public function __construct()
{
$this->lot = new Cars([]);
}
public function enter(Vehicle $car)
{
$this->lot[] = $car;
}
/**
* @return Car[] The collection of cars
*/
public function getCars(): Cars
{
return $this->lot;
}
//...
}
$my_car = new Car();
$my_car->model = 'T';
$my_car->make = 'Ford';
$my_car->color = 'Black';
$my_car->license_plate_number = 'MI-01234';
$my_sub = new Submarine();
$my_sub->name = 'Nautilus';
$parking = new Parking();
$parking->enter($my_car); // Okay
$parking->enter($my_sub); // Not okay - throws \TypeError exception
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.