Example #1
0
 /**
  * Creates an instance of Stringy and invokes the given method with the
  * rest of the passed arguments. The optional encoding is expected to be
  * the last argument. For example, the following:
  * StaticStringy::slice('fòôbàř', 0, 3, 'UTF-8'); translates to
  * Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
  * The result is not cast, so the return value may be of type Stringy,
  * integer, boolean, etc.
  *
  * @param string  $name
  * @param mixed[] $arguments
  */
 public static function __callStatic($name, $arguments)
 {
     if (!isset(static::$methodArgs[$name])) {
         throw new \BadMethodCallException($name . ' is not a valid method');
     }
     $numArgs = count($arguments);
     $str = $numArgs ? $arguments[0] : '';
     if ($numArgs === static::$methodArgs[$name]) {
         $args = array_slice($arguments, 1, -1);
         $encoding = $arguments[$numArgs - 1];
     } else {
         $args = array_slice($arguments, 1);
         $encoding = null;
     }
     $stringy = Stringy::create($str, $encoding);
     return call_user_func_array(array($stringy, $name), $args);
 }
Example #2
0
 /**
  * Returns a trimmed string with the first letter of each word capitalized.
  * Also accepts an array, $ignore, allowing you to list words not to be
  * capitalized.
  *
  * @param  array   $ignore An array of words not to capitalize
  * @return Stringy Object with a titleized $str
  */
 public function titleize($ignore = null)
 {
     $stringy = static::create($this->trim(), $this->encoding);
     $encoding = $this->encoding;
     $stringy->str = preg_replace_callback('/([\\S]+)/u', function ($match) use($encoding, $ignore) {
         if ($ignore && in_array($match[0], $ignore)) {
             return $match[0];
         } else {
             $stringy = new Stringy($match[0], $encoding);
             return (string) $stringy->toLowerCase()->upperCaseFirst();
         }
     }, $stringy->str);
     return $stringy;
 }
Example #3
0
 /**
  * Replaces all occurrences of $pattern in $str by $replacement. An alias
  * for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
  * in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
  * in the bundled version of Oniguruma in PHP 5.3.
  *
  * @param  string $str         The haystack to search through
  * @param  string $pattern     The regular expression pattern
  * @param  string $replacement The string to replace with
  * @param  string $options     Matching conditions to be used
  * @param  string $encoding    The character encoding
  * @return string The resulting string after the replacements
  */
 public static function regexReplace($str, $pattern, $replacement, $options = 'msr', $encoding = null)
 {
     return (string) Stringy::create($str, $encoding)->regexReplace($pattern, $replacement, $options, $encoding);
 }
 /**
  * Convert all HTML entities to their applicable characters.
  *
  * @param  string   $str   The string to decode.
  * @param  int|null $flags See http://php.net/manual/en/function.html-entity-decode.php
  * @param  string   $encoding    The character encoding
  * @return Stringy  Object with the resulting $str after being html decoded.
  */
 public static function htmlDecode($str, $flags = ENT_COMPAT, $encoding = null)
 {
     return (string) Stringy::create($str, $encoding)->htmlDecode($flags);
 }
Example #5
0
 /**
  * Creates an instance of Stringy and invokes the given method with the
  * rest of the passed arguments. The optional encoding is expected to be
  * the last argument. For example, the following:
  * StaticStringy::slice('fòôbàř', 0, 3, 'UTF-8'); translates to
  * Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
  * The result is not cast, so the return value may be of type Stringy,
  * integer, boolean, etc.
  *
  * @param string  $name
  * @param mixed[] $arguments
  *
  * @return Stringy
  */
 public static function __callStatic($name, $arguments)
 {
     if (!static::$methodArgs) {
         $stringyClass = new \ReflectionClass('Stringy\\Stringy');
         $methods = $stringyClass->getMethods(\ReflectionMethod::IS_PUBLIC);
         foreach ($methods as $method) {
             $params = $method->getNumberOfParameters() + 2;
             static::$methodArgs[$method->name] = $params;
         }
     }
     if (!isset(static::$methodArgs[$name])) {
         throw new \BadMethodCallException($name . ' is not a valid method');
     }
     $numArgs = count($arguments);
     $str = $numArgs ? $arguments[0] : '';
     if ($numArgs === static::$methodArgs[$name]) {
         $args = array_slice($arguments, 1, -1);
         $encoding = $arguments[$numArgs - 1];
     } else {
         $args = array_slice($arguments, 1);
         $encoding = null;
     }
     $stringy = Stringy::create($str, $encoding);
     return call_user_func_array(array($stringy, $name), $args);
 }
Example #6
0
 /**
  * Returns a trimmed string with the first letter of each word capitalized.
  * Ignores the case of other letters, preserving any acronyms. Also accepts
  * an array, $ignore, allowing you to list words not to be capitalized.
  *
  * @param  array   $ignore An array of words not to capitalize
  * @return Stringy Object with a titleized $str
  */
 public function titleize($ignore = null)
 {
     $buffer = $this->trim();
     $encoding = $this->encoding;
     $buffer = preg_replace_callback('/([\\S]+)/u', function ($match) use($encoding, $ignore) {
         if ($ignore && in_array($match[0], $ignore)) {
             return $match[0];
         } else {
             $stringy = new Stringy($match[0], $encoding);
             return (string) $stringy->upperCaseFirst();
         }
     }, $buffer);
     return new Stringy($buffer, $encoding);
 }
Example #7
0
 /**
  * @covers Stringy::sentenceCase
  * @uses Stringy
  */
 public function testSentenceCase()
 {
     $upper = new Stringy("first sentence. second sentence.");
     $this->assertContains("First sentence. Second sentence.", $upper->sentenceCase()->string());
 }