Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
III-1959: Index typicalAgeRange on events and places
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed Mar 16, 2017
1 parent e4545ca commit db62591
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Event/EventJsonDocumentTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function transform(JsonDocument $jsonDocument)
$this->copyTerms($body, $newBody);

$this->copyPerformer($body, $newBody);
$this->copyTypicalAgeRange($body, $newBody);

$this->copyAddressAndGeoInformation($body->location, $newBody);

Expand Down
32 changes: 32 additions & 0 deletions src/Offer/AbstractOfferJsonDocumentTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,38 @@ function (\stdClass $term) {
}
}

/**
* @param \stdClass $from
* @param \stdClass $to
*/
protected function copyTypicalAgeRange(\stdClass $from, \stdClass $to)
{
if (isset($from->typicalAgeRange) && is_string($from->typicalAgeRange)) {
$regexMatches = [];
preg_match('/(\d*)-(\d*)/', $from->typicalAgeRange, $regexMatches);


if (count($regexMatches) !== 3) {
// The matches should always contain exactly 3 values:
// 0: The delimiter (-)
// 1: minAge as string (or empty string)
// 2: maxAge as string (or empty string)
return;
}

// Be sure to always do a strict comparison here!
$minAge = ($regexMatches[1] !== '') ? (int) $regexMatches[1] : 0;
$maxAge = ($regexMatches[2] !== '') ? (int) $regexMatches[2] : null;

$to->typicalAgeRange = new \stdClass();
$to->typicalAgeRange->gte = $minAge;

if ($maxAge) {
$to->typicalAgeRange->lte = $maxAge;
}
}
}

/**
* @param \stdClass $from
* @param \stdClass $to
Expand Down
2 changes: 2 additions & 0 deletions src/Place/PlaceJsonDocumentTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function transform(JsonDocument $jsonDocument)
$this->copyLabelsForFreeTextSearch($body, $newBody);
$this->copyTerms($body, $newBody);

$this->copyTypicalAgeRange($body, $newBody);

$this->copyAddressAndGeoInformation($body, $newBody);

$this->copyOrganizer($body, $newBody);
Expand Down
4 changes: 4 additions & 0 deletions tests/Event/data/indexed-with-optional-fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"name": "Jane Doe"
}
],
"typicalAgeRange": {
"gte": 0,
"lte": 4
},
"addressLocality": "Leuven",
"postalCode": "3000",
"streetAddress": "Vaartkom 35",
Expand Down
3 changes: 2 additions & 1 deletion tests/Event/data/original-with-optional-fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@
"@type": "Organizer",
"@id": "http://udb-silex.dev/organizers/5e0b3f9c-5947-46a0-b8f2-a1a5a37f3b83",
"name": "PHPLeuven"
}
},
"typicalAgeRange": "-4"
}
3 changes: 3 additions & 0 deletions tests/Place/data/indexed-with-optional-fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"label": "Zaal of expohal"
}
],
"typicalAgeRange": {
"gte": 18
},
"addressLocality": "Leuven",
"postalCode": "3000",
"streetAddress": "Vaartkom 35",
Expand Down
3 changes: 2 additions & 1 deletion tests/Place/data/original-with-optional-fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
"@type": "Organizer",
"@id": "http://udb-silex.dev/organizers/5e0b3f9c-5947-46a0-b8f2-a1a5a37f3b83",
"name": "PHPLeuven"
}
},
"typicalAgeRange": "18-"
}

0 comments on commit db62591

Please sign in to comment.