static function add($name, $method) { Chain::$links[$name] = function () use($method) { return hooray(call_user_func_array($method, func_get_args())); }; }
<?php Chain::add('flatmap', function ($data, $action) { return hooray($data)->reduce(function ($carry, $datum) use($action) { $result = $action($datum); if (!is_array($result)) { $result = [$result]; } return array_merge($carry, $result); }, []); });
function testSplice() { $this->assertEquals([1, 2, 3], hooray(1, 4, 3)->splice(1, 1, 2)->realize()); }
function testChunkAcceptsPreserveKeys() { $this->assertEquals([['asdf' => 1], ['qwerty' => 2]], hooray(['asdf' => 1, 'qwerty' => 2])->chunk(1, true)->realize()); }
function testTake() { $this->assertEquals(['asdf', 'asdf'], hooray('asdf', 'asdf', 'qwerty')->take(2)->realize()); }
function testGrowKeys() { $this->assertEquals(['ASDF' => 1], hooray(['asdf' => 1])->growKeys()->realize()); }
function testPad() { $this->assertEquals([1, 1, 1, 1, 1], hooray(1)->pad(5, 1)->realize()); }
function testUnshift() { $this->assertEquals(['asdf', 'asdf'], hooray('asdf')->unshift('asdf')->realize()); }
function testSkipsOxfordCommaWhenOnlyGivenTwoItems() { $this->assertEquals('1 and 2', hooray(1, 2)->asEnglish()); }
function testFilter() { $this->assertEquals([1, 2], hooray(1, 2, 3)->filter(function ($item) { return $item < 3; })->realize()); }
function testShuffle() { $this->assertEquals([3, 1, 2], hooray(1, 2, 3)->shuffle(1000)->realize()); }
function testSum() { $this->assertEquals(10, hooray(5, 5)->sum()); }
function testReduce() { $this->assertEquals(6, hooray(1, 2, 3)->reduce(function ($carry, $item) { return $carry + $item; })); }
<?php Chain::addTerminal('asEnglish', function ($data, $ifEmpty = '') { return count($data) == 2 ? hooray($data)->join(' and ') : hooray($data)->join(', ', ', and ', $ifEmpty); });
function testWorksOnAssociativeArrays() { $this->assertEquals(1, hooray(["a" => 1, "b" => 2, "c" => 3])->first()); }
function testCountValues() { $this->assertEquals(['asdf' => 2, 'qwerty' => 1], hooray('asdf', 'asdf', 'qwerty')->countValues()->realize()); }
function testDrop() { $this->assertEquals(['qwerty'], hooray('asdf', 'asdf', 'qwerty')->drop(2)->realize()); }
function testFlatmapWithoutArrays() { $this->assertEquals([1, 2, 3], hooray(1, 2, 3)->flatmap(function ($item) { return $item; })->realize()); }
function testShrinkKeys() { $this->assertEquals(['asdf' => 1], hooray(['ASDF' => 1])->shrinkKeys()->realize()); }
function testValues() { $this->assertEquals([1, 2, 3], hooray(['asdf' => 1, 'qwerty' => 2, 'hjkl' => 3])->values()->realize()); }
function testPush() { $this->assertEquals([1, 2], hooray(1)->push(2)->realize()); }
function testShift() { $this->assertEquals(1, hooray(1, 2, 3)->shift()); }
function testIfEmpty() { $this->assertEquals('nope', hooray()->join('', '', 'nope')); }
<?php Chain::add('unique', function ($data) { return hooray(array_unique($data, SORT_REGULAR))->values()->realize(); });
function testMap() { $this->assertEquals([2, 3, 4], hooray(1, 2, 3)->map(function ($item) { return $item + 1; })->realize()); }
function testAcceptsNonArrayArgument() { $this->assertEquals([1], hooray(1)->realize()); }
function testKeys() { $this->assertEquals(['asdf', 'qwerty'], hooray(['asdf' => 1, 'qwerty' => 2])->keys()->realize()); }
function testUnique() { $this->assertEquals(["asdf", "qwerty"], hooray("asdf", "asdf", "qwerty")->unique()->realize()); }
function testColumn() { $this->assertEquals([1, 2, 3], hooray([['a' => 1], ['a' => 2], ['a' => 3]])->column('a')->realize()); }
function testPop() { $this->assertEquals(1, hooray(3, 2, 1)->pop()); }