Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FetchPairs: support traversing #286

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/Collection/Helpers/FetchPairsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,51 @@ public static function process(Traversable $collection, $key = null, $value = nu
}

if ($key === null) {
$chain = self::parseExpr($value);
foreach ($rows as $row) {
$return[] = $row->{$value};
$return[] = self::getProperty($row, $chain);
}

} elseif ($value === null) {
$chain = self::parseExpr($key);
foreach ($rows as $row) {
$return[is_object($row->{$key}) ? (string) $row->{$key} : $row->{$key}] = $row;
$result = self::getProperty($row, $chain);
$return[is_object($result) ? (string) $result : $result] = $row;
}

} else {
$keyChain = self::parseExpr($key);
$valueChain = self::parseExpr($value);
foreach ($rows as $row) {
$return[is_object($row->{$key}) ? (string) $row->{$key} : $row->{$key}] = $row->{$value};
$keyResult = self::getProperty($row, $keyChain);
$valueResult = self::getProperty($row, $valueChain);
$return[is_object($keyResult) ? (string) $keyResult : $keyResult] = $valueResult;
}
}

return $return;
}


private static function parseExpr($expr): array
{
list($chain, $sourceEntity) = ConditionParserHelper::parsePropertyExpr($expr);

if ($sourceEntity === null && count($chain) === 0) {
return [$expr];
}

return $chain;
}


private static function getProperty($row, array $chain)
{
do {
$propertyName = array_shift($chain);
$result = isset($result) ? $result->{$propertyName} : $row->{$propertyName};
} while (!empty($chain));

return $result;
}
}
56 changes: 54 additions & 2 deletions tests/cases/unit/Collection/FetchPairsHelperTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ class FetchPairsHelperTest extends TestCase
public function testParser()
{
$data = new ArrayIterator([
$one = (object) ['name' => 'jon snow', 'email' => '[email protected]', 'born' => new DateTimeImmutable('2014-01-01'), 'n' => 10],
$two = (object) ['name' => 'oberyn martell', 'email' => '[email protected]', 'born' => new DateTimeImmutable('2014-01-03'), 'n' => 12],
$one = (object) [
'name' => 'jon snow',
'email' => '[email protected]',
'born' => new DateTimeImmutable('2014-01-01'),
'n' => 10,
'house' => $houseOne = (object) ['name' => 'House Stark', 'seat' => 'Winterfell'],
],
$two = (object) [
'name' => 'oberyn martell',
'email' => '[email protected]',
'born' => new DateTimeImmutable('2014-01-03'),
'n' => 12,
'house' => $houseTwo = (object) ['name' => 'House Martell', 'seat' => 'Sunspear'],
],
]);

Assert::same(
Expand Down Expand Up @@ -60,6 +72,46 @@ class FetchPairsHelperTest extends TestCase
],
FetchPairsHelper::process($data, 'born', 'email')
);

Assert::same(
[
10 => 'House Stark',
12 => 'House Martell',
],
FetchPairsHelper::process($data, 'n', 'this->house->name')
);

Assert::same(
[
10 => $houseOne,
12 => $houseTwo,
],
FetchPairsHelper::process($data, 'n', 'this->house')
);

Assert::same(
[
'jon snow' => 'Winterfell',
'oberyn martell' => 'Sunspear',
],
FetchPairsHelper::process($data, 'this->name', 'this->house->seat')
);

Assert::same(
[
'House Stark' => 'jon snow',
'House Martell' => 'oberyn martell',
],
FetchPairsHelper::process($data, 'this->house->name', 'this->name')
);

Assert::same(
[
'House Stark' => $one,
'House Martell' => $two,
],
FetchPairsHelper::process($data, 'this->house->name')
);
}


Expand Down