PicoTwigExtension: Refactor error handling
This commit is contained in:
parent
c17f18f8cf
commit
2ce422d907
@ -103,21 +103,16 @@ class PicoTwigExtension extends Twig_Extension
|
|||||||
public function mapFilter($var, $mapKeyPath)
|
public function mapFilter($var, $mapKeyPath)
|
||||||
{
|
{
|
||||||
if (!is_array($var) && (!is_object($var) || !is_a($var, 'Traversable'))) {
|
if (!is_array($var) && (!is_object($var) || !is_a($var, 'Traversable'))) {
|
||||||
throw new InvalidArgumentException(
|
throw new Twig_Error_Runtime(sprintf(
|
||||||
'Unable to apply Twig "map" filter: '
|
'The map filter only works with arrays or "Traversable", got "%s"',
|
||||||
. 'You must pass a traversable variable'
|
is_object($var) ? get_class($var) : gettype($var)
|
||||||
);
|
));
|
||||||
}
|
|
||||||
if (empty($mapKeyPath)) {
|
|
||||||
throw new InvalidArgumentException(
|
|
||||||
'Unable to apply Twig "map" filter: '
|
|
||||||
. 'You must specify the $mapKeyPath parameter'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = array();
|
$result = array();
|
||||||
foreach ($var as $key => $value) {
|
foreach ($var as $key => $value) {
|
||||||
$result[$key] = $this->getKeyOfVar($value, $mapKeyPath);
|
$mapValue = $this->getKeyOfVar($value, $mapKeyPath);
|
||||||
|
$result[$key] = ($mapValue !== null) ? $mapValue : $value;
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@ -149,22 +144,13 @@ class PicoTwigExtension extends Twig_Extension
|
|||||||
if (is_object($var) && is_a($var, 'Traversable')) {
|
if (is_object($var) && is_a($var, 'Traversable')) {
|
||||||
$var = iterator_to_array($var, true);
|
$var = iterator_to_array($var, true);
|
||||||
} elseif (!is_array($var)) {
|
} elseif (!is_array($var)) {
|
||||||
throw new InvalidArgumentException(
|
throw new Twig_Error_Runtime(sprintf(
|
||||||
'Unable to apply Twig "sort_by" filter: '
|
'The sort_by filter only works with arrays or "Traversable", got "%s"',
|
||||||
. 'You must pass a traversable variable'
|
is_object($var) ? get_class($var) : gettype($var)
|
||||||
);
|
));
|
||||||
}
|
|
||||||
if (empty($sortKeyPath)) {
|
|
||||||
throw new InvalidArgumentException(
|
|
||||||
'Unable to apply Twig "sort_by" filter: '
|
|
||||||
. 'You must specify the $sortKeyPath parameter'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (($fallback !== 'top') && ($fallback !== 'bottom') && ($fallback !== 'keep')) {
|
if (($fallback !== 'top') && ($fallback !== 'bottom') && ($fallback !== 'keep')) {
|
||||||
throw new InvalidArgumentException(
|
throw new Twig_Error_Runtime('The sort_by filter only supports the "top", "bottom" and "keep" fallbacks');
|
||||||
'Unable to apply Twig "sort_by" filter: '
|
|
||||||
. 'Invalid $fallback parameter: ' . $fallback
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$twigExtension = $this;
|
$twigExtension = $this;
|
||||||
@ -206,7 +192,7 @@ class PicoTwigExtension extends Twig_Extension
|
|||||||
* array interpreted as key path (when passing e.g. ['foo', 'bar'],
|
* array interpreted as key path (when passing e.g. ['foo', 'bar'],
|
||||||
* the method will return $var['foo']['bar']) specifying the value
|
* the method will return $var['foo']['bar']) specifying the value
|
||||||
* @return mixed the requested
|
* @return mixed the requested
|
||||||
* value or NULL when the the given key or key path didn't match
|
* value or NULL when the given key or key path didn't match
|
||||||
*/
|
*/
|
||||||
public static function getKeyOfVar($var, $keyPath)
|
public static function getKeyOfVar($var, $keyPath)
|
||||||
{
|
{
|
||||||
@ -218,15 +204,21 @@ class PicoTwigExtension extends Twig_Extension
|
|||||||
|
|
||||||
foreach ($keyPath as $key) {
|
foreach ($keyPath as $key) {
|
||||||
if (is_object($var)) {
|
if (is_object($var)) {
|
||||||
if (is_a($var, 'Traversable')) {
|
if (is_a($var, 'ArrayAccess')) {
|
||||||
|
// use ArrayAccess, see below
|
||||||
|
} elseif (is_a($var, 'Traversable')) {
|
||||||
$var = iterator_to_array($var);
|
$var = iterator_to_array($var);
|
||||||
} elseif (isset($var->{$key})) {
|
} elseif (isset($var->{$key})) {
|
||||||
$var = $var->{$key};
|
$var = $var->{$key};
|
||||||
continue;
|
continue;
|
||||||
} elseif (is_callable(array($var, 'get' . ucfirst($key)))) {
|
} elseif (is_callable(array($var, 'get' . ucfirst($key)))) {
|
||||||
$var = call_user_func(array($var, 'get' . ucfirst($key)));
|
try {
|
||||||
continue;
|
$var = call_user_func(array($var, 'get' . ucfirst($key)));
|
||||||
} elseif (!is_a($var, 'ArrayAccess')) {
|
continue;
|
||||||
|
} catch (BadMethodCallException $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} elseif (!is_array($var)) {
|
} elseif (!is_array($var)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user