1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87:
<?php
namespace Net\Bazzline\Component\CodeGenerator;
class LineGenerator extends AbstractContentGenerator
{
private $content = [];
private $separator = ' ';
public function add($content)
{
if (is_string($content)) {
$this->content[] = $content;
} else if ($content instanceof LineGenerator) {
$this->content[] = $content->generate();
} else if (is_array($content)) {
foreach ($content as $part) {
$this->add($part);
}
} else if ($content instanceof AbstractContentGenerator) {
$content->setIndention($this->getIndention());
if ($content->hasContent()) {
$this->content[] = $content->generate();
}
} else {
throw new InvalidArgumentException('content has to be string, an array or instance of AbstractContentGenerator');
}
return $this;
}
public function setSeparator($separator)
{
$this->separator = (string) $separator;
}
public function clear()
{
$this->content = [];
}
public function hasContent()
{
return (!empty($this->content));
}
public function generate()
{
return (implode('', $this->content) !== '') ? $this->getIndention()->toString() . implode($this->separator, $this->content) : '';
}
public function count()
{
return (count($this->content));
}
}