|
17 | 17 | * Represents an icon that can be displayed on a map marker.
|
18 | 18 | *
|
19 | 19 | * @author Sylvain Blondeau <[email protected]>
|
| 20 | + * @author Hugo Alliaume <[email protected]> |
20 | 21 | */
|
21 |
| -class Icon |
| 22 | +abstract class Icon |
22 | 23 | {
|
23 |
| - public const TYPE_URL = 'url'; |
24 |
| - public const TYPE_INLINE_SVG = 'inline-svg'; |
25 |
| - public const TYPE_UX_ICON = 'ux-icon'; |
| 24 | + /** |
| 25 | + * @param non-empty-string $url |
| 26 | + */ |
| 27 | + public static function url(string $url): UrlIcon |
| 28 | + { |
| 29 | + return new UrlIcon($url); |
| 30 | + } |
26 | 31 |
|
27 |
| - private function __construct( |
28 |
| - public string $content, |
29 |
| - public string $type, |
30 |
| - public int $width = 24, |
31 |
| - public int $height = 24, |
32 |
| - ) { |
| 32 | + /** |
| 33 | + * @param non-empty-string $html |
| 34 | + */ |
| 35 | + public static function svg(string $html): SvgIcon |
| 36 | + { |
| 37 | + return new SvgIcon($html); |
33 | 38 | }
|
34 | 39 |
|
35 |
| - public function toArray(): array |
| 40 | + /** |
| 41 | + * @param non-empty-string $name |
| 42 | + */ |
| 43 | + public static function ux(string $name): UxIcon |
36 | 44 | {
|
37 |
| - return [ |
38 |
| - 'content' => $this->content, |
39 |
| - 'type' => $this->type, |
40 |
| - 'width' => $this->width, |
41 |
| - 'height' => $this->height, |
42 |
| - ]; |
| 45 | + return new UxIcon($name); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param positive-int $width |
| 50 | + * @param positive-int $height |
| 51 | + */ |
| 52 | + protected function __construct( |
| 53 | + protected IconType $type, |
| 54 | + protected int $width = 24, |
| 55 | + protected int $height = 24, |
| 56 | + ) { |
43 | 57 | }
|
44 | 58 |
|
45 |
| - public static function fromUrl(string $url, int $width = 24, int $height = 24): Url |
| 59 | + public function width(int $width): static |
46 | 60 | {
|
47 |
| - return new Url( |
48 |
| - content: $url, |
49 |
| - type: self::TYPE_URL, |
50 |
| - width: $width, |
51 |
| - height: $height |
52 |
| - ); |
| 61 | + if ($width <= 0) { |
| 62 | + throw new InvalidArgumentException('Width must be greater than 0.'); |
| 63 | + } |
| 64 | + |
| 65 | + $this->width = $width; |
| 66 | + |
| 67 | + return $this; |
53 | 68 | }
|
54 | 69 |
|
55 |
| - public static function fromInlineSVG(string $html, int $width = 24, int $height = 24): InlineSvg |
| 70 | + public function height(int $height): static |
56 | 71 | {
|
57 |
| - return new InlineSvg( |
58 |
| - content: $html, |
59 |
| - type: self::TYPE_INLINE_SVG, |
60 |
| - width: $width, |
61 |
| - height: $height |
62 |
| - ); |
| 72 | + if ($height <= 0) { |
| 73 | + throw new InvalidArgumentException('Height must be greater than 0.'); |
| 74 | + } |
| 75 | + |
| 76 | + $this->height = $height; |
| 77 | + |
| 78 | + return $this; |
63 | 79 | }
|
64 | 80 |
|
65 |
| - public static function fromUxIcon(string $name, int $width = 24, int $height = 24): UxIcon |
| 81 | + /** |
| 82 | + * @internal |
| 83 | + */ |
| 84 | + public function toArray(): array |
66 | 85 | {
|
67 |
| - return new UxIcon( |
68 |
| - content: $name, |
69 |
| - type: self::TYPE_UX_ICON, |
70 |
| - width: $width, |
71 |
| - height: $height |
72 |
| - ); |
| 86 | + return [ |
| 87 | + 'type' => $this->type->value, |
| 88 | + 'width' => $this->width, |
| 89 | + 'height' => $this->height, |
| 90 | + ]; |
73 | 91 | }
|
74 | 92 |
|
75 | 93 | /**
|
76 |
| - * @param array{ |
77 |
| - * content: string, |
78 |
| - * type: string, |
79 |
| - * width: int, |
80 |
| - * height: int, |
81 |
| - * } $data |
| 94 | + * @param array{ type: value-of<IconType>, width: positive-int, height: positive-int } |
| 95 | + * &(array{ url: non-empty-string } |
| 96 | + * |array{ html: non-empty-string } |
| 97 | + * |array{ name: non-empty-string }) $data |
82 | 98 | *
|
83 | 99 | * @internal
|
84 | 100 | */
|
85 | 101 | public static function fromArray(array $data): static
|
86 | 102 | {
|
87 | 103 | return match ($data['type']) {
|
88 |
| - 'url' => self::fromUrl($data['content'], (int) $data['width'], (int) $data['height']), |
89 |
| - 'inline-svg' => self::fromInlineSvg($data['content'], (int) $data['width'], (int) $data['height']), |
90 |
| - 'ux-icon' => self::fromUxIcon($data['content'], (int) $data['width'], (int) $data['height']), |
| 104 | + IconType::Url->value => UrlIcon::fromArray($data), |
| 105 | + IconType::Svg->value => SvgIcon::fromArray($data), |
| 106 | + IconType::UxIcon->value => UxIcon::fromArray($data), |
91 | 107 | default => throw new InvalidArgumentException(\sprintf('Invalid icon type %s.', $data['type'])),
|
92 | 108 | };
|
93 | 109 | }
|
|
0 commit comments