diff --git a/src/PhpSpreadsheet/Comment.php b/src/PhpSpreadsheet/Comment.php index a4d389749..e2fcd83ed 100644 --- a/src/PhpSpreadsheet/Comment.php +++ b/src/PhpSpreadsheet/Comment.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheet; +use PhpOffice\PhpSpreadsheet\Helper\Size; use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Color; @@ -132,7 +133,10 @@ class Comment implements IComparable */ public function setWidth(string $width): self { - $this->width = $width; + $result = new Size($width); + if ($result->valid()) { + $this->width = $result->size() . $result->unit(); + } return $this; } @@ -150,7 +154,10 @@ class Comment implements IComparable */ public function setHeight(string $height): self { - $this->height = $height; + $result = new Size($height); + if ($result->valid()) { + $this->height = $result->size() . $result->unit(); + } return $this; } @@ -168,7 +175,10 @@ class Comment implements IComparable */ public function setMarginLeft(string $margin): self { - $this->marginLeft = $margin; + $result = new Size($margin); + if ($result->valid()) { + $this->marginLeft = $result->size() . $result->unit(); + } return $this; } @@ -186,7 +196,10 @@ class Comment implements IComparable */ public function setMarginTop(string $margin): self { - $this->marginTop = $margin; + $result = new Size($margin); + if ($result->valid()) { + $this->marginTop = $result->size() . $result->unit(); + } return $this; } diff --git a/src/PhpSpreadsheet/Helper/Size.php b/src/PhpSpreadsheet/Helper/Size.php new file mode 100644 index 000000000..e1a4d8693 --- /dev/null +++ b/src/PhpSpreadsheet/Helper/Size.php @@ -0,0 +1,38 @@ +\d*\.?\d+)(?Ppt|px|em)?$/i'; + + protected $valid; + + protected $size = ''; + + protected $unit = ''; + + public function __construct(string $size) + { + $this->valid = preg_match(self::REGEXP_SIZE_VALIDATION, $size, $matches); + if ($this->valid) { + $this->size = $matches['size']; + $this->unit = $matches['unit'] ?? 'pt'; + } + } + + public function valid(): bool + { + return $this->valid; + } + + public function size() + { + return $this->size; + } + + public function unit() + { + return $this->unit; + } +} diff --git a/tests/PhpSpreadsheetTests/CommentTest.php b/tests/PhpSpreadsheetTests/CommentTest.php index c41267e6e..f21efdf05 100644 --- a/tests/PhpSpreadsheetTests/CommentTest.php +++ b/tests/PhpSpreadsheetTests/CommentTest.php @@ -57,8 +57,8 @@ class CommentTest extends TestCase public function testSetHeight(): void { $comment = new Comment(); - $comment->setHeight('60pt'); - self::assertEquals('60pt', $comment->getHeight()); + $comment->setHeight('60px'); + self::assertEquals('60px', $comment->getHeight()); } public function testSetFillColor(): void