Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Class Text
- * @package App
- * @method static int length(string $string)
- * @method static string replace(string $string, string $search, string $replace)
- * @method static mixed index(string $string, string $search)
- * @method static string upper(string $string)
- * @method static string lower(string $string)
- * @method static string capitalize(string $string)
- */
- class Text
- {
- /**
- * @var array
- */
- public static $map = [
- 'length' => [
- 'name' => 'strlen',
- ],
- 'replace' => [
- 'name' => 'str_replace',
- 'arguments' => [1, 2, 0],
- ],
- 'index' => [
- 'name' => 'strpos',
- ],
- 'upper' => [
- 'name' => 'strtoupper',
- ],
- 'lower' => [
- 'name' => 'strtolower',
- ],
- 'capitalize' => [
- 'name' => 'ucwords',
- ],
- ];
- /**
- * @param $name
- * @param $arguments
- * @return mixed
- * @throws Exception
- */
- public static function __callStatic($name, $arguments)
- {
- if (!isset(static::$map[$name])) {
- throw new Exception("Function `{$name}` not found");
- }
- $function = static::$map[$name]['name'];
- $parameters = static::parameters($name, $arguments);
- return call_user_func_array($function, $parameters);
- }
- /**
- * @param $name
- * @param $arguments
- * @return array
- * @throws Exception
- */
- private static function parameters($name, $arguments)
- {
- if (!isset(static::$map[$name]['arguments'])) {
- return $arguments;
- }
- $parameters = [];
- foreach (static::$map[$name]['arguments'] as $index) {
- if (!isset($arguments[$index])) {
- throw new Exception("Invalid arguments to `{$name}`");
- }
- $parameters[] = $arguments[$index];
- }
- return $parameters;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement