Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Class Text
  5. * @package App
  6. * @method static int length(string $string)
  7. * @method static string replace(string $string, string $search, string $replace)
  8. * @method static mixed index(string $string, string $search)
  9. * @method static string upper(string $string)
  10. * @method static string lower(string $string)
  11. * @method static string capitalize(string $string)
  12. */
  13. class Text
  14. {
  15. /**
  16. * @var array
  17. */
  18. public static $map = [
  19. 'length' => [
  20. 'name' => 'strlen',
  21. ],
  22. 'replace' => [
  23. 'name' => 'str_replace',
  24. 'arguments' => [1, 2, 0],
  25. ],
  26. 'index' => [
  27. 'name' => 'strpos',
  28. ],
  29. 'upper' => [
  30. 'name' => 'strtoupper',
  31. ],
  32. 'lower' => [
  33. 'name' => 'strtolower',
  34. ],
  35. 'capitalize' => [
  36. 'name' => 'ucwords',
  37. ],
  38. ];
  39.  
  40. /**
  41. * @param $name
  42. * @param $arguments
  43. * @return mixed
  44. * @throws Exception
  45. */
  46. public static function __callStatic($name, $arguments)
  47. {
  48. if (!isset(static::$map[$name])) {
  49. throw new Exception("Function `{$name}` not found");
  50. }
  51. $function = static::$map[$name]['name'];
  52. $parameters = static::parameters($name, $arguments);
  53.  
  54. return call_user_func_array($function, $parameters);
  55. }
  56.  
  57. /**
  58. * @param $name
  59. * @param $arguments
  60. * @return array
  61. * @throws Exception
  62. */
  63. private static function parameters($name, $arguments)
  64. {
  65. if (!isset(static::$map[$name]['arguments'])) {
  66. return $arguments;
  67. }
  68.  
  69. $parameters = [];
  70. foreach (static::$map[$name]['arguments'] as $index) {
  71. if (!isset($arguments[$index])) {
  72. throw new Exception("Invalid arguments to `{$name}`");
  73. }
  74. $parameters[] = $arguments[$index];
  75. }
  76. return $parameters;
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement