Created
June 6, 2010 19:30
-
-
Save MartinSadovy/427814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'Nette/loader.php'; | |
require 'stringFluent.php'; | |
$string = string("ahoj já jsem petr")->capitalize(); | |
echo $string."\n"; | |
echo $string->webalize(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Nette\Object; | |
use Nette\String; | |
/** | |
* String fluent | |
* @copyright Copyright (c) 2010 Martin Sadový | |
* TODO: startsWith, endsWith, compare, indent, padLeft, padRight // yes or no? | |
* TODO: clone or reference? | |
*/ | |
class StringFluent extends Object | |
{ | |
/** | |
* @var string | |
*/ | |
protected $content; | |
public function __construct($content) | |
{ | |
if(is_object($content) AND method_exists($content, '__toString') ){ | |
$this->content = String::fixEncoding($content->__toString()); | |
}elseif(is_scalar($content)){ | |
$this->content = String::fixEncoding((string) $content); | |
}else{ | |
throw new InvalidArgumentException("The argument must be either scalar or an object with method __toString, " . gettype($val) ." given."); | |
} | |
} | |
/****** Modification methods ******/ | |
/** | |
* Convert to lower case. | |
* @return StringFluent | |
*/ | |
public function lower() | |
{ | |
$this->content = String::lower($this->content); | |
return $this; | |
} | |
/** | |
* Convert to upper case. | |
* @return StringFluent | |
*/ | |
public function upper() | |
{ | |
$this->content = String::upper($this->content); | |
return $this; | |
} | |
/** | |
* Capitalize string. | |
* @return StringFluent | |
*/ | |
public function capitalize() | |
{ | |
$this->content = String::capitalize($this->content); | |
return $this; | |
} | |
/** | |
* Converts to web safe characters [a-z0-9-] text. | |
* @param string ASCII | |
* @param bool | |
* @return StringFluent | |
*/ | |
public function webalize($charlist = NULL, $lower = TRUE) | |
{ | |
$this->content = String::webalize($this->content, $charlist, $lower); | |
return $this; | |
} | |
/** | |
* Capitalize string. | |
* @param int | |
* @param string UTF-8 encoding | |
* @return StringFluent | |
*/ | |
public function truncate($maxLength, $append = NULL) | |
{ | |
$this->content = $append === NULL ? String::truncate($this->content, $maxLength) : String::truncate($this->content, $maxLength, $append); | |
return $this; | |
} | |
/** | |
* Removes special controls characters and normalizes line endings and spaces. | |
* @return StringFluent | |
*/ | |
public function normalize() | |
{ | |
$this->content = String::normalize($this->content); | |
return $this; | |
} | |
/** | |
* Strips whitespace. | |
* @param string | |
* @return StringFluent | |
*/ | |
public function trim($charlist = NULL) | |
{ | |
$this->content = $charlist === NULL ? String::trim($this->content) : String::trim($this->content, $charlist); | |
return $this; | |
} | |
/****** Regular methods ******/ | |
/** | |
* Splits string by a regular expression. | |
* @param string | |
* @param int | |
* @return array | |
*/ | |
public function split($pattern, $flags = 0) | |
{ | |
return String::split($this->content, $pattern, $flags); | |
} | |
/** | |
* Performs a regular expression match. | |
* @param string | |
* @param int | |
* @param int | |
* @return mixed | |
*/ | |
public function match($pattern, $flags = 0, $offset = 0) | |
{ | |
return String::match($this->content, $pattern, $flags, $offset); | |
} | |
/** | |
* Performs a global regular expression match. | |
* @param string | |
* @param int (PREG_SET_ORDER is default) | |
* @param int | |
* @return array | |
*/ | |
public function matchAll($pattern, $flags = 0, $offset = 0) | |
{ | |
return String::matchAll($this->content, $pattern, $flags, $offset); | |
} | |
/** | |
* Perform a regular expression search and replace. | |
* @param string|array | |
* @param string|callback | |
* @param int | |
* @return StringFluent | |
*/ | |
public function replace($pattern, $replacement = NULL, $limit = -1) | |
{ | |
$this->content = String::replace($this->content, $pattern, $replacement, $limit); | |
return $this; | |
} | |
/****** Return methods ******/ | |
/** | |
* Return length string | |
* @return int | |
*/ | |
public function getLength() | |
{ | |
return String::length($this->content); | |
} | |
/** | |
* Return string | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->content; | |
} | |
} | |
/** | |
* @return StringFluent | |
*/ | |
function string($string) | |
{ | |
return new StringFluent($string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment