@@ -40,6 +40,13 @@ class Autolink
4040 'ftps '
4141 );
4242
43+ /**
44+ * Property linkBuilder.
45+ *
46+ * @var callable
47+ */
48+ protected $ linkBuilder ;
49+
4350 /**
4451 * Class init.
4552 *
@@ -75,7 +82,7 @@ function($matches) use ($self, $attribs)
7582
7683 if (!$ inElements )
7784 {
78- return $ self ->convert ($ matches [0 ]);
85+ return $ self ->convert ($ matches [0 ], $ attribs );
7986 }
8087
8188 return $ matches [0 ];
@@ -106,7 +113,7 @@ function($matches) use ($attribs)
106113 {
107114 $ attribs ['href ' ] = 'mailto: ' . htmlspecialchars ($ matches [0 ]);
108115
109- return ( string ) new HtmlElement ( ' a ' , htmlspecialchars ( $ matches [0 ]) , $ attribs );
116+ return $ this -> buildLink ( $ matches [0 ], $ attribs );
110117 }
111118
112119 return $ matches [0 ];
@@ -154,7 +161,25 @@ public function convert($url, $attribs = array())
154161 $ attribs ['title ' ] = htmlspecialchars ($ url );
155162 }
156163
157- return (string ) new HtmlElement ('a ' , htmlspecialchars ($ content ), $ attribs );
164+ return $ this ->buildLink ($ content , $ attribs );
165+ }
166+
167+ /**
168+ * buildLink
169+ *
170+ * @param string $url
171+ * @param array $attribs
172+ *
173+ * @return string
174+ */
175+ protected function buildLink ($ url = null , $ attribs = array ())
176+ {
177+ if (is_callable ($ this ->linkBuilder ))
178+ {
179+ return call_user_func ($ this ->linkBuilder , $ url , $ attribs );
180+ }
181+
182+ return (string ) new HtmlElement ('a ' , htmlspecialchars ($ url ), $ attribs );
158183 }
159184
160185 /**
@@ -330,4 +355,33 @@ public function setSchemes($schemes)
330355
331356 return $ this ;
332357 }
358+
359+ /**
360+ * Method to get property LinkBuilder
361+ *
362+ * @return callable
363+ */
364+ public function getLinkBuilder ()
365+ {
366+ return $ this ->linkBuilder ;
367+ }
368+
369+ /**
370+ * Method to set property linkBuilder
371+ *
372+ * @param callable $linkBuilder
373+ *
374+ * @return static Return self to support chaining.
375+ */
376+ public function setLinkBuilder ($ linkBuilder )
377+ {
378+ if (!is_callable ($ linkBuilder ))
379+ {
380+ throw new \InvalidArgumentException ('Please use a callable or Closure. ' );
381+ }
382+
383+ $ this ->linkBuilder = $ linkBuilder ;
384+
385+ return $ this ;
386+ }
333387}
0 commit comments