mirror of
https://github.com/Intervention/image.git
synced 2025-08-12 00:43:59 +02:00
Refactor line length calculation
This commit is contained in:
@@ -86,7 +86,7 @@ class Line implements IteratorAggregate, Countable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Count segments of line
|
* Count segments (individual words including punctuation marks) of line
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
@@ -95,6 +95,16 @@ class Line implements IteratorAggregate, Countable
|
|||||||
return count($this->segments);
|
return count($this->segments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count characters of line
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function length(): int
|
||||||
|
{
|
||||||
|
return mb_strlen((string) $this);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cast line to string
|
* Cast line to string
|
||||||
*
|
*
|
||||||
|
@@ -62,10 +62,10 @@ class TextBlock extends Collection
|
|||||||
{
|
{
|
||||||
$lines = $this->lines();
|
$lines = $this->lines();
|
||||||
usort($lines, function (Line $a, Line $b) {
|
usort($lines, function (Line $a, Line $b) {
|
||||||
if (mb_strlen((string) $a) === mb_strlen((string) $b)) {
|
if ($a->length() === $b->length()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return mb_strlen((string) $a) > mb_strlen((string) $b) ? -1 : 1;
|
return $a->length() > $b->length() ? -1 : 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
return $lines[0];
|
return $lines[0];
|
||||||
|
@@ -45,6 +45,21 @@ final class LineTest extends BaseTestCase
|
|||||||
$this->assertEquals(2, $line->count());
|
$this->assertEquals(2, $line->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLength(): void
|
||||||
|
{
|
||||||
|
$line = new Line();
|
||||||
|
$this->assertEquals(0, $line->length());
|
||||||
|
|
||||||
|
$line = new Line("foo");
|
||||||
|
$this->assertEquals(3, $line->length());
|
||||||
|
|
||||||
|
$line = new Line("foo bar.");
|
||||||
|
$this->assertEquals(8, $line->length());
|
||||||
|
|
||||||
|
$line = new Line("🫷🙂🫸");
|
||||||
|
$this->assertEquals(3, $line->length());
|
||||||
|
}
|
||||||
|
|
||||||
public function testAdd(): void
|
public function testAdd(): void
|
||||||
{
|
{
|
||||||
$line = new Line();
|
$line = new Line();
|
||||||
|
Reference in New Issue
Block a user