PHP code example of cmatosbc / daedalus

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

    

cmatosbc / daedalus example snippets


use Daedalus\Dictionary;

$dict = new Dictionary();
$dict->add('name', 'John');
$dict->add('age', 30);

$name = $dict->get('name'); // Returns 'John'

use Daedalus\MultiMap;

$tags = new MultiMap();
$tags->add('article1', 'php');
$tags->add('article1', 'programming');
$tags->get('article1'); // ['php', 'programming']

// Bulk operations
$tags->addAll('article2', ['web', 'tutorial']);
$tags->remove('article1', 'php');
$tags->removeAll('article2');

// Check contents
$hasTag = $tags->contains('article1', 'programming'); // true
$count = $tags->count(); // Total number of key-value pairs

use Daedalus\SortedMap;

$scores = new SortedMap();
$scores['alice'] = 95;
$scores['bob'] = 87;
$scores['carol'] = 92;

// Keys are automatically sorted
foreach ($scores as $name => $score) {
    echo "$name: $score\n";
} // Outputs in alphabetical order

// Range operations
$topScores = $scores->subMap('alice', 'bob'); // Get scores from alice to bob
$highScores = $scores->tailMap(90); // Get all scores >= 90
$lowScores = $scores->headMap(90);  // Get all scores < 90

// Find adjacent entries
$nextStudent = $scores->higherKey('bob');   // 'carol'
$prevStudent = $scores->lowerKey('carol');  // 'bob'

use Daedalus\Set;

$set1 = new Set([1, 2, 3]);
$set2 = new Set([2, 3, 4]);

$union = $set1->union($set2);        // {1, 2, 3, 4}
$intersection = $set1->intersection($set2); // {2, 3}

use Daedalus\Matrix;

$matrix = new Matrix([
    [1, 2],
    [3, 4]
]);

$transposed = $matrix->transpose();

use Daedalus\EnhancedArrayObject;

$array = new EnhancedArrayObject([1, 2, 3]);
$array->addEventListener('set', function($key, $value) {
    echo "Element set at key $key with value $value\n";
});

$doubled = $array->map(fn($n) => $n * 2);

use Daedalus\EnhancedArrayObject;

class User {
    public function __construct(public string $name) {}
}

$users = new EnhancedArrayObject([], User::class);
$users[] = new User("John"); // Works
$users[] = "Not a user";     // Throws InvalidArgumentException

$array = new EnhancedArrayObject([1, 2, 3]);
$array->addEventListener('set', function($key, $value) {
    echo "Value changed at $key to $value\n";
});