Size validator for comment dimensions and margins

This commit is contained in:
MarkBaker 2020-11-02 21:24:27 +01:00
parent 93fe84da71
commit 0502fd3e7f
3 changed files with 57 additions and 6 deletions

View File

@ -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;
}

View File

@ -0,0 +1,38 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Helper;
class Size
{
const REGEXP_SIZE_VALIDATION = '/^(?P<size>\d*\.?\d+)(?P<unit>pt|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;
}
}

View File

@ -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