Skip to content

Instantly share code, notes, and snippets.

@tomoe-mami
Last active December 15, 2015 00:48
Show Gist options
  • Save tomoe-mami/5175049 to your computer and use it in GitHub Desktop.
Save tomoe-mami/5175049 to your computer and use it in GitHub Desktop.
Generating an Atom feed using Nekomata. https://github.com/rumia/nekomata
<?php
$doc = Nekomata::create(array(
'qualified_name' => 'feed',
'namespaces' => array('_' => 'http://www.w3.org/2005/Atom')
));
// to create a new node, just call Nekomata->_().
// the first arg is the node name, second arg is the node value,
// and the third arg is an array of attributes.
// the following line will create `<title type="text">dive into mark</title>`
$doc->_('title', 'dive into mark', array('type' => 'text'));
// node value will be escaped automatically. if you want to put unescaped text,
// use Nekomata->fragment() (scroll down for example of fragment)
$doc->_(
'subtitle',
'A <em>lot</em> of effort went into making this effortless',
array('type' => 'html'));
// to make things easier, you can use method chaining
$doc
->_('updated', '2005-07-31T12:29:29Z')
->_('id', 'tag:example.org,2003:3')
// a node value of false will create a self-closing tag `<like-this/>`
->_('link', false, array(
'rel' => 'alternate',
'hreflang' => 'en',
'href' => 'http://example.org',
'type' => 'text/html'))
->_('link', false, array(
'rel' => 'self',
'type' => 'application/atom+xml',
'href' => 'http://example.org/feed.atom'))
->_('rights', 'Copyright (C) 2003, Mark Pilgrim')
->_('generator', 'Example Toolkit', array('uri' => 'http://example.com', 'version' => '1.0'))
// if you don't specify second arg or if the second arg equals to true,
// the newly created node will become the base of the next chain.
->_('entry')
->_('title', 'Atom draft-07 snapshot') // <title> will be put inside <entry>
->_('link', false, array(
'rel' => 'alternate',
'type' => 'text/html',
'href' => 'http://example.org/2005/04/02/atom/'))
->_('link', false, array(
'rel' => 'enclosure',
'type' => 'audio/mpeg',
'length' => 1337,
'href' => 'http://example.org/audio/ph34r_my_podcast.mp3'))
->_('id', 'tag:example.org,2003:3.2397')
->_('updated', '2005-07-31T12:29:29Z')
->_('published', '2003-12-13T08:29:29-04:00')
->_('author') // dive one more level
->_('name', 'Mark Pilgrim')
->_('uri', 'http://example.org')
->_() // to go to parent node, just call Nekomata->() without any argument
->_('contributor')
->_('name', 'Sam Ruby')
->_()
->_('contributor')
->_('name', 'Joe Gregorio')
->_()
->_('contributor')
->_('name', '-monoko-')
->_()
->_('content', true, array(
'type' => 'xhtml',
'xml:lang' => 'en',
'xml:base' => 'http://diveintomark.org'))
->_('div', true, array('xmlns' => 'http://www.w3.org/1999/xhtml'))
->_('h1', 'Nunatak Gongamur')
// use Nekomata->fragment() to insert valid xml fragment.
// this method does not change the base node.
->fragment('<p><i>[Update: The Atom draft is finished.]</i></p>')
// if you're not sure about the validity of the fragment,
// pass a true value to its second arg to make it use html soup mode
->fragment('<p><strong>&foo<em><&a;></strong>', true);
// since Nekomata inherits SimpleXMLElement, you can use all SXE's magics.
// select node author and then add an email:
$doc->entry->author->_('email', '[email protected]');
// insert a new node before entry/content
$doc->entry->content->insert('category', false, array(
'term' => 'sample',
'scheme' => 'http://example.org/sample',
'label' => 'Sample Document'));
// change the name of the second contributor:
$doc->entry->contributor[1]->name = 'Yakumo Yukari';
// remove -monoko- from contributor:
unset($doc->entry->contributor[2]);
// sets attribute for <h1> inside content:
$doc->entry->content->div->h1['class'] = 'article-title';
header('Content-Type: application/atom+xml');
// finally, output the document.
$doc->render('php://output', false, true);
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">dive into mark</title>
<subtitle type="html">A &lt;em&gt;lot&lt;/em&gt; of effort went into making this effortless</subtitle>
<updated>2005-07-31T12:29:29Z</updated>
<id>tag:example.org,2003:3</id>
<link rel="alternate" hreflang="en" href="http://example.org" type="text/html"/>
<link rel="self" type="application/atom+xml" href="http://example.org/feed.atom"/>
<rights>Copyright (C) 2003, Mark Pilgrim</rights>
<generator uri="http://example.com" version="1.0">Example Toolkit</generator>
<entry>
<title>Atom draft-07 snapshot</title>
<link rel="alternate" type="text/html" href="http://example.org/2005/04/02/atom/"/>
<link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
<id>tag:example.org,2003:3.2397</id>
<updated>2005-07-31T12:29:29Z</updated>
<published>2003-12-13T08:29:29-04:00</published>
<author>
<name>Mark Pilgrim</name>
<uri>http://example.org</uri>
<email>[email protected]</email>
</author>
<contributor>
<name>Sam Ruby</name>
</contributor>
<contributor>
<name>Yakumo Yukari</name>
</contributor>
<category term="sample" scheme="http://example.org/sample" label="Sample Document"/>
<content type="xhtml" xml:lang="en" xml:base="http://diveintomark.org">
<div xmlns="http://www.w3.org/1999/xhtml">
<h1 class="article-title">Nunatak Gongamur</h1>
<p>
<i>[Update: The Atom draft is finished.]</i>
</p>
<p>
<strong>&amp;foo<em/></strong>
</p>
</div>
</content>
</entry>
</feed>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment