1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matks\PHPMakeUp\LineAlignment; |
4
|
|
|
|
5
|
|
|
use Matks\PHPMakeUp\LineAlignment\VariableAssignmentLine as Line; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Variable Assignment Line Block |
10
|
|
|
*/ |
11
|
|
|
class VariableAssignmentLineBlock |
12
|
|
|
{ |
13
|
|
|
const STATUS_NEW = 'new'; |
14
|
|
|
const STATUS_VALID = 'valid'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Line[] |
18
|
|
|
*/ |
19
|
|
|
private $lines = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $assignmentCharacter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $status; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $assignmentCharacter |
33
|
|
|
*/ |
34
|
|
|
public function __construct($assignmentCharacter) |
35
|
|
|
{ |
36
|
1 |
|
$this->status = static::STATUS_NEW; |
37
|
1 |
|
$this->assignmentCharacter = $assignmentCharacter; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param VariableAssignmentLine $line |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function addLine(Line $line) |
46
|
|
|
{ |
47
|
1 |
|
$this->lines[] = $line; |
48
|
|
|
|
49
|
1 |
|
if ($this->count() > 1) { |
50
|
1 |
|
$this->status = static::STATUS_VALID; |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return VariableAssignmentLine |
58
|
|
|
*/ |
59
|
1 |
|
public function getFirstLine() |
60
|
|
|
{ |
61
|
1 |
|
$lines = $this->lines; |
62
|
|
|
|
63
|
1 |
|
if (empty($lines)) { |
64
|
1 |
|
throw new RuntimeException('Cannot get first line, no lines in this block'); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return $lines[0]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return VariableAssignmentLine[] |
72
|
|
|
*/ |
73
|
|
|
public function getLines() |
74
|
|
|
{ |
75
|
1 |
|
return $this->lines; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getAssignmentCharacter() |
82
|
|
|
{ |
83
|
|
|
return $this->assignmentCharacter; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function count() |
90
|
|
|
{ |
91
|
1 |
|
return count($this->lines); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
public function getAlignment() |
98
|
|
|
{ |
99
|
1 |
|
$highestLength = 0; |
100
|
1 |
|
foreach ($this->lines as $line) { |
101
|
1 |
|
$partBefore = $line->getPartBefore(); |
102
|
|
|
|
103
|
1 |
|
if (strlen($partBefore) > $highestLength) { |
104
|
1 |
|
$highestLength = strlen($partBefore); |
105
|
1 |
|
} |
106
|
1 |
|
} |
107
|
|
|
|
108
|
1 |
|
return $highestLength; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function isValid() |
115
|
|
|
{ |
116
|
1 |
|
return (static::STATUS_VALID === $this->status); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|