From b5848c9b870fec5b1ad22d0bf4b28d72dba3fa1c Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Mon, 20 Aug 2007 22:38:37 +0000 Subject: [PATCH] Added HTTPResponse::getLinks() for unit testing git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@40589 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/HTTPResponse.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/core/control/HTTPResponse.php b/core/control/HTTPResponse.php index 7aeb942db..e401d5d20 100644 --- a/core/control/HTTPResponse.php +++ b/core/control/HTTPResponse.php @@ -99,5 +99,30 @@ class HTTPResponse extends Object { function isFinished() { return $this->statusCode == 302 || $this->statusCode == 301; } + + /** + * Return all the links in the body as an array. + * @returns An array of maps. Each map will contain 'id', 'class', and 'href', representing the HTML attributes of the link. + */ + function getLinks() { + $attributes = array('id', 'href', 'class'); + $links = array(); + $results = array(); + + preg_match_all('/]+>/i', $this->body, $links); + // $links[0] contains the actual matches + foreach($links[0] as $link) { + $processedLink = array(); + foreach($attributes as $attribute) { + $matches = array(); + if(preg_match('/' . $attribute . '\s*=\s*"([^"]+)"/i', $link, $matches)) { + $processedLink[$attribute] = $matches[1]; + } + } + $results[] = $processedLink; + } + + return $results; + } } \ No newline at end of file