Posts tagged builder
PHP String Builder
Jan 26th
PHP String Builder using array()
<?php
class TStringBuilder {
/**
* Number of items added to Collection
* @var int
*/
private $count = null;
/**
* String Array
* @var array
*/
private $str = null;
/**
* Initializes a new instance
*/
public function __construct() {
$this->count = 0;
$this->str = array();
}
/**
* Adds new string
* @param string $str
*/
public function add($str) {
++$this->count;
$this->str[] = $str;
return $this;
}
/**
* Clears the string
*/
public function clear() {
$this->count = 0;
$this->str = array();
}
/**
* Gets internal array
*/
public function get() {
return $this->str;
}
/**
* Dumps string
* @param string $seperator
* @return string
*/
public function dump($seperator = "") {
$retVal = "";
foreach ($this->str as $k) {
$retVal .= $k . $seperator;
}
return substr($retVal, 0, strlen($retVal) - strlen($seperator));
}
/**
* Dumps string continiously
*/
public function dumpc() {
foreach ($this->str as $k) {
echo $k;
}
}
/**
* Get number of enteries added
* @return int
*/
public function count() {
return $this->count;
}
/**
* Get string length
* @return int
*/
public function length() {
$retVal = 0;
foreach ($this->str as $k) {
$retVal += strlen($k);
}
return retVal;
}
}
PHP JSON Builder
Jan 26th
<?php
class TJSONBuilder extends Countable {
/**
* JSON collection object
* @var unknown_type
*/
private $json = null;
/**
* Number of items added to json collection
* @var unknown_type
*/
private $count = 0;
/**
* Initializes a new instance of the TJSONBuilder class
*/
public function __construct() {
$this->json = new stdClass();
}
/**
* Insert new node
* @param string $json_string
* @param boolean $assoc
* @param integer $deep
*/
public function add($json_string, $assoc = false, $deep = 5) {
$id = null;
$decode = json_decode($json_string, $assoc, $deep);
foreach ($decode as $k => $v) {
$id = $k;
break;
}
if (isset($id)) {
$this->json->{$id} = $decode->{$id};
++$this->count;
}
}
/**
* Clear nodes
*/
public function clear() {
$this->json = new stdClass();
$this->count = 0;
}
/**
* Get json class
* @return Ambiguous
*/
public function get() {
return $this->json;
}
/**
* Dump as json string
* @param constant $json_options
* @return string
*/
public function toString($json_options = JSON_FORCE_OBJECT) {
return json_encode($this->json, $json_options);
}
/**
* Get number of nodes
* @return int
*/
public function count() {
return $this->count;
}
}


LinkedIn
Twitter
Skype