Collection
5 months ago
Comment
5 months ago
Date
5 months ago
Media
5 months ago
Post
5 months ago
Term
5 months ago
User
5 months ago
Wrap
5 months ago
Actions.php
5 months ago
Aggregate.php
5 months ago
Append.php
5 months ago
ArrayToCollection.php
5 months ago
Avatar.php
5 months ago
AvatarUrl.php
5 months ago
BeforeAfter.php
5 months ago
BooleanLabel.php
5 months ago
CharacterLimit.php
5 months ago
CodeBlock.php
5 months ago
Color.php
5 months ago
ColumnFilter.php
5 months ago
Composite.php
5 months ago
ConditionalValue.php
5 months ago
ContentTypeLink.php
5 months ago
Count.php
5 months ago
CountLabelFormatter.php
5 months ago
DottedPassword.php
5 months ago
EmptyValue.php
5 months ago
ExplodeToCollection.php
5 months ago
ExtendedValueLink.php
5 months ago
FallBack.php
5 months ago
FallBackFormatter.php
5 months ago
FileLink.php
5 months ago
FileSizeReadable.php
5 months ago
ForeignId.php
5 months ago
FormattedJson.php
5 months ago
GroupedIdsToCollection.php
5 months ago
HasValue.php
5 months ago
HumanReadableTime.php
5 months ago
HumanTimeDifference.php
5 months ago
Id.php
5 months ago
IdsToCollection.php
5 months ago
Image.php
5 months ago
ImageSize.php
5 months ago
ImageToCollection.php
5 months ago
ImageUrl.php
5 months ago
ImageUrlsFromContent.php
5 months ago
Implode.php
5 months ago
ImplodeRecursive.php
5 months ago
Kses.php
5 months ago
Latest.php
5 months ago
Linkable.php
5 months ago
Links.php
5 months ago
MapOptionLabel.php
5 months ago
MapToId.php
5 months ago
MapToLabel.php
5 months ago
MenuLink.php
5 months ago
Message.php
5 months ago
Meta.php
5 months ago
MetaCollection.php
5 months ago
NoIcon.php
5 months ago
NullFormatter.php
5 months ago
NumberFormat.php
5 months ago
PathScope.php
5 months ago
PregReplace.php
5 months ago
Prepend.php
5 months ago
ReadingTime.php
5 months ago
RelationIdsCollection.php
5 months ago
SelectOptionMapper.php
5 months ago
SmallBlocks.php
5 months ago
StringSanitizer.php
5 months ago
StripTags.php
5 months ago
Suffix.php
5 months ago
TableRender.php
5 months ago
ToArray.php
5 months ago
TotalSum.php
5 months ago
Trim.php
5 months ago
UrlDecode.php
5 months ago
UsedByMenu.php
5 months ago
WordCount.php
5 months ago
WordLimit.php
5 months ago
Wrapper.php
5 months ago
YesIcon.php
5 months ago
YesNoIcon.php
5 months ago
Links.php
79 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 | class Links implements Formatter |
| 14 | { |
| 15 | |
| 16 | private ?string $filter_by; |
| 17 | |
| 18 | private ?array $internal_domains; |
| 19 | |
| 20 | public function __construct(?string $filter_by = null, array $internal_domains = null) |
| 21 | { |
| 22 | $this->filter_by = $filter_by; |
| 23 | $this->internal_domains = $internal_domains; |
| 24 | } |
| 25 | |
| 26 | public function format(Value $value): ValueCollection |
| 27 | { |
| 28 | $urls = Helper\Html::create()->get_links( |
| 29 | (string)$value |
| 30 | ); |
| 31 | |
| 32 | if ( ! $urls) { |
| 33 | throw ValueNotFoundException::from_id($value->get_id()); |
| 34 | } |
| 35 | |
| 36 | switch ($this->filter_by) { |
| 37 | case 'internal': |
| 38 | $urls = $this->filter_by_internal($urls); |
| 39 | break; |
| 40 | case 'external': |
| 41 | $urls = $this->filter_by_external($urls); |
| 42 | } |
| 43 | |
| 44 | $values = []; |
| 45 | |
| 46 | foreach ($urls as $url) { |
| 47 | $values[] = new Value($url); |
| 48 | } |
| 49 | |
| 50 | return new ValueCollection($value->get_id(), $values); |
| 51 | } |
| 52 | |
| 53 | private function filter_by_internal(array $urls): array |
| 54 | { |
| 55 | return array_filter($urls, [$this, 'is_internal_url']); |
| 56 | } |
| 57 | |
| 58 | private function filter_by_external(array $urls): array |
| 59 | { |
| 60 | return array_filter($urls, [$this, 'is_external_url']); |
| 61 | } |
| 62 | |
| 63 | private function is_external_url(string $url): bool |
| 64 | { |
| 65 | return ! $this->is_internal_url($url); |
| 66 | } |
| 67 | |
| 68 | private function is_internal_url(string $url): bool |
| 69 | { |
| 70 | foreach ($this->internal_domains as $domain) { |
| 71 | if (false !== strpos($url, $domain)) { |
| 72 | return true; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | } |