PHP Classes

Allow implicit sets

Recommend this page to a friend!

      PHP String  >  All threads  >  Allow implicit sets  >  (Un) Subscribe thread alerts  
Subject:Allow implicit sets
Summary:It might be useful if underlying string is altered
Messages:1
Author:Paul Johnston
Date:2012-11-26 22:38:15
 

  1. Allow implicit sets   Reply   Report abuse  
Picture of Paul Johnston Paul Johnston - 2012-11-26 22:38:15
When I first looked at the "concat" method I thought it might be useful if concat actually changed the underlying source string. And then I looked at a few others and thought it would be good to allow implicit sets either globally or per method while at the same time keeping your original intent. So, these are the changes ...

NOTE: no comments added since different human language
NOTE2: Sorry about the loss of indents
---------------

class PHPString{
//@var string $value O conteúdo da string.
protected $value;
protected $doImplicitSets=false; //PRJ 2012.11.26

//Método estático usado para quebra de linha.
public static function nl(){
echo "\n";
}
//Método estático usado para pular linha em tag html.
public static function br(){
echo "<br />";
}
/*
Método construtor
@param string $txt Texto a ser inserido no conteúdo da string.
@param boolean $implicitSet Allows Implicit Sets
*/
public function __construct($txt = '',$implicitSet=false){
$this->value = $txt;
$this->doImplicitSets = $implicitSet;
}
public function allowImplicitSet($setting) {
$this->doImplicitSets = $setting;
}
private function applyImplicitSet($newvalue , $ForcedSet = false) {
if($this->doImplicitSets || $ForcedSet) $this->value=$newvalue;
return $newvalue;
}
/*
Método mágico - Converte o objeto para string.
@return O conteúdo da string.
*/
public function __toString(){
return $this->value;
}
//Método estático usado para exibir texto.
public static function write($string = ""){
echo $string;
}
/*
@return string O conteúdo da string em maiúsculo.
*/
public function toUpperCase($ForcedSet = false){
return $this->applyImplicitSet( strtoupper($this->value) , $ForcedSet );
}
/*
@return string O conteúdo da string em minúsculo.
*/
public function toLowerCase($ForcedSet = false){
return $this->applyImplicitSet( strtolower($this->value) , $ForcedSet );
}
/*

@param string $delimiter Termo usado como critério para dividir o conteúdo da string.
@return array O vetor do conteúdo da string separado pelo $delimiter;
*/
public function split($delimiter = ''){
if($delimiter){
return explode($delimiter, $this->value);
}
return array();
}
/*
@param int $index Posição do conteúdo da string a ser retornado.
@return string Caractere do conteúdo da string na posição $index.
*/
public function charAt($index = 0){
return substr($this->value, $index, 1);
}
/*
@return string O hash do conteúdo do conteúdo da string.
*/
public function sha1(){
return sha1($this->value);
}
/*
@return string A string atual sem os espaços extras antes e depois do conteúdo.
*/
public function trim($ForcedSet = false){
return $this->applyImplicitSet( trim($this->value) , $ForcedSet );
}
/*
@param string $complement Complemento de texto a ser concatenado com o valor atual da string.
@return string O conteúdo da string concatenado com o complemento.
*/
public function concat($complement = "",$ForcedSet = false){
return $this->applyImplicitSet( $this->value . $complement , $ForcedSet );
}
/*
@return int O comprimento do conteúdo da string.
*/
public function length(){
return strlen($this->value);
}
/*
@param mixed $search Texto ou Expressão regular a ser pesquisado para substituição
@param string $replacement Texto a ocupar o espaço do texto que combinar com a pesquisa.
@return string A string com o $search substituído por $replacement.
*/
public function replace($search = '', $replacement = '', $ForcedSet = false){
if(!$search && !$replacement){
return $this->value;
}
if(substr($search, 0, 1) == '/' && substr($search, -1, 1) == '/' && strlen($search) > 2){
$search = ereg_replace('^/', '', $search);
$search = ereg_replace('/$', '', $search);
return $this->applyImplicitSet( ereg_replace($search, $replacement, $this->value) , $ForcedSet );
}else{
return $this->applyImplicitSet( str_replace($search, $replacement, $this->value) , $ForcedSet );
}
}
/*
Exibe o conteúdo da string.
*/
public function display(){
echo $this->value;
}
/*
Define um novo conteúdo para a string.
@param string $txt Conteúdo da string.
*/
public function set($txt = ''){
$this->value = $txt;
}
/*
Retorna uma parte de uma string.
@param int $begin Posição da string a qual começará o 'recorte'.
@param int $end Posição da string a qual terminará o 'recorte'.
@observation Caso não se passe um $end, o método retornará a string
a partir da posição $begin até o final da mesma.
@observation Caso não seja passado parâmetro algum, o método retorna a string inteira.
*/
public function slice($begin = 0, $end){
if(!isset($end)){
$end = $this->length();
}
if($end <= $begin){
return "";
}
return substr($this->value, $begin, $end - $begin);
}
}
}