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

Added twig extension for function 'preg_split' #2590

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/classes/Gantry/Component/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public function getFunctions()
new \Twig_SimpleFunction('parse_assets', [$this, 'parseAssetsFunc']),
new \Twig_SimpleFunction('colorContrast', [$this, 'colorContrastFunc']),
new \Twig_SimpleFunction('get_cookie', [$this, 'getCookie']),
new \Twig_SimpleFunction('preg_match', [$this, 'pregMatch']),
new \Twig_SimpleFunction('imagesize', [$this, 'imageSize']),
new \Twig_SimpleFunction('is_selected', [$this, 'is_selectedFunc']),
new \Twig_SimpleFunction('url', [$this, 'urlFunc']),
Expand All @@ -108,6 +107,8 @@ public function getFunctions()
$functions = array_merge($functions, [
new \Twig_SimpleFunction('array', [$this, 'arrayFilter']),
new \Twig_SimpleFunction('json_decode', [$this, 'jsonDecodeFilter']),
new \Twig_SimpleFunction('preg_match', [$this, 'pregMatch']),
new \Twig_SimpleFunction('preg_split', [$this, 'pregSplit']),
]);
}

Expand Down Expand Up @@ -674,10 +675,31 @@ public function getCookie($name)
return $request->cookie[$name];
}

/**
* Twig wrapper for PHP's preg_match method
*
* @param string $pattern the regex pattern to use for match
* @param string $subject the content to perform the match on
* @param array &$matches if given, the parameter is filled with the match results. The first array elements contains the text which matches the complete search pattern. The second element contains the text which matches the first bracketed subpattern and so on.
* @return mixed returns the matches if there is at least one match in the subject for a given pattern or false if not.
*/
public function pregMatch($pattern, $subject, &$matches = [])
{
preg_match($pattern, $subject, $matches);

return $matches ?: false;
}

/**
* Twig wrapper for PHP's preg_split method
*
* @param string $pattern the regex pattern to use for split
* @param string $subject the content to perform the split on
* @param int $limit the maximum possible splits for the given pattern
* @return array the resulting array after performing the split operation
*/
public function pregSplit($pattern, $subject, $limit = -1)
{
return preg_split($pattern, $subject, $limit);
}
}