Skip to content

Commit 0929b39

Browse files
Harrison IfeanyichukwuHarrison Ifeanyichukwu
Harrison Ifeanyichukwu
authored and
Harrison Ifeanyichukwu
committed
feat: added timestamps
1 parent 8d829a3 commit 0929b39

File tree

7 files changed

+103
-67
lines changed

7 files changed

+103
-67
lines changed

Diff for: src/FeedItems/ATOMFeedItem.php

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
4444
//date construct
4545
'lastUpdated' => 'atom:updated || atom:source/atom:updated || atom:published || atom:source/atom:published',
4646

47+
//date construct
48+
'createdAtTimestamp' => 'atom:published || atom:source/atom:published || atom:updated || atom:source/atom:updated',
49+
50+
//date construct
51+
'lastUpdatedTimestamp' => 'atom:updated || atom:source/atom:updated || atom:published || atom:source/atom:published',
52+
4753
'author' => 'atom:author/atom:name || atom:source/atom:author/atom:name || ' .
4854
'parent::atom:feed/atom:author/atom:name',
4955

Diff for: src/FeedItems/BaseFeedItem.php

+38-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
declare(strict_types = 1);
2+
3+
declare(strict_types=1);
34

45
namespace Forensic\FeedParser\FeedItems;
56

@@ -17,27 +18,27 @@ class BaseFeedItem
1718

1819
/**
1920
* feed item type
20-
*/
21+
*/
2122
protected $_type = null;
2223

2324
/**
2425
* feed item id
25-
*/
26+
*/
2627
protected $_id = '';
2728

2829
/**
2930
* feed item title
30-
*/
31+
*/
3132
protected $_title = '';
3233

3334
/**
3435
* url link to feed item's homepage
35-
*/
36+
*/
3637
protected $_link = '';
3738

3839
/**
3940
* feed item content
40-
*/
41+
*/
4142
protected $_content = '';
4243

