PHP code example of rcalicdan / ramsey-collection-typephp-extension

1. Go to this page and download the library: Download rcalicdan/ramsey-collection-typephp-extension 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/ */

    

rcalicdan / ramsey-collection-typephp-extension example snippets


// typephp.php
use Rcalicdan\RamseyExtension\Extension\RamseyCollectionExtension;

return [
    'enabled' => true,

    'extensions' => [
        RamseyCollectionExtension::class,
    ],

    '

use Ramsey\Collection\Collection;
use App\Domain\Dog;
use App\Domain\Car;

/** @var Collection<Dog> $dogs */
$dogs = new Collection(Dog::class);

$dogs->add(new Dog('Rex'));   // Valid
$dogs[] = new Dog('Max');     // Valid (ArrayAccess supported)

$dogs->add(new Car());        // Throws TypePHP\Exception\TypeError

use Ramsey\Collection\Collection;

/** @var Collection<positive-int> $scores */
$scores = new Collection('int');

$scores->add(100);  // Valid
$scores->add(42);   // Valid

$scores->add(-5);   // Throws TypeError: must be of type positive-int, negative int (-5) given
$scores->add(0);    // Throws TypeError: must be of type positive-int, zero int (0) given

use Ramsey\Collection\Collection;

/** @var Collection<array{id: positive-int, name: non-empty-string}> $users */
$users = new Collection('array');

$users->add(['id' => 1, 'name' => 'Alice']); // Valid

// Fails if ID is negative or name is empty:
$users->add(['id' => -1, 'name' => 'Alice']); // Throws TypeError: ['id'] must be of type positive-int
$users->add(['id' => 2, 'name' => '']);       // Throws TypeError: ['name'] must be of type non-empty-string

use Ramsey\Collection\Collection;
use App\Domain\Dog;
use App\Domain\Cat;
use App\Domain\Car;

/** @var Collection<Dog|Cat> $pets */
$pets = new Collection(object::class);

$pets->add(new Dog('Rex'));      // Valid
$pets->add(new Cat('Whiskers')); // Valid

$pets->add(new Car());           // Throws TypeError: must be of type (App\Domain\Dog | App\Domain\Cat)

use Ramsey\Collection\Map\TypedMap;
use App\Domain\Dog;
use App\Domain\Car;

/** @var TypedMap<string, Dog> $map */
$map = new TypedMap('string', Dog::class);

$map->put('alpha', new Dog('AlphaDog')); // Valid
$map['beta'] = new Dog('BetaDog');       // Valid

// Invalid Value
$map->put('gamma', new Car());           // Throws TypeError: must be of type App\Domain\Dog

// Invalid Key (Integer key when string is 

use Ramsey\Collection\DoubleEndedQueue;
use App\Domain\Dog;
use App\Domain\Car;

/** @var DoubleEndedQueue<Dog> $deque */
$deque = new DoubleEndedQueue(Dog::class);

$deque->addFirst(new Dog('FrontDog')); // Valid
$deque->addLast(new Dog('BackDog'));   // Valid

$deque->addFirst(new Car());           // Throws TypeError

namespace App\Collections;

use Ramsey\Collection\AbstractCollection;
use App\Domain\Dog;
use App\Domain\Car;

/**
 * @extends AbstractCollection<Dog>
 */
class DogCollection extends AbstractCollection
{
    public function getType(): string
    {
        return Dog::class;
    }
}

$dogs = new DogCollection();
$dogs->add(new Dog('Rex')); // Valid
$dogs->add(new Car());       // Throws TypeError

use Ramsey\Collection\Collection;
use App\Domain\Dog;
use App\Domain\Car;
use TypePHP\TypePHP;

/** @var Collection<Dog> $dogs */
$dogs = new Collection(Dog::class);

// Inspect the bound generic type at runtime:
echo TypePHP::getGenericType($dogs); // Returns "App\Domain\Dog"

// Clones automatically inherit and isolate their bound generic state:
$cloned = clone $dogs;
$cloned->add(new Dog('ClonedDog')); // Valid
$cloned->add(new Car());            // Throws TypeError