-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathTextBlock.php
More file actions
71 lines (60 loc) Β· 1.41 KB
/
TextBlock.php
File metadata and controls
71 lines (60 loc) Β· 1.41 KB
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
<?php
declare(strict_types=1);
namespace Intervention\Image\Typography;
use Intervention\Image\Collection;
class TextBlock extends Collection
{
/**
* Create new text block object.
*/
public function __construct(string $text)
{
parent::__construct(array_map(
fn(string $line): Line => new Line($line),
explode("\n", $text),
));
}
/**
* Return array of lines in text block.
*
* @return array<Line>
*/
public function lines(): array
{
return $this->items;
}
/**
* Set lines of the text block.
*
* @param array<Line> $lines
*/
public function setLines(array $lines): self
{
$this->items = $lines;
return $this;
}
/**
* Get line by given key.
*/
public function line(string|int|float $key): ?Line
{
if (!array_key_exists((string) $key, $this->lines())) {
return null;
}
return $this->lines()[(string) $key];
}
/**
* Return line with most characters of text block.
*/
public function longestLine(): Line
{
$lines = $this->lines();
usort($lines, function (Line $a, Line $b): int {
if ($a->length() === $b->length()) {
return 0;
}
return $a->length() > $b->length() ? -1 : 1;
});
return $lines[0];
}
}