Skip to content

Commit f2b1172

Browse files
author
Harrison Ifeanyichukwu
committed
feat: inspect feed item content and extract the first image item
1 parent 9bd210c commit f2b1172

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/Traits/Parser.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
trait Parser
1010
{
11-
1211
private function filterPropertyValue(string $property_name, string $value)
1312
{
14-
if ($property_name === 'lastUpdated')
13+
if ($value === '')
14+
return '';
15+
16+
switch($property_name)
1517
{
16-
//process date
17-
$value = $value;
18+
case 'lastUpdated':
19+
$timestamp = strtotime($value);
20+
$value = date('jS F, Y, g:i A', $timestamp);
21+
break;
1822
}
1923
return $value;
2024
}
@@ -141,6 +145,35 @@ private function parseArrayProperty(XPath $xpath, array &$store,
141145
}
142146
}
143147

148+
/**
149+
* parses feed item image
150+
*/
151+
protected function parseImage()
152+
{
153+
$content = $this->_content;
154+
$matches = [];
155+
156+
//capture img src
157+
if (preg_match('/<img[^>]+src=("[^"]*"|\'[^\']*\')/im', $content, $matches))
158+
{
159+
$src = $matches[1];
160+
$this->_image['src'] = substr($src, 1, strlen($src) - 2);
161+
162+
$matches = [];
163+
164+
//capture img alt
165+
if (preg_match('/<img[^>]+alt=("[^"]*"|\'[^\']*\')/im', $content, $matches))
166+
{
167+
$src = $matches[1];
168+
$this->_image['title'] = substr($src, 1, strlen($src) - 2);
169+
}
170+
else
171+
{
172+
$this->_image['title'] = $this->_title; //@codeCoverageIgnore
173+
}
174+
}
175+
}
176+
144177
/**
145178
* entry pass point
146179
*

0 commit comments

Comments
 (0)