1. Go to this page and download the library: Download micoli/multitude 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/ */
declare(strict_types=1);
namespace Micoli\Multitude\Tests\Fixtures;
class Project
{
public function __construct(
public readonly int $value,
public readonly Tags $tags,
) {
}
}
declare(strict_types=1);
namespace Micoli\Multitude\Tests\Fixtures;
use Micoli\Multitude\Set\ImmutableSet;
/**
* @extends ImmutableSet<string>
*/
class Tags extends ImmutableSet
{
}
declare(strict_types=1);
namespace Micoli\Multitude\Tests\Fixtures;
use Micoli\Multitude\Map\ImmutableMap;
/**
* @extends ImmutableMap<string, Project>
*/
class Projects extends ImmutableMap
{
/**
* Add or replace a value in the map
*/
public function improvedSet(string $newKey, Project $newValue): static
{
// do specific stuff, like logging or ther
return $this->set($newKey, $newValue);
}
}
public function testItShouldFullyWorkWithObjects(): void
{
$map = new Projects([
['library', new Project(10, new Tags(['tag1']))],
['projects', new Project(5, new Tags(['tag2']))],
['gist', new Project(7, new Tags(['tag1', 'tag2']))],
['repository', new Project(7, new Tags(['tag3']))],
]);
$totalSum = $map
->filter(fn (Project $project, mixed $category): bool => $project->tags->hasValue('tag1'))
->reduce(fn (int $sum, Project $project, mixed $category): int => $sum + $project->value, 0);
self::assertInstanceOf(
Projects::class,
$map->filter(fn (Project $project, mixed $category): bool => true),
);
self::assertSame($totalSum, 17);
self::assertCount(4, $map);
$newMap = $map->improvedSet('NewType', new Project(10, new Tags(['tag4'])));
self::assertCount(5, $newMap);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.