-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoxSize.php
63 lines (47 loc) · 1.76 KB
/
BoxSize.php
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
<?php
/**
* @see https://www.w3.org/TR/css-page-3/#page-size
*/
namespace RowBloom\CssLength;
class BoxSize
{
public readonly Length $width;
public readonly Length $height;
public static function new(string|Length $width, string|Length $height): static
{
return new static($width, $height);
}
public static function fromArray(array $size): static
{
if (! is_string($size['width']) && ! is_string($size[0])) {
throw new CssBoxException('Box size as an array must have a width value as string at key "width" or index 0.');
}
if (! is_string($size['height']) && ! is_string($size[1])) {
throw new CssBoxException('Box size as an array must have a height value as string at key "height" or index 1.');
}
return new static($size['width'] ?? $size[0], $size['height'] ?? $size[1]);
}
final public function __construct(string|Length $width, string|Length $height)
{
$this->width = $width instanceof Length ? $width : Length::fromDimension($width);
$this->height = $height instanceof Length ? $height : Length::fromDimension($height);
}
public function toLandscape(): static
{
$pxHeight = $this->height->toPxFloat();
$pxWidth = $this->width->toPxFloat();
if ($pxWidth > $pxHeight) {
return new static($this->width, $this->height);
}
return new static($this->height, $this->width);
}
public function toPortrait(): static
{
$pxHeight = $this->height->toPxFloat();
$pxWidth = $this->width->toPxFloat();
if ($pxHeight > $pxWidth) {
return new static($this->width, $this->height);
}
return new static($this->height, $this->width);
}
}