/** * @param $value * @return null|object * @example * ```php * $parsed = Expression::parse('myFunction(10)'); * * print_r($parsed); * * //Will output * //stdClass Object * //( * // [key] => myFunction * // [mode] => function * // [parameters] => [6] * //) * ``` */ public static function parse($value) { $expression = Expression::getExpression(); $return = preg_match($expression, $value, $matches); if (!$return) { return null; } $parsed = new \stdClass(); $parsed->key = $matches['key']; $parsed->mode = Globals::KEY_VARIABLE; if (isset($matches['function']) && !empty($matches['function'])) { $parsed->mode = Globals::KEY_FUNCTION; if (key_exists('parameters', $matches) && strlen((string) $matches['parameters'])) { //Handles only one parameter $parsed->parameters = [$matches['parameters']]; } } elseif (isset($matches['array']) && !empty($matches['array'])) { $parsed->mode = Globals::KEY_ARRAY; //should exists $parsed->index = $matches['index']; } return $parsed; }
/** * Replace incline and full replace key inside a test object * @param object|array $inputObject * @return object|array * @todo add special exception, when global is not found test should * be ignored with special warning (EG unavailable) */ private function searchAndReplace($inputObject) { if (is_object($inputObject)) { $object = clone $inputObject; } else { $object = $inputObject; } $pattern_replace = Expression::getExpression(Expression::EXPRESSION_TYPE_REPLACE); $pattern_inline_replace = Expression::getExpression(Expression::EXPRESSION_TYPE_INLINE_REPLACE); list($prefix, $suffix) = Expression::getPrefixSuffix(Expression::EXPRESSION_TYPE_INLINE_REPLACE); foreach ($object as $key => &$value) { if (is_array($value) || is_object($value)) { $value = $this->searchAndReplace($value); } if (is_string($value)) { $matches = []; //Complete replace if (!!preg_match($pattern_replace, $value, $matches)) { $globalsKey = $matches['value']; //replace $value = Globals::get($globalsKey); } elseif (!!preg_match_all($pattern_inline_replace, $value, $matches)) { //Foreach variable replace in string foreach ($matches['value'] as $globalsKey) { $value = str_replace($prefix . $globalsKey . $suffix, Globals::get($globalsKey), $value); } } } } return $object; }