klasse erstellt, dokumentation unvollständig und tests fehlen noch.
This commit is contained in:
parent
2cf911e0be
commit
fa7d635ee9
|
@ -0,0 +1,242 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace NAE\String;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasse um einen String aus Teil-String aufzubauen und ggf. Variablen im gesamt String zu ersetzen.
|
||||||
|
* @author Marcel Naeve <php@naeve.info>
|
||||||
|
* @package NAE\String
|
||||||
|
*/
|
||||||
|
class StringBuilder {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hält die einzelnen Teile des Strings.
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $parts = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hält die Variablen als key => value.
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $vars = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hält den Prefix (Erster Teil/Anfang) der Variablenbezeichnungen.
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $varPrefix = "{{";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hält den Suffix (Letzter Teil/Abschluss) der Variablenbezeichnungen.
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $varSuffix = "}}";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hält fest ob alle Teile des Strings einmalig (unique) sein sollen.
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $unique = false;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(bool $unique=false) {
|
||||||
|
$this->parts = [];
|
||||||
|
$this->vars = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prüft ob ein Teil vorhanden ist oder eine Zeichenkette insgesamt (variablen nicht ersetzt).
|
||||||
|
* @param string $searchString Wonach soll gesucht werden?
|
||||||
|
* @param bool $wholePart Ein Teil suchen? (true) Oder eine Zeichenkette? (false)
|
||||||
|
* @return bool Enthalten?
|
||||||
|
*/
|
||||||
|
public function contains(string $searchString, bool $wholePart=true) {
|
||||||
|
if($wholePart === true) {
|
||||||
|
return in_array($searchString, $this->parts);
|
||||||
|
} else {
|
||||||
|
$tpl = implode('', $this->parts);
|
||||||
|
return strpos($tpl, $searchString) !== false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prüft ob eine Variable mit einem bestimmten key vorhanden ist.
|
||||||
|
* @param string $key Schlüssel nach dem gesucht werden soll.
|
||||||
|
* @return bool Vorhanden?
|
||||||
|
*/
|
||||||
|
public function hasVar(string $key) : bool {
|
||||||
|
return array_key_exists(trim($key), $this->vars);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt den Wert einer Variablen.
|
||||||
|
* @param string $key Schlüssel/Name der Variablen.
|
||||||
|
* @param string $value Wert der gesetzt werden soll.
|
||||||
|
* @return StringBuilder
|
||||||
|
*/
|
||||||
|
public function setVar(string $key, string $value) : StringBuilder {
|
||||||
|
$this->vars[trim($key)] = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fragt der Wert einer Variablen ab, anhand des Schlüssels/Namens der Variable.
|
||||||
|
* @param string $key Schlüssel/Name der Variablen.
|
||||||
|
* @return string Wert der Variablen als String.
|
||||||
|
*/
|
||||||
|
public function getVar(string $key) : string {
|
||||||
|
return $this->vars[trim($key)];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abfrage aller Variablen als Array (key => value).
|
||||||
|
* @return array Alle Variablen Werte.
|
||||||
|
*/
|
||||||
|
public function getVars() : array {
|
||||||
|
return $this->vars;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt den Prefix der Variablen default: '{{'
|
||||||
|
* @param string $prefix Prefix (default: '{{')
|
||||||
|
* @return StringBuilder $this
|
||||||
|
*/
|
||||||
|
public function setVarPrefix(string $prefix="{{") : StringBuilder {
|
||||||
|
$this->varPrefix = $prefix;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt den Suffix der Variablen default: '}}'
|
||||||
|
* @param string $suffix Suffix (default: '}}')
|
||||||
|
* @return StringBuilder $this
|
||||||
|
*/
|
||||||
|
public function setVarSuffix(string $suffix="}}") : StringBuilder {
|
||||||
|
$this->varSuffix = $suffix;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getVarPrefix() : string {
|
||||||
|
return $this->varPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getVarSuffix() : string {
|
||||||
|
return $this->varSuffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getParts() : array {
|
||||||
|
return $this->parts;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param array $parts
|
||||||
|
* @return StringBuilder
|
||||||
|
*/
|
||||||
|
public function setParts(array $parts=[]) : StringBuilder {
|
||||||
|
$this->parts = $parts;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $part
|
||||||
|
* @return StringBuilder
|
||||||
|
*/
|
||||||
|
public function append(string $part, bool $trim=false) : StringBuilder {
|
||||||
|
if($trim) $part = trim($part);
|
||||||
|
if($this->unique === false or $this->contains($part) === false) {
|
||||||
|
$this->parts[] = $part;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $part
|
||||||
|
* @return StringBuilder
|
||||||
|
*/
|
||||||
|
public function appendUnique(string $part, bool $trim=false) : StringBuilder {
|
||||||
|
if($trim) $part = trim($part);
|
||||||
|
if($this->contains($part) === false) {
|
||||||
|
$this->parts[] = $part;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $part
|
||||||
|
* @return StringBuilder
|
||||||
|
*/
|
||||||
|
public function prepend(string $part, bool $trim=false) : StringBuilder {
|
||||||
|
if($trim) $part = trim($part);
|
||||||
|
if($this->unique === false or $this->contains($part) === false) {
|
||||||
|
array_unshift($this->parts, $part);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $part
|
||||||
|
* @return StringBuilder
|
||||||
|
*/
|
||||||
|
public function prependUnique(string $part, bool $trim=false) : StringBuilder {
|
||||||
|
if($trim) $part = trim($part);
|
||||||
|
if($this->contains($part) === false) {
|
||||||
|
array_unshift($this->parts, $part);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $seperator
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function join(string $seperator='') : string {
|
||||||
|
return implode($seperator, $this->parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $seperator
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function joinUnique(string $seperator='') : string {
|
||||||
|
return implode($seperator, array_unique($this->parts));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function render() : string {
|
||||||
|
$tpl = $this->join();
|
||||||
|
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