PHP code example of jausions / php-typed-collections

1. Go to this page and download the library: Download jausions/php-typed-collections 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/ */

    

jausions / php-typed-collections example snippets



use Abacus11\Collections\{
    TypedCollection,
    TypedArrayAccessTrait
};

class ArrayOf implements \ArrayAccess, TypedCollection
{
    use TypedArrayAccessTrait;
}


// With the ArrayOf class defined above

$sample = 1;
$int_array = (new ArrayOf())->setElementTypeLike($sample);

$int_array[] = 2;           // Okay
$int_array[] = true;        // Not okay - throws \TypeError exception

class SomeClass {}

$sample = new SomeClass();
$some = (new ArrayOf())->setElementTypeLike($sample);

$some[] = new SomeClass();  // Okay
$some[] = new stdClass();   // Not okay - throws \TypeError exception


// With the ArrayOf class defined above

$positive_int = (new ArrayOf())->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 a \TypeError exception


// With the ArrayOf class defined above

class A {}

class B {}

class AA extends A {}

$some_a = (new ArrayOf())->setElementType(A::class);

$some_a[] = new A();    // Okay
$some_a[] = new AA();   // Okay
$some_a[] = new B();    // Not okay - throws \TypeError exception


// With the ArrayOf class defined above

$int_array = (new ArrayOf())->setElementType('integer');

$int_array[] = 1;       // Okay
$int_array[] = '1';     // Not okay - throws \TypeError exception


// With the ArrayOf class defined above

$collection = (new ArrayOf())->setElementType('integer');

$value = 'abc';
if ($collection->isElementType($value)) {
    // Do something
}


class Vehicle
{
}

class Car extends Vehicle
{
    public $make;
    public $model;
    public $color;
    public $license_plate_number;
}

class Submarine extends Vehicle
{
    public $name;
}

// With the ArrayOf class defined above
class Cars extends ArrayOf
{
    public function __construct() {
        $this->setElementType(Car::class);
    }
}

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