No Description
unmaintained
experiment
php

str.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * PHPLib
  4. *
  5. * A PHP library to wrap php functions in a standard fashion
  6. *
  7. * @package PHPLib
  8. * @author Timothy J. Warren
  9. * @copyright Copyright (c) 2013
  10. * @link https://github.com/aviat4ion/PHPLib
  11. * @license MIT
  12. */
  13. // --------------------------------------------------------------------------
  14. namespace PHPUtil;
  15. /**
  16. * Wrapper over php string functions with a consistent argument system
  17. */
  18. class Str {
  19. /**
  20. * Original string to be modified
  21. * @var string
  22. */
  23. protected $original;
  24. /**
  25. * Constructor
  26. *
  27. * @param string $string
  28. * @return Str
  29. */
  30. public function __construct($string)
  31. {
  32. $this->original = $string;
  33. }
  34. /**
  35. * Enable using the object as a function
  36. *
  37. * @param string $string
  38. * @return $this
  39. */
  40. public function __invoke($string)
  41. {
  42. $this->__construct($string);
  43. return $this;
  44. }
  45. /**
  46. * Output from the last method
  47. *
  48. * @return string
  49. */
  50. public function __toString()
  51. {
  52. return (is_string($this->original))
  53. ? $this->original
  54. : $this->_();
  55. }
  56. /**
  57. * Return output from the last method
  58. *
  59. * @return mixed
  60. */
  61. public function _()
  62. {
  63. return $this->original;
  64. }
  65. /**
  66. * Formats a string according to the pattern passed to the str() function
  67. *
  68. * @see http://php.net/manual/function.sprintf.php
  69. * @param array $replacements
  70. * @return $this
  71. */
  72. public function format(array $replacements)
  73. {
  74. // Add the pattern string as the first argument
  75. array_unshift($replacements, $this->original);
  76. // Return the formatted string
  77. $this->original = call_user_func_array('sprintf', $replacements);
  78. return $this;
  79. }
  80. /**
  81. * Check whether a string matches the provided pattern
  82. *
  83. * @param string $pattern
  84. * @return bool
  85. */
  86. public function match($pattern)
  87. {
  88. return (bool) preg_match($pattern, $this->original);
  89. }
  90. /**
  91. * Do a basic string replacement
  92. *
  93. * @see http://php.net/manual/function.str-replace.php
  94. * @param mixed $pattern
  95. * @param mixed $replacement
  96. * @return $this
  97. */
  98. public function replace($pattern, $replacement=NULL)
  99. {
  100. if (is_array($pattern) && is_null($replacement))
  101. {
  102. $replacement = array_values($pattern);
  103. $pattern = array_keys($pattern);
  104. }
  105. $this->original = str_replace($pattern, $replacement, $this->original);
  106. return $this;
  107. }
  108. /**
  109. * String replacement via a regular expression
  110. *
  111. * @see http://php.net/manual/function.preg-replace.php
  112. * @param mixed $pattern
  113. * @param mixed $replacement
  114. * @return $this
  115. */
  116. public function reg_replace($pattern, $replacement)
  117. {
  118. $this->original = preg_replace($pattern, $replacement, $this->original);
  119. return $this;
  120. }
  121. }
  122. // End of str.php