/** * @dataProvider assocOneToTen */ public function testThatGroupByGroupsTheElementsCorrectlyAndPreservesKeys(\Pinq\ITraversable $traversable, array $data) { $isEven = function ($i) { return $i % 2 === 0; }; //First number is odd, so the first group should be the odd group list($odd, $even) = $traversable->groupBy($isEven)->asArray(); $this->assertMatches($odd, array_filter($data, function ($i) use($isEven) { return !$isEven($i); })); $this->assertMatches($even, array_filter($data, $isEven)); }
/** * @dataProvider people */ public function testGroupByWithArrayKey(\Pinq\ITraversable $traversable, array $data) { $ageGroupData = $traversable->groupBy(function ($i) { return ['sex' => $i['sex'], 'ageGroup' => floor($i['age'] / 10) * 10]; })->orderByAscending(function (\Pinq\ITraversable $group, $key) { return $key['ageGroup']; })->thenByAscending(function (\Pinq\ITraversable $group, $key) { return $key['sex']; })->select(function (\Pinq\ITraversable $group, $key) { $ageGroup = $key['ageGroup'] . '+'; $sex = $key['sex']; return sprintf('%s(%s){%s}', $ageGroup, $sex, $group->implode(',', function ($i) { return $i['firstName']; })); })->implode(':'); $this->assertEquals('20+(Female){Casy}:20+(Male){Alex,Dave}:30+(Female){Zoe,Sandy,Beth}:30+(Male){Daniel}:' . '40+(Male){Hugo}:50+(Male){Daniel}:60+(Male){David}', $ageGroupData); }
public function visitGroupBy(Segments\GroupBy $query) { $this->traversable = $this->traversable->groupBy($this->resolvedParameters[$query->getProjectionFunction()->getCallableId()]); }