1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author sleibelt |
4
|
|
|
* @since 2014-04-25 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\CodeGenerator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class LineGenerator |
11
|
|
|
* |
12
|
|
|
* @package Net\Bazzline\Component\Locator\LocatorGenerator |
13
|
|
|
*/ |
14
|
|
|
class LineGenerator extends AbstractContentGenerator |
15
|
|
|
{ |
16
|
|
|
/** @var array|string[] */ |
17
|
|
|
private $content = []; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $separator = ' '; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string|array|GeneratorInterface[]|AbstractContentGenerator[] $content |
24
|
|
|
* @return $this |
25
|
|
|
* @throws InvalidArgumentException |
26
|
|
|
*/ |
27
|
|
|
public function add($content) |
28
|
|
|
{ |
29
|
|
|
if (is_string($content)) { |
30
|
|
|
$this->content[] = $content; |
31
|
|
|
} else if ($content instanceof LineGenerator) { |
32
|
|
|
$this->content[] = $content->generate(); |
33
|
|
View Code Duplication |
} else if (is_array($content)) { |
|
|
|
|
34
|
|
|
foreach ($content as $part) { |
35
|
|
|
$this->add($part); |
36
|
|
|
} |
37
|
|
|
} else if ($content instanceof AbstractContentGenerator) { |
38
|
|
|
$content->setIndention($this->getIndention()); |
39
|
|
|
if ($content->hasContent()) { |
40
|
|
|
$this->content[] = $content->generate(); |
41
|
|
|
} |
42
|
|
|
} else { |
43
|
|
|
throw new InvalidArgumentException('content has to be string, an array or instance of AbstractContentGenerator'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $separator |
51
|
|
|
*/ |
52
|
|
|
public function setSeparator($separator) |
53
|
|
|
{ |
54
|
|
|
$this->separator = (string) $separator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function clear() |
58
|
|
|
{ |
59
|
|
|
$this->content = []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasContent() |
66
|
|
|
{ |
67
|
|
|
return (!empty($this->content)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws InvalidArgumentException|RuntimeException |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function generate() |
75
|
|
|
{ |
76
|
|
|
return (implode('', $this->content) !== '') ? $this->getIndention()->toString() . implode($this->separator, $this->content) : ''; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
public function count() |
83
|
|
|
{ |
84
|
|
|
return (count($this->content)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.