1. Go to this page and download the library: Download da2e/generic-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/ */
da2e / generic-collection example snippets
$a = [1, 2, new \stdClass(), function () { return 'foo'; }];
$r = 0;
foreach ($a as $v) {
$r += $v; // here for stdClass and a closure you will get an error: Object of class ... could not be converted to int.
}
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\CustomType;
$collection = new GCollection(new CustomType(function ($value) {
return $value === 'foobar';
}));
$collection[] = 'foobar'; // valid
$collection[] = 'bar'; // invalid
use DA2E\GenericCollection\Type\GCollectionType;
use DA2E\GenericCollection\Type\StringType;
use DA2E\GenericCollection\GCollection;
$collection = new GCollection(new GCollectionType());
$collection[] = new GCollection(new StringType()); // valid
$collection[] = new StringType(); // invalid
$value = new Some\Class\Here();
$type = new DA2E\GenericCollection\Type\ObjectType('Some\Class\Here');
$type->validate($value);
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\StringType;
use DA2E\GenericCollection\Exception\InvalidTypeException;
try {
$collection = new GCollection(new StringType()); // You can pass an array as 2nd argument as well.
$collection[] = 'string'; // this one is fine
$collection[] = 1; // this one will throw an exception
} catch (InvalidTypeException $e) {
// handle exception
}
function foobar(GCollection $collection) {
try {
$collection->elementsShouldBeTypeOf(StringType::class); // If something is wrong InvalidTypeException is thrown
} catch (InvalidTypeException $e) {
// handle exception
}
// ...
}
function foobar(GCollection $collection) {
try {
$collection->elementsShouldBeTypeOf(ObjectType::class); // This is fine and valid but does not give a lot of information which exactly object is there.
$collection->elementsShouldBeTypeOf(YourAnyClass::class); // This is more verbose and clear. We know that we expect a concrete YourAnyClass objects.
} catch (InvalidTypeException $e) {
// handle exception
}
// ...
}
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\StringType;
$collection = new GCollection(new StringType(), ['a' ,'b']);
$collection->map(function ($item) {
return $item . '2';
}); // Returns [ 'a2', 'b2', 'c2' ]
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\StringType;
$collection = new GCollection(new StringType(), ['a' ,'b']);
$collection->filter(function ($item) {
return $item === 'b';
}); // Returns [ 'b' ]
use DA2E\GenericCollection\GCollection;
use DA2E\GenericCollection\Type\StringType;
$collection = new GCollection(new StringType(), ['a' ,'b']);
$collection->sort(function (array $items) {
rsort($items); // Here you can call any sort function you wish.
return $items;
}); // Returns [ 'b', 'a' ]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.