|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2026 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\DTO; |
| 10 | + |
| 11 | +use App\Enum\AspectRatioType; |
| 12 | +use App\Enum\ColumnSortingAlbumType; |
| 13 | +use App\Enum\ColumnSortingPhotoType; |
| 14 | +use App\Enum\LicenseType; |
| 15 | +use App\Enum\OrderSortingType; |
| 16 | +use App\Enum\PhotoLayoutType; |
| 17 | +use App\Enum\TimelineAlbumGranularity; |
| 18 | +use App\Enum\TimelinePhotoGranularity; |
| 19 | + |
| 20 | +/** |
| 21 | + * Typed payload for a bulk partial-update of album metadata and visibility. |
| 22 | + * |
| 23 | + * Only fields that were actually present in the incoming request are considered |
| 24 | + * "set". Use {@see has()} to distinguish "not sent" from "sent as null" |
| 25 | + * (which clears the field). |
| 26 | + * |
| 27 | + * Boolean fields (is_nsfw, is_public, …) are required to be non-null when |
| 28 | + * present, so their types are non-nullable; absent fields remain null here |
| 29 | + * and are guarded by {@see has()}. |
| 30 | + */ |
| 31 | +class BulkAlbumPatchData |
| 32 | +{ |
| 33 | + /** @var string[] Names of fields that were present in the request. */ |
| 34 | + private array $present_fields; |
| 35 | + |
| 36 | + /** @var string[] */ |
| 37 | + public array $album_ids; |
| 38 | + |
| 39 | + public ?string $description; |
| 40 | + public ?string $copyright; |
| 41 | + public ?LicenseType $license; |
| 42 | + public ?PhotoLayoutType $photo_layout; |
| 43 | + public ?ColumnSortingPhotoType $photo_sorting_col; |
| 44 | + public ?OrderSortingType $photo_sorting_order; |
| 45 | + public ?ColumnSortingAlbumType $album_sorting_col; |
| 46 | + public ?OrderSortingType $album_sorting_order; |
| 47 | + public ?AspectRatioType $album_thumb_aspect_ratio; |
| 48 | + public ?TimelineAlbumGranularity $album_timeline; |
| 49 | + public ?TimelinePhotoGranularity $photo_timeline; |
| 50 | + // Boolean fields: null here means "not present in the request" |
| 51 | + public ?bool $is_nsfw; |
| 52 | + public ?bool $is_public; |
| 53 | + public ?bool $is_link_required; |
| 54 | + public ?bool $grants_full_photo_access; |
| 55 | + public ?bool $grants_download; |
| 56 | + public ?bool $grants_upload; |
| 57 | + |
| 58 | + /** |
| 59 | + * @param string[] $album_ids |
| 60 | + * @param string[] $present_fields names of optional fields that were included in the request |
| 61 | + * @param ?string $description |
| 62 | + * @param ?string $copyright |
| 63 | + * @param ?LicenseType $license |
| 64 | + * @param ?PhotoLayoutType $photo_layout |
| 65 | + * @param ?ColumnSortingPhotoType $photo_sorting_col |
| 66 | + * @param ?OrderSortingType $photo_sorting_order |
| 67 | + * @param ?ColumnSortingAlbumType $album_sorting_col |
| 68 | + * @param ?OrderSortingType $album_sorting_order |
| 69 | + * @param ?AspectRatioType $album_thumb_aspect_ratio |
| 70 | + * @param ?TimelineAlbumGranularity $album_timeline |
| 71 | + * @param ?TimelinePhotoGranularity $photo_timeline |
| 72 | + * @param ?bool $is_nsfw |
| 73 | + * @param ?bool $is_public |
| 74 | + * @param ?bool $is_link_required |
| 75 | + * @param ?bool $grants_full_photo_access |
| 76 | + * @param ?bool $grants_download |
| 77 | + * @param ?bool $grants_upload |
| 78 | + */ |
| 79 | + public function __construct( |
| 80 | + array $album_ids, |
| 81 | + array $present_fields, |
| 82 | + ?string $description, |
| 83 | + ?string $copyright, |
| 84 | + ?LicenseType $license, |
| 85 | + ?PhotoLayoutType $photo_layout, |
| 86 | + ?ColumnSortingPhotoType $photo_sorting_col, |
| 87 | + ?OrderSortingType $photo_sorting_order, |
| 88 | + ?ColumnSortingAlbumType $album_sorting_col, |
| 89 | + ?OrderSortingType $album_sorting_order, |
| 90 | + ?AspectRatioType $album_thumb_aspect_ratio, |
| 91 | + ?TimelineAlbumGranularity $album_timeline, |
| 92 | + ?TimelinePhotoGranularity $photo_timeline, |
| 93 | + ?bool $is_nsfw, |
| 94 | + ?bool $is_public, |
| 95 | + ?bool $is_link_required, |
| 96 | + ?bool $grants_full_photo_access, |
| 97 | + ?bool $grants_download, |
| 98 | + ?bool $grants_upload, |
| 99 | + ) { |
| 100 | + $this->album_ids = $album_ids; |
| 101 | + $this->present_fields = $present_fields; |
| 102 | + $this->description = $description; |
| 103 | + $this->copyright = $copyright; |
| 104 | + $this->license = $license; |
| 105 | + $this->photo_layout = $photo_layout; |
| 106 | + $this->photo_sorting_col = $photo_sorting_col; |
| 107 | + $this->photo_sorting_order = $photo_sorting_order; |
| 108 | + $this->album_sorting_col = $album_sorting_col; |
| 109 | + $this->album_sorting_order = $album_sorting_order; |
| 110 | + $this->album_thumb_aspect_ratio = $album_thumb_aspect_ratio; |
| 111 | + $this->album_timeline = $album_timeline; |
| 112 | + $this->photo_timeline = $photo_timeline; |
| 113 | + $this->is_nsfw = $is_nsfw; |
| 114 | + $this->is_public = $is_public; |
| 115 | + $this->is_link_required = $is_link_required; |
| 116 | + $this->grants_full_photo_access = $grants_full_photo_access; |
| 117 | + $this->grants_download = $grants_download; |
| 118 | + $this->grants_upload = $grants_upload; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Build a DTO from a validated request values array. |
| 123 | + * |
| 124 | + * Handles all enum coercion and boolean normalization so that callers |
| 125 | + * (FormRequests, tests, …) never have to repeat that logic. |
| 126 | + * |
| 127 | + * @param array<string,mixed> $values the output of {@see \Illuminate\Foundation\Http\FormRequest::validated()} |
| 128 | + * @param string[] $present_fields names of optional fields that were present in the request |
| 129 | + */ |
| 130 | + public static function fromValidated(array $values, array $present_fields): self |
| 131 | + { |
| 132 | + return new self( |
| 133 | + album_ids: $values['album_ids'], |
| 134 | + present_fields: $present_fields, |
| 135 | + description: $values['description'] ?? null, |
| 136 | + copyright: $values['copyright'] ?? null, |
| 137 | + license: array_key_exists('license', $values) ? LicenseType::tryFrom($values['license']) : null, |
| 138 | + photo_layout: array_key_exists('photo_layout', $values) ? PhotoLayoutType::tryFrom($values['photo_layout']) : null, |
| 139 | + photo_sorting_col: array_key_exists('photo_sorting_col', $values) ? ColumnSortingPhotoType::tryFrom($values['photo_sorting_col']) : null, |
| 140 | + photo_sorting_order: array_key_exists('photo_sorting_order', $values) ? OrderSortingType::tryFrom($values['photo_sorting_order']) : null, |
| 141 | + album_sorting_col: array_key_exists('album_sorting_col', $values) ? ColumnSortingAlbumType::tryFrom($values['album_sorting_col']) : null, |
| 142 | + album_sorting_order: array_key_exists('album_sorting_order', $values) ? OrderSortingType::tryFrom($values['album_sorting_order']) : null, |
| 143 | + album_thumb_aspect_ratio: array_key_exists('album_thumb_aspect_ratio', $values) ? AspectRatioType::tryFrom($values['album_thumb_aspect_ratio']) : null, |
| 144 | + album_timeline: array_key_exists('album_timeline', $values) ? TimelineAlbumGranularity::tryFrom($values['album_timeline']) : null, |
| 145 | + photo_timeline: array_key_exists('photo_timeline', $values) ? TimelinePhotoGranularity::tryFrom($values['photo_timeline']) : null, |
| 146 | + is_nsfw: array_key_exists('is_nsfw', $values) ? filter_var($values['is_nsfw'], FILTER_VALIDATE_BOOLEAN) : null, |
| 147 | + is_public: array_key_exists('is_public', $values) ? filter_var($values['is_public'], FILTER_VALIDATE_BOOLEAN) : null, |
| 148 | + is_link_required: array_key_exists('is_link_required', $values) ? filter_var($values['is_link_required'], FILTER_VALIDATE_BOOLEAN) : null, |
| 149 | + grants_full_photo_access: array_key_exists('grants_full_photo_access', $values) ? filter_var($values['grants_full_photo_access'], FILTER_VALIDATE_BOOLEAN) : null, |
| 150 | + grants_download: array_key_exists('grants_download', $values) ? filter_var($values['grants_download'], FILTER_VALIDATE_BOOLEAN) : null, |
| 151 | + grants_upload: array_key_exists('grants_upload', $values) ? filter_var($values['grants_upload'], FILTER_VALIDATE_BOOLEAN) : null, |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Returns true if the named field was present in the original request. |
| 157 | + * |
| 158 | + * A field can be present-but-null (meaning "clear this value") or |
| 159 | + * present-with-a-value. A field that was absent should not be updated. |
| 160 | + */ |
| 161 | + public function has(string $field): bool |
| 162 | + { |
| 163 | + return in_array($field, $this->present_fields, true); |
| 164 | + } |
| 165 | +} |
0 commit comments