ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ILIAS\Refinery\String\MakeClickable Class Reference
+ Inheritance diagram for ILIAS\Refinery\String\MakeClickable:
+ Collaboration diagram for ILIAS\Refinery\String\MakeClickable:

Public Member Functions

 __construct ($open_in_new_tab=true)
 
 transform ($from)
 Perform the transformation. More...
 
- Public Member Functions inherited from ILIAS\Refinery\Transformation
 transform ($from)
 Perform the transformation. More...
 
 applyTo (Result $result)
 Perform the transformation and reify possible failures. More...
 
 __invoke ($from)
 Transformations should be callable. More...
 

Protected Member Functions

 additionalAttributes ()
 

Private Member Functions

 replaceMatches (string $from, callable $replace)
 
 regexPos (string $regexp, string $string)
 
 requireString ($maybeHTML)
 
 shouldReplace (string $maybeHTML, int $startOfMatch, int $endOfMatch)
 
 match (string $pattern, string $haystack)
 
 replace (string $url, string $protocol)
 

Private Attributes

const URL_PATTERN = '(^|[^[:alnum:]])(((https?:\/\/)|(www.))[^[:cntrl:][:space:]<>\'"]+)([^[:alnum:]]|$)'
 
bool $open_in_new_tab
 

Detailed Description

Definition at line 28 of file MakeClickable.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Refinery\String\MakeClickable::__construct (   $open_in_new_tab = true)

Definition at line 37 of file MakeClickable.php.

38 {
39 $this->open_in_new_tab = $open_in_new_tab;
40 }

References ILIAS\Refinery\String\MakeClickable\$open_in_new_tab.

Member Function Documentation

◆ additionalAttributes()

ILIAS\Refinery\String\MakeClickable::additionalAttributes ( )
protected

Definition at line 123 of file MakeClickable.php.

123 : string
124 {
125 if ($this->open_in_new_tab) {
126 return ' target="_blank" rel="noopener"';
127 }
128
129 return '';
130 }

Referenced by ILIAS\Refinery\String\MakeClickable\replace().

+ Here is the caller graph for this function:

◆ match()

ILIAS\Refinery\String\MakeClickable::match ( string  $pattern,
string  $haystack 
)
private
Parameters
string$patternPattern without delimiters.
Returns
null|string[]

Definition at line 106 of file MakeClickable.php.

106 : ?array
107 {
108 $pattern = str_replace('@', '\@', $pattern);
109 return 1 === preg_match('@' . $pattern . '@', $haystack, $matches) ? $matches : null;
110 }

Referenced by ILIAS\Refinery\String\MakeClickable\regexPos(), ILIAS\Refinery\String\MakeClickable\replaceMatches(), and ILIAS\Refinery\String\MakeClickable\shouldReplace().

+ Here is the caller graph for this function:

◆ regexPos()

ILIAS\Refinery\String\MakeClickable::regexPos ( string  $regexp,
string  $string 
)
private

Definition at line 73 of file MakeClickable.php.

73 : int
74 {
75 $matches = $this->match($regexp, $string);
76 if (null !== $matches) {
77 return strpos($string, $matches[0]);
78 }
79
80 return strlen($string);
81 }
match(string $pattern, string $haystack)

References ILIAS\Refinery\String\MakeClickable\match().

Referenced by ILIAS\Refinery\String\MakeClickable\shouldReplace().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replace()

ILIAS\Refinery\String\MakeClickable::replace ( string  $url,
string  $protocol 
)
private

Definition at line 112 of file MakeClickable.php.

112 : string
113 {
114 $maybeProtocol = !$protocol ? 'https://' : '';
115 return sprintf(
116 '<a%s href="%s">%s</a>',
117 $this->additionalAttributes(),
118 $maybeProtocol . $url,
119 $url
120 );
121 }
$url
Definition: shib_logout.php:68

References $url, and ILIAS\Refinery\String\MakeClickable\additionalAttributes().

Referenced by ILIAS\Refinery\String\MakeClickable\transform().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replaceMatches()

ILIAS\Refinery\String\MakeClickable::replaceMatches ( string  $from,
callable  $replace 
)
private

Definition at line 53 of file MakeClickable.php.

