Skip to content

Commit

Permalink
fix: fixed dates
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Jun 11, 2020
1 parent d66fd2d commit 68f5673
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 87 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ The following configuration options can be passed in when creating an instance o
//constructor signature
new Parser(
string $default_lang = 'en',
string $date_template = '',
bool $remove_styles = true,
bool $remove_scripts = true
);
Expand All @@ -103,15 +102,6 @@ new Parser(
$parser->setDefaultLanguage('fr');
```

- **date_template**:

This option sets the date formatter template used when parsing feed date properties such as `lastUpdated`. the default format used is `'jS F, Y, g:i A'`. The formatter template should be a valid php date formatter argument. see [date](http://php.net/manual/en/function.date.php) for details.

```php
$parser = new Parser();
$parser->setDateTemplate('jS F, y, g:i a');
```

- **remove_styles**:

This option states if stylings found in any feed item's content, should be stripped off. The stylings include html `style` element and attribute. Defaults to true.
Expand Down
6 changes: 0 additions & 6 deletions src/FeedItems/ATOMFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ 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
10 changes: 0 additions & 10 deletions src/FeedItems/BaseFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,6 @@ class BaseFeedItem
*/
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
*/
Expand Down
3 changes: 0 additions & 3 deletions src/FeedItems/RDFFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
'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 || ' .
Expand Down
3 changes: 0 additions & 3 deletions src/FeedItems/RSSFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public function __construct(DOMElement $item, XPath $xpath, array $parser_option
'createdAt' => 'pubDate',
'lastUpdated' => 'lastBuildDate || pubDate',

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

'author' => 'author || dc:creator',
'category' => 'category'
];
Expand Down
27 changes: 1 addition & 26 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class Parser
*/
private $_options = [
'remove-styles' => null,
'remove-scripts' => null,
'date-template' => ''
'remove-scripts' => null
];

/**
Expand All @@ -49,12 +48,10 @@ class Parser
*/
public function __construct(
string $default_lang = 'en',
string $date_template = '',
bool $remove_styles = true,
bool $remove_scripts = true
) {
$this->setDefaultLanguage($default_lang);
$this->setDateTemplate($date_template);
$this->removeStyles($remove_styles);
$this->removeScripts($remove_scripts);
}
Expand All @@ -76,28 +73,6 @@ public function getDefaultLanguage()
return $this->_default_lang;
}

/**
* sets date template used when processing dates
*/
public function setDateTemplate(string $date_template)
{
if ($date_template !== '')
$this->_options['date-template'] = $date_template;

else if ($this->_options['date-template'] === '')
$this->_options['date-template'] = 'jS F, Y, g:i A';

return $this;
}

/**
* return date template used when processing dates
*/
public function getDateTemplate()
{
return $this->_options['date-template'];
}

/**
* sets or returns the remove styles parse attributes
*
Expand Down
16 changes: 5 additions & 11 deletions src/Traits/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Forensic\FeedParser\XPath;


trait Parser
{
private function filterPropertyValue(
Expand All @@ -19,16 +18,11 @@ private function filterPropertyValue(
}

switch ($property_name) {
case 'lastUpdated':
case 'createdAt':
$timestamp = strtotime($value);
$value = date($parser_options['date-template'], $timestamp);
break;

case 'lastUpdatedTimestamp':
case 'createdAtTimestamp':
$value = strtotime($value) * 1000;
break;
// case 'lastUpdated':
// case 'createdAt':
// $timestamp = strtotime($value);
// $value = date($parser_options['date-template'], $timestamp);
// break;

case 'textContent':
$value = \strip_tags($value);
Expand Down
24 changes: 6 additions & 18 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public function testDefaultLanguage()
$this->assertSame('en', $this->_parser->getDefaultLanguage());
}

public function testDefaultDateTemplate()
{
$this->assertEquals('jS F, Y, g:i A', $this->_parser->getDateTemplate());
}

public function testDefaultRemoveStyles()
{
$this->assertTrue($this->_parser->removeStyles());
Expand All @@ -51,12 +46,6 @@ public function testUpdateDefaultLanguage()
$this->assertSame('fr', $this->_parser->getDefaultLanguage());
}

public function testUpdateDateTemplate()
{
$this->_parser->setDateTemplate('jS f, Y, g:i A');
$this->assertSame('jS f, Y, g:i A', $this->_parser->getDateTemplate());
}

public function testUpdateRemoveStyles()
{
$this->_parser->removeStyles(false);
Expand All @@ -71,7 +60,7 @@ public function testUpdateRemoveScripts()

public function testConstructSettings()
{
$parser = new Parser('fr', '', false, false);
$parser = new Parser('fr', false, false);
$this->assertSame('fr', $parser->getDefaultLanguage());
$this->assertFalse($parser->removeStyles());
$this->assertFalse($parser->removeScripts());
Expand Down Expand Up @@ -133,7 +122,7 @@ public function testFromExistingURL()

public function testFromString()
{
$parser = new Parser('en', '', false, false);
$parser = new Parser('en', false, false);
$feed = $parser->parseFromString(
file_get_contents(__DIR__ . '/Helpers/Feeds/atom.xml')
);
Expand All @@ -145,7 +134,7 @@ public function testFromString()
*/
public function testFeedExistingPropertyAccessibility()
{
$parser = new Parser('en', '', false, false);
$parser = new Parser('en', false, false);
$feed = $parser->parseFromString(
file_get_contents(__DIR__ . '/Helpers/Feeds/atom.xml')
);
Expand All @@ -157,7 +146,7 @@ public function testFeedExistingPropertyAccessibility()
*/
public function testFeedNonExistingPropertyAccessibility()
{
$parser = new Parser('en', '', false, false);
$parser = new Parser('en', false, false);
$feed = $parser->parseFromString(
file_get_contents(__DIR__ . '/Helpers/Feeds/atom.xml')
);
Expand All @@ -169,7 +158,7 @@ public function testFeedNonExistingPropertyAccessibility()
*/
public function testFeedItemExistingPropertyAccessibility()
{
$parser = new Parser('en', '', false, false);
$parser = new Parser('en', false, false);
$feed = $parser->parseFromString(
file_get_contents(__DIR__ . '/Helpers/Feeds/atom.xml')
);
Expand All @@ -181,7 +170,7 @@ public function testFeedItemExistingPropertyAccessibility()
*/
public function testFeedItemNonExistingPropertyAccessibility()
{
$parser = new Parser('en', '', false, false);
$parser = new Parser('en', false, false);
$feed = $parser->parseFromString(
file_get_contents(__DIR__ . '/Helpers/Feeds/atom.xml')
);
Expand Down Expand Up @@ -229,7 +218,6 @@ public function testFeedToJSONMethod()
public function testFeedItemToJSONMethod()
{
$feed = $this->_parser->parseFromFile(__DIR__ . '/Helpers/Feeds/rss.xml');
echo $feed->items[0]->toJSON();
$this->assertTrue(is_string($feed->items[0]->toJSON()));
}
}

0 comments on commit 68f5673

Please sign in to comment.