Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Added Tag URI support when rendering Atom Entries - fixes ZF-10246
Browse files Browse the repository at this point in the history
  • Loading branch information
Padraic Brady committed Aug 1, 2010
1 parent 20424e8 commit fe58bcb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/Writer/Renderer/Entry/Atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Zend\Feed\Writer;
use Zend\Date;
use Zend\URI;
use Zend\Validator;

/**
* @uses DOMDocument
Expand All @@ -36,7 +37,8 @@
* @uses \Zend\Feed\Writer\Renderer\Feed\Atom\Source
* @uses \Zend\Feed\Writer\Renderer\RendererAbstract
* @uses \Zend\Feed\Writer\Renderer\RendererInterface
* @uses \Zend\Uri\Uri
* @uses \Zend\Uri\Url
* @uses \Zend\Validator
* @uses tidy
* @category Zend
* @package Zend_Feed_Writer
Expand Down Expand Up @@ -292,7 +294,9 @@ protected function _setId(\DOMDocument $dom, \DOMElement $root)
$this->getDataContainer()->getLink());
}
if (!URI\URL::validate($this->getDataContainer()->getId()) &&
!preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", $this->getDataContainer()->getId())) {
!preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#",
$this->getDataContainer()->getId()
) && !$this->_validateTagUri($this->getDataContainer()->getId())) {
throw new Exception('Atom 1.0 IDs must be a valid URI/IRI');
}
$id = $dom->createElement('id');
Expand All @@ -301,6 +305,38 @@ protected function _setId(\DOMDocument $dom, \DOMElement $root)
$id->appendChild($text);
}

/**
* Validate a URI using the tag scheme (RFC 4151)
*
* @param string $id
* @return bool
*/
protected function _validateTagUri($id)
{
if (preg_match('/^tag:(?<name>.*),(?<date>\d{4}-?\d{0,2}-?\d{0,2}):(?<specific>.*)(.*:)*$/', $id, $matches)) {
$dvalid = false;
$nvalid = false;
$date = $matches['date'];
$d6 = strtotime($date);
if ((strlen($date) == 4) && $date <= date('Y')) {
$dvalid = true;
} elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) {
$dvalid = true;
} elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) {
$dvalid = true;
}
$validator = new Validator\EmailAddress;
if ($validator->isValid($matches['name'])) {
$nvalid = true;
} else {
$nvalid = $validator->isValid('info@' . $matches['name']);
}
return $dvalid && $nvalid;

}
return false;
}

/**
* Set entry content
*
Expand Down
18 changes: 18 additions & 0 deletions test/Writer/Renderer/Entry/AtomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ public function testEntryIdHasBeenSet()
$entry = $feed->current();
$this->assertEquals('urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6', $entry->getId());
}

public function testEntryIdHasBeenSetUsingSimpleTagUri()
{
$this->_validEntry->setId('tag:example.org,2010:/foo/bar/');
$renderer = new Renderer\Feed\Atom($this->_validWriter);
$feed = Reader\Reader::importString($renderer->render()->saveXml());
$entry = $feed->current();
$this->assertEquals('tag:example.org,2010:/foo/bar/', $entry->getId());
}

public function testEntryIdHasBeenSetUsingComplexTagUri()
{
$this->_validEntry->setId('tag:diveintomark.org,2004-05-27:/archives/2004/05/27/howto-atom-linkblog');
$renderer = new Renderer\Feed\Atom($this->_validWriter);
$feed = Reader\Reader::importString($renderer->render()->saveXml());
$entry = $feed->current();
$this->assertEquals('tag:diveintomark.org,2004-05-27:/archives/2004/05/27/howto-atom-linkblog', $entry->getId());
}

public function testFeedIdDefaultIsUsedIfNotSetByHand()
{
Expand Down

0 comments on commit fe58bcb

Please sign in to comment.