53 : string
54 {
55 $endOfLastMatch = 0;
56 $stringParts = [];
57
58 while (null !== ($matches = $this->match(self::URL_PATTERN, substr($from, $endOfLastMatch)))) {
59 $startOfMatch = $endOfLastMatch + strpos(substr($from, $endOfLastMatch), $matches[0]);
60 $endOfMatch = $startOfMatch + strlen($matches[1] . $matches[2]);
61
62 $stringParts[] = substr($from, $endOfLastMatch, $startOfMatch - $endOfLastMatch);
63 $stringParts[] = $matches[1] . $replace($startOfMatch, $endOfMatch, $matches[2], $matches[4]);
64
65 $endOfLastMatch = $endOfMatch;
66 }
67
68 $stringParts[] = substr($from, $endOfLastMatch);
69
70 return implode('', $stringParts);
71 }

References ILIAS\Refinery\String\MakeClickable\match().

Referenced by ILIAS\Refinery\String\MakeClickable\transform().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ requireString()

ILIAS\Refinery\String\MakeClickable::requireString (   $maybeHTML)
private
Parameters
mixed$maybeHTML
Returns
void

Definition at line 87 of file MakeClickable.php.

87 : void
88 {
89 if (!is_string($maybeHTML)) {
90 throw new ConstraintViolationException('not a string', 'not_a_string');
91 }
92 }

Referenced by ILIAS\Refinery\String\MakeClickable\transform().

+ Here is the caller graph for this function:

◆ shouldReplace()

ILIAS\Refinery\String\MakeClickable::shouldReplace ( string  $maybeHTML,
int  $startOfMatch,
int  $endOfMatch 
)
private

Definition at line 94 of file MakeClickable.php.

94 : bool
95 {
96 $isNotInAnchor = $this->regexPos('<a.*</a>', substr($maybeHTML, $endOfMatch)) <= $this->regexPos('</a>', substr($maybeHTML, $endOfMatch));
97 $isNotATagAttribute = null === $this->match('^[^>]*[[:space:]][[:alpha:]]+<', strrev(substr($maybeHTML, 0, $startOfMatch)));
98
99 return $isNotInAnchor && $isNotATagAttribute;
100 }
regexPos(string $regexp, string $string)

References ILIAS\Refinery\String\MakeClickable\match(), and ILIAS\Refinery\String\MakeClickable\regexPos().

Referenced by ILIAS\Refinery\String\MakeClickable\transform().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ transform()

ILIAS\Refinery\String\MakeClickable::transform (   $from)

Perform the transformation.

Please use this for transformations. It's more performant than calling invoke.

Parameters
mixed$from
Returns
mixed
Exceptions
InvalidArgumentExceptionif the argument could not be transformed

Implements ILIAS\Refinery\Transformation.

Definition at line 42 of file MakeClickable.php.

42 : string
43 {
44 $this->requireString($from);
45
46 return $this->replaceMatches($from, fn(int $startOfMatch, int $endOfMatch, string $url, string $protocol): string => (
47 $this->shouldReplace($from, $startOfMatch, $endOfMatch) ?
48 $this->replace($url, $protocol) :
49 $url
50 ));
51 }
replaceMatches(string $from, callable $replace)
shouldReplace(string $maybeHTML, int $startOfMatch, int $endOfMatch)
replace(string $url, string $protocol)

References $url, ILIAS\Refinery\String\MakeClickable\replace(), ILIAS\Refinery\String\MakeClickable\replaceMatches(), ILIAS\Refinery\String\MakeClickable\requireString(), and ILIAS\Refinery\String\MakeClickable\shouldReplace().

+ Here is the call graph for this function:

Field Documentation

◆ $open_in_new_tab

bool ILIAS\Refinery\String\MakeClickable::$open_in_new_tab
private

Definition at line 35 of file MakeClickable.php.

Referenced by ILIAS\Refinery\String\MakeClickable\__construct().

◆ URL_PATTERN

const ILIAS\Refinery\String\MakeClickable::URL_PATTERN = '(^|[^[:alnum:]])(((https?:\/\/)|(www.))[^[:cntrl:][:space:]<>\'"]+)([^[:alnum:]]|$)'
private

Definition at line 33 of file MakeClickable.php.


The documentation for this class was generated from the following file: