Compare commits
3 Commits
fa7d635ee9
...
039fd102ae
Author | SHA1 | Date |
---|---|---|
|
039fd102ae | |
|
9dae115ad4 | |
|
e8bb01844e |
|
@ -33,6 +33,12 @@ class StringBuilder {
|
||||||
*/
|
*/
|
||||||
protected $varSuffix = "}}";
|
protected $varSuffix = "}}";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hält den String, welcher zwischen den Teilen in den Gesamt-String eingefügt wird.
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $seperator = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hält fest ob alle Teile des Strings einmalig (unique) sein sollen.
|
* Hält fest ob alle Teile des Strings einmalig (unique) sein sollen.
|
||||||
* @var bool
|
* @var bool
|
||||||
|
@ -49,6 +55,24 @@ class StringBuilder {
|
||||||
$this->vars = [];
|
$this->vars = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzte den Seperierungs String, welcher zwischen die Teile eingefügt wird beim zusammensetzen des gesamt-Strings.
|
||||||
|
* @param string $seperator Seperator der Teile im Gesamt-String
|
||||||
|
* @return StringBuilder $this
|
||||||
|
*/
|
||||||
|
public function setSeperator(string $seperator) : StringBuilder {
|
||||||
|
$this->seperator = $seperator;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abfrage des Seperators, welcher zwischen Teile im Gesamt-String eingefügt wird.
|
||||||
|
* @return string Seperators, welcher zwischen Teile im Gesamt-String eingefügt wird
|
||||||
|
*/
|
||||||
|
public function getSeperator() : string {
|
||||||
|
return $this->seperator;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prüft ob ein Teil vorhanden ist oder eine Zeichenkette insgesamt (variablen nicht ersetzt).
|
* Prüft ob ein Teil vorhanden ist oder eine Zeichenkette insgesamt (variablen nicht ersetzt).
|
||||||
* @param string $searchString Wonach soll gesucht werden?
|
* @param string $searchString Wonach soll gesucht werden?
|
||||||
|
@ -122,33 +146,33 @@ class StringBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Abfrage des aktuell gesetzten Prefix der Variablenbezeichnungen.
|
||||||
* @return string
|
* @return string Prefix der Variablenbezeichnungen
|
||||||
*/
|
*/
|
||||||
public function getVarPrefix() : string {
|
public function getVarPrefix() : string {
|
||||||
return $this->varPrefix;
|
return $this->varPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Abfrage des aktuell gesetzten Suffix der Variablenbezeichnungen
|
||||||
* @return string
|
* @return string Suffix der Variablenbezeichnungen
|
||||||
*/
|
*/
|
||||||
public function getVarSuffix() : string {
|
public function getVarSuffix() : string {
|
||||||
return $this->varSuffix;
|
return $this->varSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Abfrage aller Teile im StringBilder, aus welchem sich der String zusammensetzen wird.
|
||||||
* @return array
|
* @return array Alle Teile aus welchem sich der Strin zusammensetzen wird.
|
||||||
*/
|
*/
|
||||||
public function getParts() : array {
|
public function getParts() : array {
|
||||||
return $this->parts;
|
return $this->parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Ersetzt/Setzt alle aktuellen Teile, aus welchen sich der String zusammensetzen wird.
|
||||||
* @param array $parts
|
* @param array $parts Teile aus welchen sich der String zusammensetzen wird
|
||||||
* @return StringBuilder
|
* @return StringBuilder $this
|
||||||
*/
|
*/
|
||||||
public function setParts(array $parts=[]) : StringBuilder {
|
public function setParts(array $parts=[]) : StringBuilder {
|
||||||
$this->parts = $parts;
|
$this->parts = $parts;
|
||||||
|
@ -156,9 +180,9 @@ class StringBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Einen Teil am Ende des Strings hinzufügen.
|
||||||
* @param string $part
|
* @param string $part Teil des Strings welches hinzugefügt werden soll
|
||||||
* @return StringBuilder
|
* @return StringBuilder $this
|
||||||
*/
|
*/
|
||||||
public function append(string $part, bool $trim=false) : StringBuilder {
|
public function append(string $part, bool $trim=false) : StringBuilder {
|
||||||
if($trim) $part = trim($part);
|
if($trim) $part = trim($part);
|
||||||
|
@ -169,9 +193,9 @@ class StringBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Einen Teil am Ende des Strings hinzufügen, sofern nicht berits an beliebiger Stelle vorhanden.
|
||||||
* @param string $part
|
* @param string $part Teil des Strings welches hinzugefügt werden soll
|
||||||
* @return StringBuilder
|
* @return StringBuilder $this
|
||||||
*/
|
*/
|
||||||
public function appendUnique(string $part, bool $trim=false) : StringBuilder {
|
public function appendUnique(string $part, bool $trim=false) : StringBuilder {
|
||||||
if($trim) $part = trim($part);
|
if($trim) $part = trim($part);
|
||||||
|
@ -182,9 +206,9 @@ class StringBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Einen Teil am Anfang des Strings hinzufügen.
|
||||||
* @param string $part
|
* @param string $part Teil des Strings welches hinzugefügt werden soll
|
||||||
* @return StringBuilder
|
* @return StringBuilder $this
|
||||||
*/
|
*/
|
||||||
public function prepend(string $part, bool $trim=false) : StringBuilder {
|
public function prepend(string $part, bool $trim=false) : StringBuilder {
|
||||||
if($trim) $part = trim($part);
|
if($trim) $part = trim($part);
|
||||||
|
@ -195,9 +219,9 @@ class StringBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Einen Teil am Anfang des Strings hinzufügen, sofern nicht berits an beliebiger Stelle vorhanden.
|
||||||
* @param string $part
|
* @param string $part Teil des Strings welches hinzugefügt werden soll
|
||||||
* @return StringBuilder
|
* @return StringBuilder $this
|
||||||
*/
|
*/
|
||||||
public function prependUnique(string $part, bool $trim=false) : StringBuilder {
|
public function prependUnique(string $part, bool $trim=false) : StringBuilder {
|
||||||
if($trim) $part = trim($part);
|
if($trim) $part = trim($part);
|
||||||
|
@ -209,26 +233,24 @@ class StringBuilder {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Alle Teile zu einem String zusammenfügen.
|
||||||
* @param string $seperator
|
* @return string Zusammengesetzter gesamt String
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function join(string $seperator='') : string {
|
public function join() : string {
|
||||||
return implode($seperator, $this->parts);
|
return implode($this->getSeperator(), $this->parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Alle Teile zu einem String zusammenfügen, wärend doppelt vorkommende Teile nicht erneut vorkommen.
|
||||||
* @param string $seperator
|
* @return string Zusammengesetzter gesamt String
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function joinUnique(string $seperator='') : string {
|
public function joinUnique() : string {
|
||||||
return implode($seperator, array_unique($this->parts));
|
return implode($this->getSeperator(), array_unique($this->parts));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Alle Teile zu einem String zusammenfügen, und dann variablen im gesamt String ersetzen.
|
||||||
* @return string
|
* @return string Gesamt-String mit Variablen ersetzt.
|
||||||
*/
|
*/
|
||||||
public function render() : string {
|
public function render() : string {
|
||||||
$tpl = $this->join();
|
$tpl = $this->join();
|
||||||
|
@ -239,4 +261,19 @@ class StringBuilder {
|
||||||
}
|
}
|
||||||
return $tpl;
|
return $tpl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alle Teile zu einem String zusammenfügen, wärend doppelt vorkommende Teile nicht erneut vorkommen und dann variablen im gesamt String ersetzen.
|
||||||
|
* @return string Gesamt-String mit Variablen ersetzt.
|
||||||
|
*/
|
||||||
|
public function renderUnique() : string {
|
||||||
|
$tpl = $this->joinUnique();
|
||||||
|
foreach($this->vars as $key => $value) {
|
||||||
|
if(is_numeric($value) or is_string($value)) {
|
||||||
|
$tpl = preg_replace("/".$this->varPrefix."\s*".$key."\s*".$this->varSuffix."/", $value, $tpl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $tpl;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue