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
FallBackFormatter.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter; |
| 6 | |
| 7 | use AC; |
| 8 | use AC\Exception\ValueNotFoundException; |
| 9 | use AC\Formatter; |
| 10 | use AC\Type\Value; |
| 11 | |
| 12 | class FallBackFormatter implements AC\Formatter |
| 13 | { |
| 14 | private Formatter $formatter; |
| 15 | |
| 16 | private ?Formatter $fallback_formatter; |
| 17 | |
| 18 | public function __construct(Formatter $formatter, ?Formatter $fallback_formatter = null) |
| 19 | { |
| 20 | $this->formatter = $formatter; |
| 21 | $this->fallback_formatter = $fallback_formatter; |
| 22 | } |
| 23 | |
| 24 | public function format(Value $value): Value |
| 25 | { |
| 26 | try { |
| 27 | $formatted_value = $this->formatter->format($value); |
| 28 | } catch (ValueNotFoundException $e) { |
| 29 | return $this->format_fallback($value); |
| 30 | } |
| 31 | |
| 32 | if ($formatted_value instanceof Value && $formatted_value->get_value()) { |
| 33 | return $formatted_value; |
| 34 | } |
| 35 | |
| 36 | return $this->format_fallback($value); |
| 37 | } |
| 38 | |
| 39 | private function format_fallback(Value $value): Value |
| 40 | { |
| 41 | if (! $this->fallback_formatter) { |
| 42 | return $value; |
| 43 | } |
| 44 | |
| 45 | $fallback_value = $this->fallback_formatter->format($value); |
| 46 | |
| 47 | return $fallback_value instanceof Value |
| 48 | ? $fallback_value |
| 49 | : $value; |
| 50 | } |
| 51 | |
| 52 | } |
| 53 |