/** * Get global key's value. * An expression to access array elements or a function can be given. * @param string $key Expression key, can have parenthesis or square brackets operator. * @return mixed|callable|array If you access a function without the * parenthesis operator or an array without square brackets operator * then this method will return the callable or the whole array respectively. * @example * ```php * Globals::get('myVarible'); * * Globals::get('rand-boolean()'); * * Globals::get('rand-integer(10)'); //A random integer from 0 to 10 * * Globals::get('myArray[1]'); //Get second array element * ``` * @throws \Exception When expression key is invalid. * @throws \Phramework\Exceptions\NotFoundException When key is not found. */ public static function get($key = null, $operators = null) { if (static::$globals === null) { static::initializeGlobals(); } if ($key !== null) { $parsed = Expression::parse($key); if ($parsed === null) { throw new \Exception(sprintf('Invalid key "%s"', $key)); } if (!static::exists($parsed->key)) { throw new UnsetGlobalException($parsed->key); } $global = static::$globals->{$parsed->key}; switch ($parsed->mode) { case Globals::KEY_FUNCTION: $functionParameters = []; if (property_exists($parsed, 'parameters')) { $functionParameters = Globals::handleFunctionVariable($parsed->parameters); } return call_user_func_array($global, $functionParameters); case Globals::KEY_ARRAY: return $global[(int) Globals::handleArrayVariable($parsed->index)]; case Globals::KEY_VARIABLE: default: return $global; } } return static::$globals; }
/** * @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; }