Skip to content

Instantly share code, notes, and snippets.

@trk
Last active October 15, 2017 20:00
Show Gist options
  • Save trk/8bda3dfe2bee4ef551f2757eaf660803 to your computer and use it in GitHub Desktop.
Save trk/8bda3dfe2bee4ef551f2757eaf660803 to your computer and use it in GitHub Desktop.
ProcessWire String processing class
<?php
class ProcessString {
public static $allowedFunctions = [
"pages", "page", "config", "modules", "user", "users",
"session", "fields", "templates", "database", "permissions", "roles",
"sanitizer", "datetime", "files", "cache", "languages", "input",
"inputGet", "inputPost", "inputCookie", "urls", "paths", "profiler", "region",
"label", "description", "notes"
];
// ------------------------------------------------------------------------
public static $allowedPageFunctions = ["label", "description", "notes"];
// ------------------------------------------------------------------------
/**
* ProcessString
*
* @param string $str
* @param null $page
* @param string $language
* @return mixed|string
*/
public static function processString($str = "", $page = null, $language = "") {
$page = is_null($page) ? wire('page') : $page;
if(!$language && wire('languages') && !wire('user')->language->isDefault()) {
$language = wire('user')->language;
}
$patterns = [];
foreach (self::$allowedFunctions as $key => $allowedFunction) {
$pattern = "/(?<" . $allowedFunction . ">\{" . $allowedFunction . "(:|\()(.*?)\})/";
$patterns[] = $pattern;
preg_match_all($pattern, $str, $matches);
$found = self::element($allowedFunction, $matches, []);
if(count($found)) {
foreach ($found as $k => $value) {
$str = str_replace($value, self::precessFunction($value, $page, $language), $str);
}
}
}
return $str;
}
// ------------------------------------------------------------------------
/**
* Process Function check and call value function
*
* @param string $value
* @param null $page
* @param string $language
* @return mixed|null|Config|Fields|Fieldtypes|Modules|Notices|Page|Pages|Permissions|ProcessWire|Roles|Sanitizer|Session|Templates|User|Users|Wire|WireDatabasePDO|WireDateTime|WireFileTools|WireHooks|WireInput|WireMailTools|string
*/
protected static function precessFunction($value = "", $page = null, $language = "") {
// Prepare value
$cleanValue = str_replace(['{', '}'], ['', ''], $value);
$isMultiple = explode(':', $cleanValue);
// Set language (for field properties)
if(!$language && wire('languages') && !wire('user')->language->isDefault()) {
$language = wire('user')->language;
}
// Set call and field property function and args
$call = [];
$fieldProperty = [];
if(count($isMultiple) > 1) {
foreach ($isMultiple as $key => $val) {
$isFunction = self::isFunction($val);
if(in_array($isFunction['value'], self::$allowedPageFunctions)) $fieldProperty[] = $isFunction;
else $call[] = $isFunction;
}
} else {
$isFunction = self::isFunction($isMultiple[0]);
if(in_array($isFunction['value'], self::$allowedPageFunctions)) $fieldProperty[] = $isFunction;
else $call[] = $isFunction;
}
$functionCall = null;
// Get and replace $call[] values
if(count($call)) {
foreach ($call as $key => $val) {
if($key == 0 && is_null($functionCall)) {
if($val['value'] == 'page' && $page instanceof Page) {
$functionCall = $page;
}
if($val['isFunction'] === true) {
$functionCall = is_null($functionCall) ? wire($val['value'])->get($val['args']) : $page->get($val['args']);
// $functionCall = wire($val['value'])->get($val['args']);
} else {
$functionCall = is_null($functionCall) ? wire($val['value']) : $page;
// $functionCall = wire($val['value']);
}
} else {
if($val['isFunction'] === true) {
$functionCall = call_user_func([$functionCall, $val['value']], $val['args']);
} else {
$functionCall = $functionCall->{$val['value']};
}
}
}
}
// Get and replace $fieldProperty[] values
if(count($fieldProperty)) {
foreach ($fieldProperty as $key => $val) {
$functionCall = $page instanceof Page ? $page : wire('page');
if(in_array($val['value'], self::$allowedPageFunctions)) {
$field_name = "";
$prefix = "";
$suffix = "";
if(is_array($val['args'])) {
if(!empty($val['args'][0])) $field_name = $val['args'][0];
if(!empty($val['args'][1])) $prefix = $val['args'][1];
if(!empty($val['args'][2])) $suffix = $val['args'][2];
} else $field_name = $val['args'];
if($field_name && $functionCall->template->fields($field_name)) {
$field = $functionCall->fields($field_name);
$property = $field->get($val['value'] . $language);
if($property) $property = $prefix . $property . $suffix;
$functionCall = $property;
}
}
}
}
return $functionCall;
}
// ------------------------------------------------------------------------
/**
* Check value: is function, have args or arg ?
* @param string $value
* @return array
*/
protected static function isFunction($value = "") {
$return = [
'isFunction' => false,
'value' => $value,
'args' => ''
];
if(preg_match("/(?<args>\(([^\)]*)\))/", $value, $arg)) {
$args = self::element('args', $arg, []);
$return['value'] = str_replace($args, '', $value);
$args = strtr($args, ['(' => '', ')' => '']);
$args = explode('~', $args);
if(count($args) == 1) $args = $args[0];
$return['isFunction'] = true;
$return['args'] = $args;
}
return $return;
}
}
?>
Usage :: original => inside string => result
<?php
$str = "
<ul class='uk-list uk-list-striped'>
<li><b>01: GET FIELD LABEL</b> <code>&#123;label(title)&#125;</code> <b>RESULT :</b> <code>{label(title)}</code></li>
<li><b>02: GET FIELD LABEL WITH PREFIX</b> <code>&#123;label(title~=> )&#125;</code> <b>RESULT :</b> <code>{label(title~=> )}</code></li>
<li><b>03: GET FIELD LABEL WITH SUFFIX</b> <code>&#123;label(title~~ <=)&#125;</code> <b>RESULT :</b> <code>{label(title~~ <=)}</code></li>
<li><b>04: GET FIELD DESCRIPTION</b> <code>&#123;description(title)&#125;</code> <b>RESULT :</b> <code>{description(title)}</code><br>
<li><b>05: GET FIELD DESCRIPTION WITH PREFIX</b> <code>&#123;description(title~=> )&#125;</code> <b>RESULT :</b> <code>{description(title~=> )}</code></li>
<li><b>06: GET FIELD DESCRIPTION WITH SUFFIX</b> <code>&#123;description(title~~ <=)&#125;</code> <b>RESULT :</b> <code>{description(title~~ <=)}</code></li>
<li><b>07: GET FIELD NOTES</b> <code>&#123;notes(title)&#125;</code> <b>RESULT :</b> <code>{notes(title)}</code><br>
<li><b>08: GET FIELD NOTES WITH PREFIX</b> <code>&#123;notes(title~=> )&#125;</code> <b>RESULT :</b> <code>{notes(title~=> )}</code></li>
<li><b>09: GET FIELD NOTES WITH SUFFIX</b> <code>&#123;notes(title~~ <=)&#125;</code> <b>RESULT :</b> <code>{notes(title~~ <=)}</code></li>
<li><b>10: GET PAGE TITLE</b> <code>&#123;page(title)&#125;</code> <b>RESULT :</b> <code>{page(title)}</code></li>
<li><b>11: GET PAGE TITLE</b> <code>&#123;page:title&#125;</code> <b>RESULT :</b> <code>{page:title}</code></li>
<li><b>12: GET PAGE RENDER TITLE</b> <code>&#123;page:render:title&#125;</code> <b>RESULT :</b> <code>{page:render:title}</code></li>
<li><b>12: GET HOMEPAGE TITLE</b> <code>&#123;pages:get(template=home):title&#125;</code> <b>RESULT :</b> <code>{pages:get(template=home):title}</code></li>
<li><b>13: GET HOMEPAGE TEMPLATE ID</b> <code>&#123;pages:get(template=home):template:id&#125;</code> <b>RESULT :</b> <code>{pages:get(template=home):template:id}</code></li>
</ul>
";
$processString = new ProcessString();
echo $processString::processString($str);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment