示例#1
0
 public function testWithReturnsPlinqWrapper()
 {
     $input = [1];
     $expected = 'plinq\\plinqWrapper';
     $actual = plinq::with($input);
     $this->assertInstanceOf($expected, $actual);
 }
示例#2
0
 public function testAnyReturnsFalseWithEmptyInputAndNoCondition()
 {
     $input = [];
     $expected = false;
     $actual = plinq::any($input);
     $this->assertEquals($expected, $actual);
 }
示例#3
0
 public function testAllReturnsTrueWhenInputIsEmpty()
 {
     $input = [];
     $expected = true;
     $actual = plinq::all($input, $this->condition);
     $this->assertEquals($expected, $actual);
 }
示例#4
0
 public function testTailReturnsEmptyArrayIfInputHasOneElement()
 {
     $input = [1];
     $expected = [];
     $actual = plinq::tail($input);
     $this->assertEquals($expected, $actual);
 }
示例#5
0
 public function testCountTwoParamsReturnsNumberOfElementsMatchingTheExpression()
 {
     $input = [1, 2, 3];
     $expected = 1;
     $actual = plinq::count($input, $this->evenNumberExpression);
     $this->assertEquals($expected, $actual);
 }
示例#6
0
 public function testReturnsKeyAndValueWhenFlagSet()
 {
     $input = ["a" => 1, "b" => 2, "c" => 3, "d" => 4];
     $expected = ["d" => 4];
     $actual = plinq::on($input)->first($this->condition, plinq::$MAINTAIN_KEY);
     $this->assertEquals($expected, $actual->toArray());
 }
示例#7
0
 public function testResultIsNotPlinqWrapperWhenChainDoesntStartWithOn()
 {
     $input = [1, 2, 3, 4, 5, 6];
     $expected = 'plinq\\plinqWrapper';
     $actual = plinq::where($input, $this->evenNumberExpression);
     $this->assertNotInstanceOf($expected, $actual);
 }
示例#8
0
 public function testSelectReturnsArrayWhenChainDoesntStartWithOn()
 {
     $input = [1, 2, 3];
     $expected = "array";
     $doublingSelection = function ($key, $value) {
         return $value * 2;
     };
     $actual = plinq::select($input, $doublingSelection);
     $this->assertInternalType($expected, $actual);
 }
示例#9
0
 public function testMaxChoosesNewNumberAsLargerIfComparatorReturnsTrue()
 {
     $input = [1, 2, "three"];
     $expected = "three";
     $callback = function ($currentMax, $compareTo) {
         return true;
     };
     $actual = plinq::max($input, $callback);
     $this->assertEquals($expected, $actual);
 }
示例#10
0
 public function testReturnsDistinctValuesFromExpressionIfProvided()
 {
     $input = ["a", "A", "b", "B"];
     $expected = ["a", "b"];
     $expression = function ($k, $v) {
         return strtolower($v);
     };
     $actual = plinq::with($input)->distinct($expression)->toArray();
     $this->assertEquals($expected, $actual);
 }
示例#11
0
 public function testMinChoosesNewNumberAsSmallerIfComparatorReturnsTrue()
 {
     $input = [1, 2, "three"];
     $expected = "three";
     // The mock callback always returns true, thus the last element is "smallest"
     $callback = function ($currentMin, $compareTo) {
         return true;
     };
     $actual = plinq::min($input, $callback);
     $this->assertEquals($expected, $actual);
 }
示例#12
0
 public function testFunctionAppliedToEachElement()
 {
     $input = [1, 2, 3, 4, 5];
     $seed = 0;
     $accumulator = function ($acc, $key, $val) {
         $acc += $val;
         return $acc;
     };
     $expected = 15;
     $actual = plinq::aggregate($input, $seed, $accumulator);
     $this->assertEquals($expected, $actual);
 }
示例#13
0
文件: books.php 项目: andybursh/plinq
    return strtolower($v->genre) == "fantasy";
});
print "There are {$numFantasyBooks} fantasy books \r\n";
print "\r\n";
// Calculate the price of all romance books
print "==== The price of all romance books ==== \r\n";
$sumAggregate = function ($acc, $k, $v) {
    return $acc + floatval($v->price);
};
$priceOfRomanceBooks = plinq::with($books)->where(function ($v) {
    return strtolower($v->genre) == "romance";
})->aggregate(0.0, $sumAggregate);
print "The price of all romance books is {$priceOfRomanceBooks} \r\n";
print "\r\n";
// Calculate the price of all genres of book
print "==== The price of each genre book ==== \r\n";
$allPrices = plinq::with($books)->distinct(function ($k, $v) {
    return strtolower($v->genre);
})->aggregate(array(), function ($acc, $k, $v) use($books, $sumAggregate) {
    if (!array_key_exists($v, $acc)) {
        // Find the sum of the prices in this genre
        $acc[$v] = plinq::on($books)->where(function ($b) use($v) {
            return strtolower($b->genre) == $v;
        })->aggregate(0.0, $sumAggregate);
    }
    return $acc;
})->toArray();
foreach ($allPrices as $g => $p) {
    print "The price of all {$g} books is {$p} \r\n";
}
print "\r\n";
示例#14
0
文件: plinq.php 项目: andybursh/plinq
 /**
  * Alias of on
  * @param 	array 	$input
  *
  * @return 	plinqArrayWrapper
  */
 public static function with(array $input)
 {
     return plinq::on($input);
 }