PHP code example of micoli / multitude

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/ */

    

micoli / multitude example snippets


    public function testItShouldFullyWorkWithAssociativeArray(): void
    {
        /** @var ImmutableMap<string, array{value:int,tags:list<string>}> $map */
        $map = new ImmutableMap([
            ['library', ['value' => 10, 'tags' => ['tag1']]],
            ['projects', ['value' => 5, 'tags' => ['tag2']]],
            ['gist', ['value' => 7, 'tags' => ['tag1', 'tag2']]],
            ['repository', ['value' => 7, 'tags' => ['tag3']]],
        ]);
        $totalSum = $map
            ->filter(fn (array $project, mixed $category): bool => array_search('tag1', $project['tags']) !== false)
            ->reduce(fn (int $sum, mixed $project, mixed $category): int => $sum + $project['value'], 0);
        self::assertSame($totalSum, 17);
        self::assertCount(4, $map);
    }



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);
    }