Skip to content

Commit 4bb0f42

Browse files
committed
Add link builder
1 parent 362e1ea commit 4bb0f42

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# php-autolink
1+
# PHP Autolink Library
2+
3+
A library to auto convert URLs to links in text.
4+
5+
## Installation via

src/Autolink.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/AutolinkTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,21 @@ public function testRenderEmail()
276276

277277
$this->assertStringSafeEquals($html, $this->instance->renderEmail($text));
278278
}
279+
280+
/**
281+
* testSetLinkBuilder
282+
*
283+
* @return void
284+
*/
285+
public function testGetAndSetLinkBuilder()
286+
{
287+
$this->instance->setLinkBuilder(function($url, $attribs)
288+
{
289+
return $url . json_encode($attribs);
290+
});
291+
292+
$this->assertEquals('http://google.com{"foo":"bar","href":"http:\/\/google.com"}', $this->instance->convert('http://google.com', array('foo' => 'bar')));
293+
294+
$this->assertInstanceOf('Closure', $this->instance->getLinkBuilder());
295+
}
279296
}

0 commit comments

Comments
 (0)