Пример #1
0
 static function add($name, $method)
 {
     Chain::$links[$name] = function () use($method) {
         return hooray(call_user_func_array($method, func_get_args()));
     };
 }
Пример #2
0
<?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);
    }, []);
});
Пример #3
0
 function testSplice()
 {
     $this->assertEquals([1, 2, 3], hooray(1, 4, 3)->splice(1, 1, 2)->realize());
 }
Пример #4
0
 function testChunkAcceptsPreserveKeys()
 {
     $this->assertEquals([['asdf' => 1], ['qwerty' => 2]], hooray(['asdf' => 1, 'qwerty' => 2])->chunk(1, true)->realize());
 }
Пример #5
0
 function testTake()
 {
     $this->assertEquals(['asdf', 'asdf'], hooray('asdf', 'asdf', 'qwerty')->take(2)->realize());
 }
Пример #6
0
 function testGrowKeys()
 {
     $this->assertEquals(['ASDF' => 1], hooray(['asdf' => 1])->growKeys()->realize());
 }
Пример #7
0
 function testPad()
 {
     $this->assertEquals([1, 1, 1, 1, 1], hooray(1)->pad(5, 1)->realize());
 }
Пример #8
0
 function testUnshift()
 {
     $this->assertEquals(['asdf', 'asdf'], hooray('asdf')->unshift('asdf')->realize());
 }
Пример #9
0
 function testSkipsOxfordCommaWhenOnlyGivenTwoItems()
 {
     $this->assertEquals('1 and 2', hooray(1, 2)->asEnglish());
 }
Пример #10
0
 function testFilter()
 {
     $this->assertEquals([1, 2], hooray(1, 2, 3)->filter(function ($item) {
         return $item < 3;
     })->realize());
 }
Пример #11
0
 function testShuffle()
 {
     $this->assertEquals([3, 1, 2], hooray(1, 2, 3)->shuffle(1000)->realize());
 }
Пример #12
0
 function testSum()
 {
     $this->assertEquals(10, hooray(5, 5)->sum());
 }
Пример #13
0
 function testReduce()
 {
     $this->assertEquals(6, hooray(1, 2, 3)->reduce(function ($carry, $item) {
         return $carry + $item;
     }));
 }
Пример #14
0
<?php

Chain::addTerminal('asEnglish', function ($data, $ifEmpty = '') {
    return count($data) == 2 ? hooray($data)->join(' and ') : hooray($data)->join(', ', ', and ', $ifEmpty);
});
Пример #15
0
 function testWorksOnAssociativeArrays()
 {
     $this->assertEquals(1, hooray(["a" => 1, "b" => 2, "c" => 3])->first());
 }
Пример #16
0
 function testCountValues()
 {
     $this->assertEquals(['asdf' => 2, 'qwerty' => 1], hooray('asdf', 'asdf', 'qwerty')->countValues()->realize());
 }
Пример #17
0
 function testDrop()
 {
     $this->assertEquals(['qwerty'], hooray('asdf', 'asdf', 'qwerty')->drop(2)->realize());
 }
Пример #18
0
 function testFlatmapWithoutArrays()
 {
     $this->assertEquals([1, 2, 3], hooray(1, 2, 3)->flatmap(function ($item) {
         return $item;
     })->realize());
 }
Пример #19
0
 function testShrinkKeys()
 {
     $this->assertEquals(['asdf' => 1], hooray(['ASDF' => 1])->shrinkKeys()->realize());
 }
Пример #20
0
 function testValues()
 {
     $this->assertEquals([1, 2, 3], hooray(['asdf' => 1, 'qwerty' => 2, 'hjkl' => 3])->values()->realize());
 }
Пример #21
0
 function testPush()
 {
     $this->assertEquals([1, 2], hooray(1)->push(2)->realize());
 }
Пример #22
0
 function testShift()
 {
     $this->assertEquals(1, hooray(1, 2, 3)->shift());
 }
Пример #23
0
 function testIfEmpty()
 {
     $this->assertEquals('nope', hooray()->join('', '', 'nope'));
 }
Пример #24
0
<?php

Chain::add('unique', function ($data) {
    return hooray(array_unique($data, SORT_REGULAR))->values()->realize();
});
Пример #25
0
 function testMap()
 {
     $this->assertEquals([2, 3, 4], hooray(1, 2, 3)->map(function ($item) {
         return $item + 1;
     })->realize());
 }
Пример #26
0
 function testAcceptsNonArrayArgument()
 {
     $this->assertEquals([1], hooray(1)->realize());
 }
Пример #27
0
 function testKeys()
 {
     $this->assertEquals(['asdf', 'qwerty'], hooray(['asdf' => 1, 'qwerty' => 2])->keys()->realize());
 }
Пример #28
0
 function testUnique()
 {
     $this->assertEquals(["asdf", "qwerty"], hooray("asdf", "asdf", "qwerty")->unique()->realize());
 }
Пример #29
0
 function testColumn()
 {
     $this->assertEquals([1, 2, 3], hooray([['a' => 1], ['a' => 2], ['a' => 3]])->column('a')->realize());
 }
Пример #30
0
 function testPop()
 {
     $this->assertEquals(1, hooray(3, 2, 1)->pop());
 }