|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Courier\Notifications; |
| 6 | + |
| 7 | +use Courier\Core\Attributes\Optional; |
| 8 | +use Courier\Core\Attributes\Required; |
| 9 | +use Courier\Core\Concerns\SdkModel; |
| 10 | +use Courier\Core\Contracts\BaseModel; |
| 11 | +use Courier\Notifications\ElementWithChecksums\Locale; |
| 12 | + |
| 13 | +/** |
| 14 | + * An element with its content checksum and optional nested elements and locale checksums. |
| 15 | + * |
| 16 | + * @phpstan-import-type LocaleShape from \Courier\Notifications\ElementWithChecksums\Locale |
| 17 | + * |
| 18 | + * @phpstan-type ElementWithChecksumsShape = array{ |
| 19 | + * checksum: string, |
| 20 | + * type: string, |
| 21 | + * id?: string|null, |
| 22 | + * elements?: list<mixed>|null, |
| 23 | + * locales?: array<string,Locale|LocaleShape>|null, |
| 24 | + * } |
| 25 | + */ |
| 26 | +final class ElementWithChecksums implements BaseModel |
| 27 | +{ |
| 28 | + /** @use SdkModel<ElementWithChecksumsShape> */ |
| 29 | + use SdkModel; |
| 30 | + |
| 31 | + /** |
| 32 | + * MD5 hash of translatable content. |
| 33 | + */ |
| 34 | + #[Required] |
| 35 | + public string $checksum; |
| 36 | + |
| 37 | + /** |
| 38 | + * Element type (text, meta, action, etc.). |
| 39 | + */ |
| 40 | + #[Required] |
| 41 | + public string $type; |
| 42 | + |
| 43 | + #[Optional] |
| 44 | + public ?string $id; |
| 45 | + |
| 46 | + /** |
| 47 | + * Nested child elements (for group-type elements). |
| 48 | + * |
| 49 | + * @var list<mixed>|null $elements |
| 50 | + */ |
| 51 | + #[Optional(list: ElementWithChecksums::class)] |
| 52 | + public ?array $elements; |
| 53 | + |
| 54 | + /** |
| 55 | + * Locale-specific content with checksums. |
| 56 | + * |
| 57 | + * @var array<string,Locale>|null $locales |
| 58 | + */ |
| 59 | + #[Optional(map: Locale::class)] |
| 60 | + public ?array $locales; |
| 61 | + |
| 62 | + /** |
| 63 | + * `new ElementWithChecksums()` is missing required properties by the API. |
| 64 | + * |
| 65 | + * To enforce required parameters use |
| 66 | + * ``` |
| 67 | + * ElementWithChecksums::with(checksum: ..., type: ...) |
| 68 | + * ``` |
| 69 | + * |
| 70 | + * Otherwise ensure the following setters are called |
| 71 | + * |
| 72 | + * ``` |
| 73 | + * (new ElementWithChecksums)->withChecksum(...)->withType(...) |
| 74 | + * ``` |
| 75 | + */ |
| 76 | + public function __construct() |
| 77 | + { |
| 78 | + $this->initialize(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Construct an instance from the required parameters. |
| 83 | + * |
| 84 | + * You must use named parameters to construct any parameters with a default value. |
| 85 | + * |
| 86 | + * @param list<mixed>|null $elements |
| 87 | + * @param array<string,Locale|LocaleShape>|null $locales |
| 88 | + */ |
| 89 | + public static function with( |
| 90 | + string $checksum, |
| 91 | + string $type, |
| 92 | + ?string $id = null, |
| 93 | + ?array $elements = null, |
| 94 | + ?array $locales = null, |
| 95 | + ): self { |
| 96 | + $self = new self; |
| 97 | + |
| 98 | + $self['checksum'] = $checksum; |
| 99 | + $self['type'] = $type; |
| 100 | + |
| 101 | + null !== $id && $self['id'] = $id; |
| 102 | + null !== $elements && $self['elements'] = $elements; |
| 103 | + null !== $locales && $self['locales'] = $locales; |
| 104 | + |
| 105 | + return $self; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * MD5 hash of translatable content. |
| 110 | + */ |
| 111 | + public function withChecksum(string $checksum): self |
| 112 | + { |
| 113 | + $self = clone $this; |
| 114 | + $self['checksum'] = $checksum; |
| 115 | + |
| 116 | + return $self; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Element type (text, meta, action, etc.). |
| 121 | + */ |
| 122 | + public function withType(string $type): self |
| 123 | + { |
| 124 | + $self = clone $this; |
| 125 | + $self['type'] = $type; |
| 126 | + |
| 127 | + return $self; |
| 128 | + } |
| 129 | + |
| 130 | + public function withID(string $id): self |
| 131 | + { |
| 132 | + $self = clone $this; |
| 133 | + $self['id'] = $id; |
| 134 | + |
| 135 | + return $self; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Nested child elements (for group-type elements). |
| 140 | + * |
| 141 | + * @param list<mixed> $elements |
| 142 | + */ |
| 143 | + public function withElements(array $elements): self |
| 144 | + { |
| 145 | + $self = clone $this; |
| 146 | + $self['elements'] = $elements; |
| 147 | + |
| 148 | + return $self; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Locale-specific content with checksums. |
| 153 | + * |
| 154 | + * @param array<string,Locale|LocaleShape> $locales |
| 155 | + */ |
| 156 | + public function withLocales(array $locales): self |
| 157 | + { |
| 158 | + $self = clone $this; |
| 159 | + $self['locales'] = $locales; |
| 160 | + |
| 161 | + return $self; |
| 162 | + } |
| 163 | +} |
0 commit comments