Skip to content

Commit

Permalink
feat: added timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Jun 8, 2020
1 parent 8d829a3 commit 0929b39
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 67 deletions.
6 changes: 6 additions & 0 deletions src/FeedItems/ATOMFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
//date construct
'lastUpdated' => 'atom:updated || atom:source/atom:updated || atom:published || atom:source/atom:published',

//date construct
'createdAtTimestamp' => 'atom:published || atom:source/atom:published || atom:updated || atom:source/atom:updated',

//date construct
'lastUpdatedTimestamp' => 'atom:updated || atom:source/atom:updated || atom:published || atom:source/atom:published',

'author' => 'atom:author/atom:name || atom:source/atom:author/atom:name || ' .
'parent::atom:feed/atom:author/atom:name',

Expand Down
65 changes: 38 additions & 27 deletions src/FeedItems/BaseFeedItem.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);

namespace Forensic\FeedParser\FeedItems;

Expand All @@ -17,27 +18,27 @@ class BaseFeedItem

/**
* feed item type
*/
*/
protected $_type = null;

/**
* feed item id
*/
*/
protected $_id = '';

/**
* feed item title
*/
*/
protected $_title = '';

/**
* url link to feed item's homepage
*/
*/
protected $_link = '';

/**
* feed item content
*/
*/
protected $_content = '';

/**
Expand All @@ -47,7 +48,7 @@ class BaseFeedItem

/**
* image associated with the feed item
*/
*/
protected $_image = [
'src' => '', //image src link
'link' => '', //url that this image links to, likely the feed item's homepage
Expand All @@ -56,7 +57,7 @@ class BaseFeedItem

/**
* media type associated with this item
*/
*/
protected $_enclosure = [

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

/**
* time string describing when this feed item was last updated
*/
*/
protected $_lastUpdated = '';

/**
* timestamp describing when this feed item was created
*/
protected $_createdAtTimestamp = '';

/**
* timestamp describing when this feed item was last updated
*/
protected $_lastUpdatedTimestamp = '';

/**
* what category does this feed item belong to
*/
*/
protected $_category = '';

/**
* item's source
*/
*/
protected $_source = '';

/**
* who is the author of this item?
*/
*/
protected $_author = '';

/**
Expand All @@ -97,10 +108,14 @@ class BaseFeedItem
*@param DOMElement $item - the feed item node
*@param XPath $xpath - the xpath instance for the feed
*@param array $property_selectors - array of property selector maps
*/
public function __construct(FeedItemTypes $feed_item_type, DOMElement $item, XPath $xpath,
array $property_selectors, array $parser_options)
{
*/
public function __construct(
FeedItemTypes $feed_item_type,
DOMElement $item,
XPath $xpath,
array $property_selectors,
array $parser_options
) {
$this->_type = $feed_item_type;

$xpath->setContextNode($item);
Expand All @@ -113,12 +128,11 @@ public function __construct(FeedItemTypes $feed_item_type, DOMElement $item, XPa
*
*@param string $property - the property to retrieve
*@return string|null
*/
*/
public function __get(string $property)
{
$this_property = '_' . $property;
if (property_exists($this, $this_property))
{
if (property_exists($this, $this_property)) {
$value = $this->{$this_property};
if (is_array($value))
return new ParameterBag($value);
Expand All @@ -132,23 +146,20 @@ public function __get(string $property)
/**
* converts the item to array
*@return array
*/
*/
public function toArray()
{
$reflector = new ReflectionClass(get_class($this));
$props = $reflector->getProperties(ReflectionProperty::IS_PROTECTED);

$result = [];

foreach($props as $prop)
{
foreach ($props as $prop) {
$this_property_name = $prop->getName();
$property_name = substr($this_property_name, 1); //dont include the underscore
if ($property_name === 'type') {
$result[$property_name] = $this->{$this_property_name}->value();
}

else {
} else {
$result[$property_name] = $this->{$this_property_name};
}
}
Expand All @@ -159,9 +170,9 @@ public function toArray()
/**
* convert the feed to json
*@return string
*/
*/
public function toJSON()
{
return json_encode($this->toArray());
}
}
}
11 changes: 8 additions & 3 deletions src/FeedItems/RDFFeedItem.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);

namespace Forensic\FeedParser\FeedItems;

Expand All @@ -26,10 +27,14 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
//'image' => { 'src' => '', 'link' => '', 'title' => '' }, // to be parsed specially
'createdAt' => 'dc:date', // a date construct
'lastUpdated' => 'dc:date', // a date construct

'createdAtTimestamp' => 'dc:date', // a date construct
'lastUpdatedTimestamp' => 'dc:date', // a date construct

'author' => 'dc:creator || dc:contributor',
'category' => 'dc:coverage || dc:subject/taxo:topic/@rdf:value || dc:subject || ' .
'parent::rdf:RDF/def:channel/dc:coverage || ' .
'parent::rdf:RDF/def:channel/dc:subject/taxo:topic/@rdf:value || ' .
'parent::rdf:RDF/def:channel/dc:subject/taxo:topic/@rdf:value || ' .
'parent::rdf:RDF/def:channel/dc:subject' // defaults to the parent category
];

Expand All @@ -41,4 +46,4 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
$parser_options
);
}
}
}
9 changes: 7 additions & 2 deletions src/FeedItems/RSSFeedItem.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);

namespace Forensic\FeedParser\FeedItems;

Expand All @@ -26,6 +27,10 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
//'image' => { 'src' => '', 'link' => '', 'title' => '' }, // to be parsed specially
'createdAt' => 'pubDate',
'lastUpdated' => 'lastBuildDate || pubDate',

'createdAtTimestamp' => 'pubDate',
'lastUpdatedTimestamp' => 'lastBuildDate || pubDate',

'author' => 'author || dc:creator',
'category' => 'category'
];
Expand All @@ -38,4 +43,4 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
$parser_options
);
}
}
}
Loading

0 comments on commit 0929b39

Please sign in to comment.