Skip to content

Commit

Permalink
Get values of different languages in one time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berthereau authored and Daniel Berthereau committed Sep 14, 2020
1 parent ea99176 commit 8eff1b9
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ public function values()
* returns the first matching value.
* - default: (null) Default value if no values match criteria. Returns null
* by default for single result, empty array for all results.
* - lang: (null) Get values of this language only. Returns values of all
* languages by default.
* - lang (array|string): Get values of these languages only. Returns values
* of all languages by default. Use `['']` to get values without language.
* @return ValueRepresentation|ValueRepresentation[]|mixed
*/
public function value($term, array $options = [])
Expand All @@ -331,9 +331,6 @@ public function value($term, array $options = [])
if (!isset($options['default'])) {
$options['default'] = $options['all'] ? [] : null;
}
if (!isset($options['lang'])) {
$options['lang'] = null;
}

if (!$this->getAdapter()->isTerm($term)) {
return $options['default'];
Expand All @@ -343,6 +340,14 @@ public function value($term, array $options = [])
return $options['default'];
}

if (empty($options['lang'])) {
$langs = false;
} elseif (is_array($options['lang'])) {
$langs = $options['lang'];
} else {
$langs = [$options['lang']];
}

// Match only the representations that fit all the criteria.
$matchingValues = [];
foreach ($this->values()[$term]['values'] as $value) {
Expand All @@ -351,9 +356,7 @@ public function value($term, array $options = [])
) {
continue;
}
if (!is_null($options['lang'])
&& $value->lang() !== $options['lang']
) {
if ($langs && !in_array($value->lang(), $langs)) {
continue;
}
$matchingValues[] = $value;
Expand Down

0 comments on commit 8eff1b9

Please sign in to comment.