PHP code example of quartet / haydn

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

    

quartet / haydn example snippets


$fruits = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$drinks = [
    ['name' => 'Yoghurt', 'price' => 200],
    ['name' => 'Soda',    'price' => 120],
    ['name' => 'Spirit',  'price' => 160],
];

$fruitSet = new Set(new ArraySource('fruit', $fruits, new HashKeyColumnMapper()));
$drinkSet = new Set(new ArraySource('drink', $drinks, new HashKeyColumnMapper()));

$fruitDrinkSet = $fruitSet->product($drinkSet);
$result = $fruitDrinkSet->toArray();

$fruitsAssoc = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$fruitsSet = new Set(new ArraySource('fruit', $fruitsAssoc, new HashKeyColumnMapper());

foreach ($fruitSet as $fruit) {
    echo 'name:' . $fruit['name'] . ' price:' . $fruit['price'] . PHP_EOL;
}

$fruitsAssoc = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$fruitsSet = new Set(new ArraySource('fruit', $fruitsAssoc, new HashKeyColumnMapper());

$identicalSet = new IdenticalSet();

$all = $fruitSet->product($identicalSet);

// same as $fruitSet

$all = $identicalSet->product($fruitSet);

// same as $fruitSet

$all = $fruitSet->union($identicalSet);

// same as $fruitSet

$all = $identicalSet->union($fruitSet);

// same as $fruitSet

$fruitsAssoc = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$fruitsSet = new Set(new ArraySource('fruit', $fruitsAssoc, new HashKeyColumnMapper());

$emptySet = new EmptySet();

$all = $fruitSet->product($emptySet);

// empty

$all = $emptySet->product($fruitSet);

// empty

$all = $fruitSet->union($emptySet);

// same as $fruitSet

$all = $emptySet->union($fruitSet);

// same as $fruitSet

$fruits = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$drinks = [
    ['name' => 'Yoghurt', 'price' => 200],
    ['name' => 'Soda',    'price' => 120],
    ['name' => 'Spirit',  'price' => 160],
];

$fruitSet = new Set(new ArraySource('fruit', $fruits, new HashKeyColumnMapper()));
$drinkSet = new Set(new ArraySource('drink', $drinks, new HashKeyColumnMapper()));

$fruitDrinkSet = $fruitSet->product($drinkSet);

$fruits = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$fruitMenuSet = new Set(new ArraySource('fruit', $fruits, new HashKeyColumnMapper()))
    ->select([function($row) {
        return [
            'menu item name' => $row['name']
        ];
    }]);
;


$products = [
    ['name' => 'Apple',   'price' => 100, 'type' => 'fruit'],
    ['name' => 'Yoghurt', 'price' => 200, 'type' => 'drink'],
    ['name' => 'Soda',    'price' => 120, 'type' => 'drink'],
    ['name' => 'Banana',  'price' =>  80, 'type' => 'fruit'],
    ['name' => 'Spirit',  'price' => 160, 'type' => 'drink'],
];

$fruitSet = new Set(new ArraySource('product', $products, new HashKeyColumnMapper()))
    ->filter(new Matcher(['type' => 'fruit']));
;

new Matcher(['type' => function($value) {
    return strpos($value, ':') !== false;
}])

$products = [
    ['name' => 'Apple',   'price' => 100, 'type' => 'fruit'],
    ['name' => 'Yoghurt', 'price' => 200, 'type' => 'drink'],
    ['name' => 'Soda',    'price' => 120, 'type' => 'drink'],
    ['name' => 'Banana',  'price' =>  80, 'type' => 'fruit'],
    ['name' => 'Spirit',  'price' => 160, 'type' => 'drink'],
];

$productSet = new Set(new ArraySource('product', $products, new HashKeyColumnMapper()));

list($fruitSet, $drinkSet) = $productSet->devide([
    'fruit' => new Matcher(['type' => 'fruit']),
    'drink' => new Matcher(['type' => 'drink']),
]);


$fruits = [
    ['name' => 'Apple',  'price' => 100],
    ['name' => 'Banana', 'price' =>  80],
];

$drinks = [
    ['name' => 'Yoghurt', 'price' => 200],
    ['name' => 'Soda',    'price' => 120],
    ['name' => 'Spirit',  'price' => 160],
];

$fruitSet = new Set(new ArraySource('fruit', $fruits, new HashKeyColumnMapper()));
$drinkSet = new Set(new ArraySource('drink', $drinks, new HashKeyColumnMapper()));

$allSet = $fruitSet->union($drinkSet);

$k1 = new Set(new SingleColumnArraySource('k1', ['あいう', 'かきく']));
$k2 = new Set(new SingleColumnArraySource('k2', ['abc', 'def']));
$k3 = new Set(new SingleColumnArraySource('k3', ['123', '456']));

$g1 = new Set\GroupingSet(
        // Key Set
        $k1->product($k2),
        // Header Generator
        function ($r) { return ['type' => 'header', 'name' => $r['k1'] . '-' . $r['k2']]; },
        // Detail Set Generator
        function ($r) use ($k3) {
            $set = new Set(new SingleRowSource('k1k2', $r));
            $resultSet = $set->product($k3)->select([function ($r) {
                return [
                    'type' => 'detail',
                    'content' => $r['k1'] . ' ' . $r['k2'] . ' ' . $r['k3'],
                ];
            }]);
            return $resultSet;
        },
        // Footer Generator
        null
    );
    
$all = $g1->toArray();
var_dump($all);

// [
//     'type' => 'header',
//     'name' => 'あいう-abc',
// ], [
//     'type' => 'detail',
//     'content' => 'あいう abc 123',
// ], [
//     'type' => 'detail',
//     'content' => 'あいう abc 456',
// ], [
//     'type' => 'header',
//     'name' => 'あいう-def',
// ], [
//     'type' => 'detail',
//     'content' => 'あいう def 123',
// ], [
//     'type' => 'detail',
//     'content' => 'あいう def 456',
// ], [
//     'type' => 'header',
//     'name' => 'かきく-abc',
// ], [
//     'type' => 'detail',
//     'content' => 'かきく abc 123',
// ], [
//     'type' => 'detail',
//     'content' => 'かきく abc 456',
// ], [
//     'type' => 'header',
//     'name' => 'かきく-def',
// ], [
//     'type' => 'detail',
//     'content' => 'かきく def 123',
// ], [
//     'type' => 'detail',
//     'content' => 'かきく def 456',
// ],

$fruitArray = [
    ['id' => 1, 'name' => 'Apple'],
    ['id' => 2, 'name' => 'Banana'],
    ['id' => 3, 'name' => 'Lemon'],
];

$fruitSet = new Set(new ArraySource('fruit', $fruitArray, new HashKeyColumnMapper()));

$output = $fruitSet->toArray();
var_dump($output);

// ['id' => 1, 'name' => 'Apple'],
// ['id' => 2, 'name' => 'Banana'],
// ['id' => 3, 'name' => 'Lemon']

$fruitArray = [
  'Apple',
  'Banana',
  'Lemon'];

$fruitSet = new Set(new SingleColumnArraySource('fruit', $fruitArray, new NullColumnMapper()));

$output = $fruitSet->toArray();
var_dump($output);

// ['fruit' => 'Apple'],
// ['fruit' => 'Banana'],
// ['fruit' => 'Lemon'],

$fruitArray = ['1','Apple','140'];

$fruitSet = new Set(new SingleRowSource('fruit', $fruitArray, new SimpleArrayColumnMapper([
    'id', 'name', 'price'
])));

$output = $fruitSet->toArray();
var_dump($output);

// ['id'=>1, 'name' => 'Apple', 'price'=>140],

$fruitArray = [
    ['id' => 1, 'name' => 'Apple'],
    ['id' => 2, 'name' => 'Banana'],
    ['id' => 3, 'name' => 'Lemon'],
];

$fruitSet = new Set(new ArraySource('fruit', $fruitArray, new HashKeyColumnMapper()));

$output = $fruitSet->toArray();
var_dump($output);

// ['id' => 1, 'name' => 'Apple'],
// ['id' => 2, 'name' => 'Banana'],
// ['id' => 3, 'name' => 'Lemon']

$fruitColumn = ['id', 'name'];

$fruitArray = [
    [1, 'Apple'],
    [2, 'Banana'],
    [3, 'Lemon'],
];

$fruitSet = new Set(new ArraySource('fruit', $fruitArray, new SimpleArrayColumnMapper($fruitColumn)));

$output = $fruitSet->toArray();
var_dump($output);

// ['id' => 1, 'name' => 'Apple'],
// ['id' => 2, 'name' => 'Banana'],
// ['id' => 3, 'name' => 'Lemon']

$fruitArray = [
    [1, 'Apple'],
    [2, 'Banana'],
    [3, 'Lemon'],
];

$fruitSet = new Set(new ArraySource('fruit', $fruitArray, new NullColumnMapper()));

$output = $fruitSet->toArray();
var_dump($output);

// [0 => 1, 1 => 'Apple'],
// [0 => 2, 1 => 'Banana'],
// [0 => 3, 1 => 'Lemon']