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
PregReplace.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter; |
| 6 | |
| 7 | use AC\Formatter; |
| 8 | use AC\Type\Value; |
| 9 | |
| 10 | class PregReplace implements Formatter |
| 11 | { |
| 12 | |
| 13 | private array $patterns = []; |
| 14 | |
| 15 | private array $replacements = []; |
| 16 | |
| 17 | public function add_pattern(string $pattern, string $replacement): self |
| 18 | { |
| 19 | $this->patterns[] = $pattern; |
| 20 | $this->replacements[] = $replacement; |
| 21 | |
| 22 | return $this; |
| 23 | } |
| 24 | |
| 25 | public function replace_br(string $replacement = ', '): self |
| 26 | { |
| 27 | return $this->add_pattern('#<br\s*/?>#i', $replacement); |
| 28 | } |
| 29 | |
| 30 | public function replace_new_line(string $replacement = ' '): self |
| 31 | { |
| 32 | return $this->add_pattern('/(\r\n|\r|\n)/', $replacement); |
| 33 | } |
| 34 | |
| 35 | public function replace_tabs(string $replacement = ' '): self |
| 36 | { |
| 37 | return $this->add_pattern('/\t+/', $replacement); |
| 38 | } |
| 39 | |
| 40 | public function replace_multiple_spaces(string $replacement = ' '): self |
| 41 | { |
| 42 | return $this->add_pattern('/\s{2,}/', $replacement); |
| 43 | } |
| 44 | |
| 45 | public function format(Value $value): Value |
| 46 | { |
| 47 | if (empty($this->patterns)) { |
| 48 | return $value; |
| 49 | } |
| 50 | |
| 51 | $replaced_value = preg_replace($this->patterns, $this->replacements, (string)$value); |
| 52 | |
| 53 | return $value->with_value((string)$replaced_value); |
| 54 | } |
| 55 | |
| 56 | } |