ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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
 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 29 of file MakeClickable.php.

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 38 of file MakeClickable.php.

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

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

Member Function Documentation

◆ additionalAttributes()

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

Definition at line 124 of file MakeClickable.php.

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

124  : string
125  {
126  if ($this->open_in_new_tab) {
127  return ' target="_blank" rel="noopener"';
128  }
129 
130  return '';
131  }
+ 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 107 of file MakeClickable.php.

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

107  : ?array
108  {
109  $pattern = str_replace('@', '\@', $pattern);
110  return 1 === preg_match('@' . $pattern . '@', $haystack, $matches) ? $matches : null;
111  }
+ Here is the caller graph for this function:

◆ regexPos()

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

Definition at line 74 of file MakeClickable.php.

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

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

74  : int
75  {
76  $matches = $this->match($regexp, $string);
77  if (null !== $matches) {
78  return strpos($string, $matches[0]);
79  }
80 
81  return strlen($string);
82  }
match(string $pattern, string $haystack)
+ 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 113 of file MakeClickable.php.

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

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

113  : string
114  {
115  $maybeProtocol = !$protocol ? 'https://' : '';
116  return sprintf(
117  '<a%s href="%s">%s</a>',
118  $this->additionalAttributes(),
119  $maybeProtocol . $url,
120  $url
121  );
122  }
$url
+ 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 54 of file MakeClickable.php.

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

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

54  : string
55  {
56  $endOfLastMatch = 0;
57  $stringParts = [];
58 
59  while (null !== ($matches = $this->match(self::URL_PATTERN, substr($from, $endOfLastMatch)))) {
60  $startOfMatch = $endOfLastMatch + strpos(substr($from, $endOfLastMatch), $matches[0]);
61  $endOfMatch = $startOfMatch + strlen($matches[1] . $matches[2]);
62 
63  $stringParts[] = substr($from, $endOfLastMatch, $startOfMatch - $endOfLastMatch);
64  $stringParts[] = $matches[1] . $replace($startOfMatch, $endOfMatch, $matches[2], $matches[4]);
65 
66  $endOfLastMatch = $endOfMatch;
67  }
68 
69  $stringParts[] = substr($from, $endOfLastMatch);
70 
71  return implode('', $stringParts);
72  }
match(string $pattern, string $haystack)
+ 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 88 of file MakeClickable.php.

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

88  : void
89  {
90  if (!is_string($maybeHTML)) {
91  throw new ConstraintViolationException('not a string', 'not_a_string');
92  }
93  }
+ Here is the caller graph for this function:

◆ shouldReplace()

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

Definition at line 95 of file MakeClickable.php.

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

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

95  : bool
96  {
97  $isNotInAnchor = $this->regexPos('<a.*</a>', substr($maybeHTML, $endOfMatch)) <= $this->regexPos('</a>', substr($maybeHTML, $endOfMatch));
98  $isNotATagAttribute = null === $this->match('^[^>]*[[:space:]][[:alpha:]]+<', strrev(substr($maybeHTML, 0, $startOfMatch)));
99 
100  return $isNotInAnchor && $isNotATagAttribute;
101  }
match(string $pattern, string $haystack)
regexPos(string $regexp, string $string)
+ 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 43 of file MakeClickable.php.

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

43  : string
44  {
45  $this->requireString($from);
46 
47  return $this->replaceMatches($from, fn (int $startOfMatch, int $endOfMatch, string $url, string $protocol): string => (
48  $this->shouldReplace($from, $startOfMatch, $endOfMatch) ?
49  $this->replace($url, $protocol) :
50  $url
51  ));
52  }
replace(string $url, string $protocol)
replaceMatches(string $from, callable $replace)
shouldReplace(string $maybeHTML, int $startOfMatch, int $endOfMatch)
$url
+ 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 36 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 34 of file MakeClickable.php.


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