Collection
2 days ago
Comment
2 days ago
Date
2 days ago
Media
2 days ago
Post
2 days ago
Term
2 days ago
User
2 days ago
Wrap
2 days ago
Actions.php
2 days ago
Aggregate.php
2 days ago
Append.php
2 days ago
ArrayToCollection.php
2 days ago
Avatar.php
2 days ago
AvatarUrl.php
2 days ago
BeforeAfter.php
2 days ago
BooleanLabel.php
2 days ago
CharacterLimit.php
2 days ago
CodeBlock.php
2 days ago
Color.php
2 days ago
ColumnFilter.php
2 days ago
Composite.php
2 days ago
ConditionalValue.php
2 days ago
ContentTypeLink.php
2 days ago
Count.php
2 days ago
CountLabelFormatter.php
2 days ago
DottedPassword.php
2 days ago
EmptyValue.php
2 days ago
ExplodeToCollection.php
2 days ago
ExtendedValueLink.php
2 days ago
FallBack.php
2 days ago
FallBackFormatter.php
2 days ago
FileLink.php
2 days ago
FileSizeReadable.php
2 days ago
ForeignId.php
2 days ago
FormattedJson.php
2 days ago
GroupedIdsToCollection.php
2 days ago
HasValue.php
2 days ago
HtmlEntityDecode.php
2 days ago
HumanReadableTime.php
2 days ago
HumanTimeDifference.php
2 days ago
Id.php
2 days ago
IdsToCollection.php
2 days ago
Image.php
2 days ago
ImageSize.php
2 days ago
ImageToCollection.php
2 days ago
ImageUrl.php
2 days ago
ImageUrlsFromContent.php
2 days ago
Implode.php
2 days ago
ImplodeRecursive.php
2 days ago
Kses.php
2 days ago
Latest.php
2 days ago
Linkable.php
2 days ago
Links.php
2 days ago
MapOptionLabel.php
2 days ago
MapToId.php
2 days ago
MapToLabel.php
2 days ago
MenuLink.php
2 days ago
Message.php
2 days ago
Meta.php
2 days ago
MetaCollection.php
2 days ago
MetaCount.php
2 days ago
MetaValueCollection.php
2 days ago
NoIcon.php
2 days ago
NullFormatter.php
2 days ago
NumberFormat.php
2 days ago
PathScope.php
2 days ago
PregReplace.php
2 days ago
Prepend.php
2 days ago
ReadingTime.php
2 days ago
RelationIdsCollection.php
2 days ago
SelectOptionMapper.php
2 days ago
SmallBlocks.php
2 days ago
StringSanitizer.php
2 days ago
StripTags.php
2 days ago
Suffix.php
2 days ago
TableRender.php
2 days ago
ToArray.php
2 days ago
ToggleValue.php
2 days ago
TotalSum.php
2 days ago
Trim.php
2 days ago
UrlDecode.php
2 days ago
UsedByMenu.php
2 days ago
WordCount.php
2 days ago
WordLimit.php
2 days ago
Wrapper.php
2 days ago
YesIcon.php
2 days ago
YesNoIcon.php
2 days ago
ImageToCollection.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter; |
| 6 | |
| 7 | use AC\Exception\ValueNotFoundException; |
| 8 | use AC\Formatter; |
| 9 | use AC\Helper; |
| 10 | use AC\Type\Value; |
| 11 | use AC\Type\ValueCollection; |
| 12 | |
| 13 | /** |
| 14 | * Takes various raw values that represent one or more image attachments and formats them into a ValueCollection of |
| 15 | * attachment IDs or URLs. It prioritizes parsing for attachment IDs first. If no valid IDs are found, it will then |
| 16 | * attempt to parse for image URLs. |
| 17 | * Supported input value types: |
| 18 | * - A single attachment ID (integer or string) or a single image URL (string) |
| 19 | * - A comma-separated string of attachment IDs or image URLs |
| 20 | * - An array of attachment IDs and/or image URLs |
| 21 | */ |
| 22 | class ImageToCollection implements Formatter |
| 23 | { |
| 24 | public function format(Value $value): ValueCollection |
| 25 | { |
| 26 | $source = $value->get_value(); |
| 27 | |
| 28 | if (! $source) { |
| 29 | throw ValueNotFoundException::from_id($value->get_id()); |
| 30 | } |
| 31 | |
| 32 | if (is_string($source)) { |
| 33 | $source = $this->parse_string($source); |
| 34 | } |
| 35 | |
| 36 | if (! is_array($source)) { |
| 37 | throw ValueNotFoundException::from_id($value->get_id()); |
| 38 | } |
| 39 | |
| 40 | $ids = $this->filter_ids($source); |
| 41 | |
| 42 | if ($ids) { |
| 43 | return ValueCollection::from_ids($value->get_id(), $ids); |
| 44 | } |
| 45 | |
| 46 | $urls = $this->filter_urls($source); |
| 47 | |
| 48 | if ($urls) { |
| 49 | return new ValueCollection($value->get_id(), array_map([$this, 'create_value'], $urls)); |
| 50 | } |
| 51 | |
| 52 | throw ValueNotFoundException::from_id($value->get_id()); |
| 53 | } |
| 54 | |
| 55 | private function filter_ids(array $ids): array |
| 56 | { |
| 57 | return array_map('absint', array_filter($ids, 'is_numeric')); |
| 58 | } |
| 59 | |
| 60 | private function filter_urls(array $urls): array |
| 61 | { |
| 62 | return array_filter($urls, [Helper\Strings::create(), 'is_image']); |
| 63 | } |
| 64 | |
| 65 | private function parse_string(string $value): array |
| 66 | { |
| 67 | if (str_contains($value, ',')) { |
| 68 | return explode(',', $value); |
| 69 | } |
| 70 | |
| 71 | return [$value]; |
| 72 | } |
| 73 | |
| 74 | private function create_value(string $value): Value |
| 75 | { |
| 76 | return new Value($value); |
| 77 | } |
| 78 | |
| 79 | } |
| 80 |