1. Go to this page and download the library: Download voku/arrayy 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/ */
/**
* @property int $id
* @property string $firstName
* @property string $lastName
*
* @extends \Arrayy\Arrayy<array-key,mixed>
*/
class User extends \Arrayy\Arrayy
{
protected $checkPropertyTypes = true;
protected $checkPropertiesMismatchInConstructor = true;
}
/**
* @template TKey of array-key
* @extends AbstractCollection<TKey,User>
*/
class UserCollection extends \Arrayy\Collection\AbstractCollection
{
/**
* The type (FQCN) associated with this collection.
*
* @return string
*/
public function getType()
{
return User::class;
}
}
$m = User::meta();
$data = static function () use ($m) {
yield new User([$m->id => 40, $m->firstName => 'Foo', $m->lastName => 'Moelleken']);
yield new User([$m->id => 30, $m->firstName => 'Sven', $m->lastName => 'Moelleken']);
yield new User([$m->id => 20, $m->firstName => 'Lars', $m->lastName => 'Moelleken']);
yield new User([$m->id => 10, $m->firstName => 'Lea', $m->lastName => 'Moelleken']);
};
$users = UserCollection::createFromGeneratorFunction($data);
$names = $users
->filter(static function (User $user): bool {
return $user->id <= 30;
})
->customSortValuesImmutable(static function (User $a, User $b): int {
return $a->firstName <=> $b->firstName;
})
->map(static function (User $user): string {
return $user->firstName;
})
->implode(';');
static::assertSame('Lars;Lea;Sven', $names);
use Arrayy\Collection\AbstractCollection;
/**
* @extends AbstractCollection<array-key,YOURInterface>
*/
class YOURCollection extends AbstractCollection
{
/**
* The type (FQCN) associated with this collection.
*
* @return string
*/
public function getType(): string
{
return YOURInterface::class;
}
}
$YOURobject1 = new YOURClass();
$YOURobject2 = new YOURClass();
$YOURcollection = new YOURCollection($YOURobject1);
$YOURcollection->add($YOURobject2); // add one more object
// Or, you can use an array of objects.
//
// $YOURcollection = new YOURCollection([$YOURobject1, $YOURobject2]);
// Or, if you don't want to create new classes ...
// ... and you don't need typehints and autocompletion via classes.
//
// $YOURcollection = \Arrayy\Collection::construct(YOURInterface::class, [$YOURobject1]);
// $YOURcollection->add($YOURobject2); // add one more object
// Or, if you don't like classes at all. ;-)
//
// $YOURcollection = \Arrayy\collection(YOURInterface::class, [$YOURobject1]);
// $YOURcollection->add($YOURobject2); // add one more object
foreach ($YOURcollection as $YOURobject) {
if ($YOURobject instanceof YOURInterface) {
// Do something with $YOURobject
}
}
use Arrayy\Type\StringCollection;
$collection = new StringCollection(['A', 'B', 'C', 1]);
use Arrayy\Type\IntCollection;
use Arrayy\Type\StringCollection;
use Arrayy\Type\InstancesCollection;
use Arrayy\Type\TypeInterface;
$collection = InstancesCollection::construct(
TypeInterface::class,
[new StringCollection(['A', 'B', 'C']), new IntCollection([1])]
);
$collection->toArray(true); // [['A', 'B', 'C'], [1]]
namespace Arrayy\tests\Collection;
use Arrayy\Collection\AbstractCollection;
/**
* @extends AbstractCollection<array-key,\Arrayy\tests\UserData>
*/
class UserDataCollection extends AbstractCollection
{
/**
* The type (FQCN) associated with this collection.
*
* @return string
*/
public function getType()
{
return \Arrayy\tests\UserData::class;
}
}
$json = '[{"id":1,"firstName":"Lars","lastName":"Moelleken","city":{"name":"Düsseldorf","plz":null,"infos":["lall"]}}, {"id":1,"firstName":"Sven","lastName":"Moelleken","city":{"name":"Köln","plz":null,"infos":["foo"]}}]';
$userDataCollection = UserDataCollection::createFromJsonMapper($json);
/** @var \Arrayy\tests\UserData[] $userDatas */
$userDataCollection->getAll();
$userData0 = $userDataCollection[0];
echo $userData0->firstName; // 'Lars'
$userData0->city; // CityData::class
echo $userData0->city->name; // 'Düsseldorf'
$userData1 = $userDataCollection[1];
echo $userData1->firstName; // 'Sven'
$userData1->city; // CityData::class
echo $userData1->city->name; // 'Köln'
$arrayy = new Arrayy(array('fòô', 'bàř')); // Arrayy['fòô', 'bàř']
php
use function Arrayy\create as a;
// Instead of: A::create(['fòô', 'bàř'])->reverse()->implode();
a(['fòô', 'bàř'])->reverse()->implode(','); // 'bàř,fòô'
/tests/CollectionTest.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.