4344
/**
@@ -47,7 +48,7 @@ class BaseFeedItem
4748

4849
/**
4950
* image associated with the feed item
50-
*/
51+
*/
5152
protected $_image = [
5253
'src' => '', //image src link
5354
'link' => '', //url that this image links to, likely the feed item's homepage
@@ -56,7 +57,7 @@ class BaseFeedItem
5657

5758
/**
5859
* media type associated with this item
59-
*/
60+
*/
6061
protected $_enclosure = [
6162

6263
'type' => '', //enclose media type
@@ -73,22 +74,32 @@ class BaseFeedItem
7374

7475
/**
7576
* time string describing when this feed item was last updated
76-
*/
77+
*/
7778
protected $_lastUpdated = '';
7879

80+
/**
81+
* timestamp describing when this feed item was created
82+
*/
83+
protected $_createdAtTimestamp = '';
84+
85+
/**
86+
* timestamp describing when this feed item was last updated
87+
*/
88+
protected $_lastUpdatedTimestamp = '';
89+
7990
/**
8091
* what category does this feed item belong to
81-
*/
92+
*/
8293
protected $_category = '';
8394

8495
/**
8596
* item's source
86-
*/
97+
*/
8798
protected $_source = '';
8899

89100
/**
90101
* who is the author of this item?
91-
*/
102+
*/
92103
protected $_author = '';
93104

94105
/**
@@ -97,10 +108,14 @@ class BaseFeedItem
97108
*@param DOMElement $item - the feed item node
98109
*@param XPath $xpath - the xpath instance for the feed
99110
*@param array $property_selectors - array of property selector maps
100-
*/
101-
public function __construct(FeedItemTypes $feed_item_type, DOMElement $item, XPath $xpath,
102-
array $property_selectors, array $parser_options)
103-
{
111+
*/
112+
public function __construct(
113+
FeedItemTypes $feed_item_type,
114+
DOMElement $item,
115+
XPath $xpath,
116+
array $property_selectors,
117+
array $parser_options
118+
) {
104119
$this->_type = $feed_item_type;
105120

106121
$xpath->setContextNode($item);
@@ -113,12 +128,11 @@ public function __construct(FeedItemTypes $feed_item_type, DOMElement $item, XPa
113128
*
114129
*@param string $property - the property to retrieve
115130
*@return string|null
116-
*/
131+
*/
117132
public function __get(string $property)
118133
{
119134
$this_property = '_' . $property;
120-
if (property_exists($this, $this_property))
121-
{
135+
if (property_exists($this, $this_property)) {
122136
$value = $this->{$this_property};
123137
if (is_array($value))
124138
return new ParameterBag($value);
@@ -132,23 +146,20 @@ public function __get(string $property)
132146
/**
133147
* converts the item to array
134148
*@return array
135-
*/
149+
*/
136150
public function toArray()
137151
{
138152
$reflector = new ReflectionClass(get_class($this));
139153
$props = $reflector->getProperties(ReflectionProperty::IS_PROTECTED);
140154

141155
$result = [];
142156

143-
foreach($props as $prop)
144-
{
157+
foreach ($props as $prop) {
145158
$this_property_name = $prop->getName();
146159
$property_name = substr($this_property_name, 1); //dont include the underscore
147160
if ($property_name === 'type') {
148161
$result[$property_name] = $this->{$this_property_name}->value();
149-
}
150-
151-
else {
162+
} else {
152163
$result[$property_name] = $this->{$this_property_name};
153164
}
154165
}
@@ -159,9 +170,9 @@ public function toArray()
159170
/**
160171
* convert the feed to json
161172
*@return string
162-
*/
173+
*/
163174
public function toJSON()
164175
{
165176
return json_encode($this->toArray());
166177
}
167-
}
178+
}

Diff for: src/FeedItems/RDFFeedItem.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
declare(strict_types = 1);
2+
3+
declare(strict_types=1);
34

45
namespace Forensic\FeedParser\FeedItems;
56

@@ -26,10 +27,14 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
2627
//'image' => { 'src' => '', 'link' => '', 'title' => '' }, // to be parsed specially
2728
'createdAt' => 'dc:date', // a date construct
2829
'lastUpdated' => 'dc:date', // a date construct
30+
31+
'createdAtTimestamp' => 'dc:date', // a date construct
32+
'lastUpdatedTimestamp' => 'dc:date', // a date construct
33+
2934
'author' => 'dc:creator || dc:contributor',
3035
'category' => 'dc:coverage || dc:subject/taxo:topic/@rdf:value || dc:subject || ' .
3136
'parent::rdf:RDF/def:channel/dc:coverage || ' .
32-
'parent::rdf:RDF/def:channel/dc:subject/taxo:topic/@rdf:value || ' .
37+
'parent::rdf:RDF/def:channel/dc:subject/taxo:topic/@rdf:value || ' .
3338
'parent::rdf:RDF/def:channel/dc:subject' // defaults to the parent category
3439
];
3540

@@ -41,4 +46,4 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
4146
$parser_options
4247
);
4348
}
44-
}
49+
}

Diff for: src/FeedItems/RSSFeedItem.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
declare(strict_types = 1);
2+
3+
declare(strict_types=1);
34

45
namespace Forensic\FeedParser\FeedItems;
56

@@ -26,6 +27,10 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
2627
//'image' => { 'src' => '', 'link' => '', 'title' => '' }, // to be parsed specially
2728
'createdAt' => 'pubDate',
2829
'lastUpdated' => 'lastBuildDate || pubDate',
30+
31+
'createdAtTimestamp' => 'pubDate',
32+
'lastUpdatedTimestamp' => 'lastBuildDate || pubDate',
33+
2934
'author' => 'author || dc:creator',
3035
'category' => 'category'
3136
];
@@ -38,4 +43,4 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
3843
$parser_options
3944
);
4045
}
41-
}
46+
}

0 commit comments

Comments
 (0)