PHP code example of jgswift / qinq

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

    

jgswift / qinq example snippets


$richPeople = $people
    ->where(function($person) { return $person['money'] > 1000000; })
    ->order(function($personA,$personB) { return ($personA['money'] < $personB['money']) ? -1 : 1; })
    ->index(function($person) { return $person['lastName']; })
    ->select(function($person) {
        return [
            'fullName' => $person['firstName'].' '.$person['lastName'],
            'money' => $person['money'],
            'networth' => $person['money'] - $person['debt']
        ];
    });

$integers = new qinq\Collection(range(1,50));
$names = new qinq\Collection(['bob','joe','sam','john','jake']);

// Retrieve all integers divisible by 5
foreach($integers->where(function($n) { return $n % 5 === 0; }) as $integer) {
    // 5, 10, 15, 20 ...
}

// Retrieve all names with a character length of 3
foreach($names->where(function($n) { return strlen($n) === 3; }) as $name) {
    // bob, joe, sam
}

foreach($numbers
        ->filter(function($v) {
            return (bool)($v & 1); // filter out even values
        })
        ->map(function($v) {
            return $v * $v * $v; // cube all remaining odd values
        }) as $number) {
    // 1, 27, 125, 343, 729 ...
}

// Retrieves all integers in descending order
foreach($integers->order(qinq\Order::DESCENDING) as $integer) {
    // 50, 49, 48, 47 ...
}

// Retrieves all names in order of character length
foreach($names->order(function($n) { return strlen($n); } ) as $name ) {
    // john, jake, bob ...
}

// Retrieve all names in order of character length (with compare function)
foreach($names->sort(function($a,$b) { return (strlen($a) > strlen($b)) ? -1 : 1; } ) as $name ) {
    // john, jake, bob ...
}

// Group values by divisibility of 2
foreach($integers->group(function($n) { return $n % 2; }) as $group) {
    // [ 2, 4, 6, 8 ... ], [ 1, 3, 5, 7 ... ]
}

// Group names by to character length
foreach($names->group(function($n) { return strlen($n); }) as $group) {
    // [ bob, joe, sam ], [ john, jake ]
}

// Join integer collections using comparison method (on) and output method (to)
foreach($integers
    ->join($integers)
    ->on(function($outer,$inner) {
        return ($outer >= $inner) ? true : false;
    })
    ->to(function($outer,qinq\Collection $innerGroup) {
        return $outer.':'.implode(',',$innerGroup->toArray());
    }) as $integer ) {
        // 1:1 , 2:1,2 , 3:1,2,3 ...
    }

// Join integer and name collection, grouping names with integer that matches the character length
foreach($integers
    ->join($names)
    ->on(function($outer) {
        return $outer;
    }, 'strlen')
    ->to(function($outer,$inner) {
        return $outer.':'.$inner;
    }) as $number ) {
        // 3:bob, 3:joe, 3:sam, 4:john, 4:jake
    }

foreach($integers
    ->difference(range(25,50))
    as $number) {
        // 1, 2, 3, ..., 24
    }

foreach($integers
    ->except(range(25,50))
    as $number) {
        // 1, 2, 3, ..., 24
    }

echo $integers->first(); // 1

echo $integers->last(); // 50

$users = new qinq\Collection([
    [
        'name' => 'bob'
        'email' => '[email protected]'
    ],
    [
        'name' => 'jim'
        'email' => '[email protected]'
    ]
]);

foreach($users->pluck('name') as $name) {
    // bob, jim 
}

class User {
    public $name, $email;

    function __construct($name, $email) { /* ... */ }
}

$users = new qinq\Collection([
    new User('bob','[email protected]'),
    new User('jim','[email protected]'),
]);

foreach($users->pluck('email') as $email) {
    // [email protected], [email protected]
}

foreach($integers
    ->from([3,4])
    as $number) {
        // 3, 4
    }

foreach($integers
    ->from(3,4,5)
    as $number) {
        // 3, 4, 5
    }

foreach($integers
    ->intersect(range(25,100))
    as $number) {
        // 25, 26, 27, ..., 50
    }

$q = new qinq\Collection([1,2,3,4,5]);

foreach($q
    ->reduce(function($carry,$item) {
        return $carry * $item; // 1 * 2 * 3 * 4 * 5
    })
    as $result) {
        // 120
    }

foreach($integers
    ->shuffle()
    as $result) {
        // random number between 1 and 50
    }

foreach($integers
    ->values()
    as $result) {
        // 1, 2, 3, ..., 50
    }

foreach($integers
    ->keys()
    as $number) {
        // 1, 2, 3, ..., 50
    }

$junk = new qinq\Collection([
    false, 0, false, '0', 'hello'
]);

foreach($junk
    ->pack()
    as $item) {
        // 'hello'
    }

foreach($integers
    ->random(5)
    as $result) {
        // 5 random items from array
    }

$numbers = new qinq\Collection([
    1, 2, [3, 4, [5, 6]], 7, [8, [9, 10]] // multidimensional array
]);

foreach($numbers
    ->recursive(function($value) {
        return $value * $value; // square(^2) values
    }) as $number) {
        // [ 1, 4, [ 9, 16, [ 25, 36 ] ] , 49, [ 64, [ 81, 100 ] ] ]
    }

$numbers = new qinq\Collection([
    1, 2, [3, 4, [5, 6]], 7, [8, [9, 10]] // multidimensional array
]);

foreach($numbers
    ->search(function($value) {
        return ($value & 1) ? true : false; // only 

$ages = new qinq\Collection([
    'joe' => 26,
    'jim' => 40
    'john' => 16
]);

foreach($ages
    ->selector('joe|jim') as $age) {
        // [ 26, 40 ]
    }

$favorites = new qinq\Collection([
    'joe' => [
        'color' => 'red',
        'hat' => 'fedora'
    ]
]);

foreach($favorites
    ->selector('[joe][color]') as $favorite) {
        // [ 'color' => 'red' ]
    }

$tree = new qinq\Collection([
    [1,2,[8,9],3,4],
    [4,5,6,[1,2,3]],
    [8,[9,10],[4,5]]
]);

foreach($tree
    ->flatten()
    as $number) {
        // 1, 2, 8, 9, 3, 4, 4, 5..
    }

use qinq\Object\Query\Flatten;

$tree = new qinq\Collection([
    [1,2,[3,4]
    [5,new ArrayObject([6,7,8])],
]);

foreach($tree
    ->flatten(Flatten::ARRAYONLY)
    as $number) {
        // 1, 2, 3, 4, ArrayObject()
    }

$query = new \qinq\Object\Query($integers);

$query->select(function($n) {
    return $n * 3;
})->where(function($n) {
    return $n % 2 === 0;
});

$query_to_cache = serialize($query);

$query_from_cache = unserialize($query_to_cache);

var_dump( $query_from_cache->execute() === $query->execute() ) // true
sh
php composer.phar