PHP code example of kartavik / typed-collection

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

    

kartavik / typed-collection example snippets




use kartavik\Support\Collection;
use kartavik\Support\Strict;

$items = [1, 2, 3, 4,];
$collection = new Collection(Strict::integer(), $items); // Return instance of typed collection
$collection = Collection::{'integer'}($items); // Work same as constructor

// Another examples

// string
Collection::{Strict::STRING}(['str1', 'str2']);

// float
Collection::{Strict::FLOAT}([12.3, 23.5, 3., 54.321,]);

// array
Collection::{Strict::ARRAYABLE}([[1, 2], ['str1'], [123.456]]);

// boolean
Collection::{Strict::BOOLEAN}([true, false]);

// object
Collection::{Strict::OBJECT}([new stdClass(), new Exception()]);



use kartavik\Support\Collection;
use kartavik\Support\Strict;

// You can put name of class to static call
// In this case collection can take only stdClass
// It will work with any declared classes
$collection = Collection::{stdClass::class}([]);

// you can also do it with constructor
$collection = new Collection(Strict::object(stdClass::class), []);

// Strict class also support static call for class name
$strict = Strict::{stdClass::class}();
$collection = new Collection($strict, []);



use kartavik\Support;

class StringCollection extends Support\Collection
{
    public function __construct(array $items)
    {
        // do something
        
        parent::__construct(Support\Strict::string(), $items);
    }
}