Last active
October 11, 2018 13:56
-
-
Save wagura-maurice/6f09fd9e9ea68bb6e4795d7afa9e8539 to your computer and use it in GitHub Desktop.
laravel parser helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Helpers; | |
/** | |
* Parser Class | |
* | |
* @package Laravel | |
* @subpackage Libraries | |
* @category Parser | |
* @author Wagura Maurice | |
* @link https://gist.github.com/wagura-maurice/6f09fd9e9ea68bb6e4795d7afa9e8539.js | |
*/ | |
class Parser { | |
const _l = '{'; | |
const _r = '}'; | |
/** | |
* Parse a String | |
* | |
* Parses pseudo-variables contained in the specified string, | |
* replacing them with the data in the second param | |
* | |
* @param string | |
* @param array | |
* @param bool | |
* @return string | |
*/ | |
public static function parse_string($template, $data, $return = FALSE) | |
{ | |
return Self::_parse($template, $data, $return); | |
} | |
// -------------------------------------------------------------------- | |
/** | |
* Parse a template | |
* | |
* Parses pseudo-variables contained in the specified template, | |
* replacing them with the data in the second param | |
* | |
* @param string | |
* @param array | |
* @param bool | |
* @return string | |
*/ | |
protected static function _parse($template, $data, $return = FALSE) | |
{ | |
if ($template === '') | |
{ | |
return FALSE; | |
} | |
$replace = array(); | |
foreach ($data as $key => $val) | |
{ | |
$replace = array_merge( | |
$replace, | |
is_array($val) | |
? Self::_parse_pair($key, $val, $template) | |
: Self::_parse_single($key, (string) $val, $template) | |
); | |
} | |
unset($data); | |
$template = strtr($template, $replace); | |
return $template; | |
} | |
/** | |
* Parse a single key/value | |
* | |
* @param string | |
* @param string | |
* @param string | |
* @return string | |
*/ | |
protected static function _parse_single($key, $val, $string) | |
{ | |
return array(Self::_l.$key.Self::_r => (string) $val); | |
} | |
// -------------------------------------------------------------------- | |
/** | |
* Parse a tag pair | |
* | |
* Parses tag pairs: {some_tag} string... {/some_tag} | |
* | |
* @param string | |
* @param array | |
* @param string | |
* @return string | |
*/ | |
protected static function _parse_pair($variable, $data, $string) | |
{ | |
$replace = array(); | |
preg_match_all( | |
'#'.preg_quote(Self::_l.$variable.Self::_r).'(.+?)'.preg_quote(Self::_l.'/'.$variable.Self::_r).'#s', | |
$string, | |
$matches, | |
PREG_SET_ORDER | |
); | |
foreach ($matches as $match) | |
{ | |
$str = ''; | |
foreach ($data as $row) | |
{ | |
$temp = array(); | |
foreach ($row as $key => $val) | |
{ | |
if (is_array($val)) | |
{ | |
$pair = Self::_parse_pair($key, $val, $match[1]); | |
if ( ! empty($pair)) | |
{ | |
$temp = array_merge($temp, $pair); | |
} | |
continue; | |
} | |
$temp[Self::_l.$key.Self::_r] = $val; | |
} | |
$str .= strtr($match[1], $temp); | |
} | |
$replace[$match[0]] = $str; | |
} | |
return $replace; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment