1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.feed.writer">
4 <title>Zend_Feed_Writer</title>
6 <sect2 id="zend.feed.writer.introduction">
7 <title>Introduction</title>
10 <classname>Zend_Feed_Writer</classname> is the sibling component to
11 <classname>Zend_Feed_Reader</classname> responsible for generating feeds for output. It
12 supports the Atom 1.0 specification (<acronym>RFC</acronym> 4287) and
13 <acronym>RSS</acronym> 2.0 as specified by the <acronym>RSS</acronym> Advisory Board
14 (<acronym>RSS</acronym> 2.0.11). It does not deviate from these standards. It does,
15 however, offer a simple Extension system which allows for any extension and module for
16 either of these two specifications to be implemented if they are not provided out of the
21 In many ways, <classname>Zend_Feed_Writer</classname> is the inverse of
22 <classname>Zend_Feed_Reader</classname>. Where <classname>Zend_Feed_Reader</classname>
23 focuses on providing an easy to use architecture fronted by getter methods,
24 <classname>Zend_Feed_Writer</classname> is fronted by similarly named setters or
25 mutators. This ensures the <acronym>API</acronym> won't pose a learning curve to anyone
26 familiar with <classname>Zend_Feed_Reader</classname>.
30 As a result of this design, the rest may even be obvious. Behind the scenes, data set on
31 any <classname>Zend_Feed_Writer</classname> Data Container object is translated at
32 render time onto a DOMDocument object using the necessary feed elements. For each
33 supported feed type there is both an Atom 1.0 and <acronym>RSS</acronym> 2.0 renderer.
34 Using a DOMDocument class rather than a templating solution has numerous advantages,
35 the most obvious being the ability to export the DOMDocument for
36 additional processing and relying on <acronym>PHP</acronym> <acronym>DOM</acronym> for
37 correct and valid rendering.
41 As with <classname>Zend_Feed_Reader</classname>, <classname>Zend_Feed_Writer</classname>
42 is a standalone replacement for <classname>Zend_Feed</classname>'s Builder architecture
43 and is not compatible with those classes.
47 <sect2 id="zend.feed.writer.architecture">
48 <title>Architecture</title>
51 The architecture of <classname>Zend_Feed_Writer</classname> is very simple. It has two
52 core sets of classes: data containers and renderers.
56 The containers include the <classname>Zend_Feed_Writer_Feed</classname> and
57 <classname>Zend_Feed_Writer_Entry</classname> classes. The Entry classes can be attached
58 to any Feed class. The sole purpose of these containers is to collect data about the
59 feed to generate using a simple interface of setter methods. These methods perform some
60 data validity testing. For example, it will validate any passed <acronym>URI</acronym>s,
61 dates, etc. These checks are not tied to any of the feed standards definitions. The
62 container objects also contain methods to allow for fast rendering and export of the
63 final feed, and these can be reused at will.
67 In addition to the main data container classes, there are two additional Atom 2.0
68 specific classes. <classname>Zend_Feed_Writer_Source</classname> and
69 <classname>Zend_Feed_Writer_Deleted</classname>. The former implements Atom 2.0 source
70 elements which carry source feed metadata for a specific entry within an aggregate feed
71 (i.e. the current feed is not the entry's original source). The latter implements the
72 Atom Tombstones <acronym>RFC</acronym> allowing feeds to carry references to entries
73 which have been deleted.
77 While there are two main data container types, there are four renderers - two matching
78 container renderers per supported feed type. Each renderer accepts a container, and
79 based on its content attempts to generate valid feed markup. If the renderer is unable
80 to generate valid feed markup, perhaps due to the container missing an obligatory data
81 point, it will report this by throwing an <classname>Exception</classname>. While it is
82 possible to ignore <classname>Exception</classname>s, this removes the default safeguard
83 of ensuring you have sufficient data set to render a wholly valid feed.
87 To explain this more clearly, you may construct a set of data containers for a feed
88 where there is a Feed container, into which has been added some Entry containers and a
89 Deleted container. This forms a data hierarchy resembling a normal feed. When rendering
90 is performed, this hierarchy has its pieces passed to relevant renderers and the partial
91 feeds (all DOMDocuments) are then pieced together to create a complete feed. In the case
92 of Source or Deleted (Tomestone) containers, these are rendered only for Atom 2.0 and
93 ignored for <acronym>RSS</acronym>.
97 Due to the system being divided between data containers and renderers, it can make
98 Extensions somewhat interesting. A typical Extension offering namespaced feed and entry
99 level elements, must itself reflect the exact same architecture, i.e. offer feed and
100 entry level data containers, and matching renderers. There is, fortunately, no complex
101 integration work required since all Extension classes are simply registered and
102 automatically used by the core classes. We'll meet Extensions in more detail at the end
107 <sect2 id="zend.feed.writer.getting.started">
108 <title>Getting Started</title>
111 Using <classname>Zend_Feed_Writer</classname> is as simple as setting data and
112 triggering the renderer. Here is an example to generate a minimal Atom 1.0 feed.
113 As this demonstrates, each feed or entry uses a separate data container.
116 <programlisting language="php"><![CDATA[
118 * Create the parent feed
120 $feed = new Zend_Feed_Writer_Feed;
121 $feed->setTitle('Paddy\'s Blog');
122 $feed->setLink('http://www.example.com');
123 $feed->setFeedLink('http://www.example.com/atom', 'atom');
124 $feed->addAuthor(array(
126 'email' => 'paddy@example.com',
127 'uri' => 'http://www.example.com',
129 $feed->setDateModified(time());
130 $feed->addHub('http://pubsubhubbub.appspot.com/');
133 * Add one or more entries. Note that entries must
134 * be manually added once created.
136 $entry = $feed->createEntry();
137 $entry->setTitle('All Your Base Are Belong To Us');
138 $entry->setLink('http://www.example.com/all-your-base-are-belong-to-us');
139 $entry->addAuthor(array(
141 'email' => 'paddy@example.com',
142 'uri' => 'http://www.example.com',
144 $entry->setDateModified(time());
145 $entry->setDateCreated(time());
146 $entry->setDescription('Exposing the difficultly of porting games to English.');
148 'I am not writing the article. The example is long enough as is ;).'
150 $feed->addEntry($entry);
153 * Render the resulting feed to Atom 1.0 and assign to $out.
154 * You can substitute "atom" with "rss" to generate an RSS 2.0 feed.
156 $out = $feed->export('atom');
160 The output rendered should be as follows:
163 <programlisting language="xml"><![CDATA[
164 <?xml version="1.0" encoding="utf-8"?>
165 <feed xmlns="http://www.w3.org/2005/Atom">
166 <title type="text">Paddy's Blog</title>
167 <subtitle type="text">Writing about PC Games since 176 BC.</subtitle>
168 <updated>2009-12-14T20:28:18+00:00</updated>
169 <generator uri="http://framework.zend.com" version="1.10.0alpha">
172 <link rel="alternate" type="text/html" href="http://www.example.com"/>
173 <link rel="self" type="application/atom+xml"
174 href="http://www.example.com/atom"/>
175 <id>http://www.example.com</id>
178 <email>paddy@example.com</email>
179 <uri>http://www.example.com</uri>
181 <link rel="hub" href="http://pubsubhubbub.appspot.com/"/>
183 <title type="html"><![CDATA[All Your Base Are Belong To
184 Us]]]]><![CDATA[></title>
185 <summary type="html">
186 <![CDATA[Exposing the difficultly of porting games to
187 English.]]]]><![CDATA[>
189 <published>2009-12-14T20:28:18+00:00</published>
190 <updated>2009-12-14T20:28:18+00:00</updated>
191 <link rel="alternate" type="text/html"
192 href="http://www.example.com/all-your-base-are-belong-to-us"/>
193 <id>http://www.example.com/all-your-base-are-belong-to-us</id>
196 <email>paddy@example.com</email>
197 <uri>http://www.example.com</uri>
199 <content type="html">
200 <![CDATA[I am not writing the article.
201 The example is long enough as is ;).]]]]><![CDATA[>
208 This is a perfectly valid Atom 1.0 example. It should be noted that omitting an
209 obligatory point of data, such as a title, will trigger an
210 <classname>Exception</classname> when rendering as Atom 1.0. This will differ for
211 <acronym>RSS</acronym> 2.0 since a title may be omitted so long as a description is
212 present. This gives rise to Exceptions that differ between the two standards depending
213 on the renderer in use. By design, <classname>Zend_Feed_Writer</classname> will not
214 render an invalid feed for either standard unless the end-user deliberately elects to
215 ignore all Exceptions. This built in safeguard was added to ensure users without
216 in-depth knowledge of the relevant specifications have a bit less to worry about.
220 <sect2 id="zend.feed.writer.setting.feed.data.points">
221 <title>Setting Feed Data Points</title>
224 Before you can render a feed, you must first setup the data necessary for
225 the feed being rendered. This utilises a simple setter style <acronym>API</acronym>
226 which doubles as an initial method for validating the data being set. By design, the
227 <acronym>API</acronym> closely matches that for <classname>Zend_Feed_Reader</classname>
228 to avoid undue confusion and uncertainty.
232 <para>Users have commented that the lack of a simple array based notation for input data
233 gives rise to lengthy tracts of code. This will be addressed in a future release.</para>
237 <classname>Zend_Feed_Writer</classname> offers this <acronym>API</acronym> via its data
238 container classes <classname>Zend_Feed_Writer_Feed</classname> and
239 <classname>Zend_Feed_Writer_Entry</classname> (not to mention the Atom 2.0 specific
240 and Extension classes). These classes merely store
241 all feed data in a type-agnostic manner, meaning you may reuse any data
242 container with any renderer without requiring additional work. Both classes
243 are also amenable to Extensions, meaning that an Extension may define its own
244 container classes which are registered to the base container classes as extensions, and
245 are checked when any method call triggers the base container's
246 <methodname>__call()</methodname> method.
250 Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
251 comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
252 accounts for a number of included Extensions bundled with
253 <classname>Zend_Feed_Writer</classname>. The naming of these
254 Extension sourced methods remain fairly generic - all Extension
255 methods operate at the same level as the Core <acronym>API</acronym> though we do allow
256 you to retrieve any specific Extension object separately if required.
260 The Feed Level <acronym>API</acronym> for data is contained in
261 <classname>Zend_Feed_Writer_Feed</classname>. In addition to the <acronym>API</acronym>
262 detailed below, the class also implements the <classname>Countable</classname> and
263 <classname>Iterator</classname> interfaces.
267 <title>Feed Level API Methods</title>
272 <entry><methodname>setId()</methodname></entry>
275 Set a unique ID associated with this feed. For Atom 1.0
276 this is an atom:id element, whereas for <acronym>RSS</acronym> 2.0 it
277 is added as a guid element. These are optional so long as a link is
278 added, i.e. the link is set as the ID.
283 <entry><methodname>setTitle()</methodname></entry>
284 <entry>Set the title of the feed.</entry>
288 <entry><methodname>setDescription()</methodname></entry>
289 <entry>Set the text description of the feed.</entry>
293 <entry><methodname>setLink()</methodname></entry>
296 Set a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
297 containing the same or
298 similar information as this feed (i.e. if the feed is from a blog,
299 it should provide the blog's <acronym>URI</acronym> where the
300 <acronym>HTML</acronym> version of the entries can be read).
305 <entry><methodname>setFeedLinks()</methodname></entry>
308 Add a link to an <acronym>XML</acronym> feed, whether the feed being
309 generated or an alternate <acronym>URI</acronym> pointing to the same
310 feed but in a different format. At a minimum, it is recommended to
311 include a link to the feed being generated so it has an identifiable
312 final <acronym>URI</acronym> allowing a client to track its location
313 changes without necessitating constant redirects. The parameter is an
314 array of arrays, where each sub-array contains the keys "type" and
315 "uri". The type should be one of "atom", "rss", or "rdf".
320 <entry><methodname>addAuthors()</methodname></entry>
323 Sets the data for authors. The parameter is an array of arrays
324 where each sub-array may contain the keys "name", "email" and
325 "uri". The "uri" value is only applicable for Atom feeds since
326 <acronym>RSS</acronym> contains no facility to show it. For
327 <acronym>RSS</acronym> 2.0, rendering will create two elements - an
328 author element containing the email reference with the name in brackets,
329 and a Dublin Core creator element only containing the name.
334 <entry><methodname>addAuthor()</methodname></entry>
337 Sets the data for a single author following the same
338 array format as described above for a single sub-array.
343 <entry><methodname>setDateCreated()</methodname></entry>
346 Sets the date on which this feed was created. Generally
347 only applicable to Atom where it represents the date the resource
348 described by an Atom 1.0 document was created. The expected parameter
349 may be a <acronym>UNIX</acronym> timestamp or a
350 <classname>Zend_Date</classname> object.
355 <entry><methodname>setDateModified()</methodname></entry>
358 Sets the date on which this feed was last modified. The expected
359 parameter may be a <acronym>UNIX</acronym> timestamp or a
360 <classname>Zend_Date</classname> object.
365 <entry><methodname>setLastBuildDate()</methodname></entry>
368 Sets the date on which this feed was last build. The expected
369 parameter may be a <acronym>UNIX</acronym> timestamp or a
370 <classname>Zend_Date</classname> object. This will only be
371 rendered for <acronym>RSS</acronym> 2.0 feeds and is automatically
372 rendered as the current date by default when not explicity set.
377 <entry><methodname>setLanguage()</methodname></entry>
380 Sets the language of the feed. This will be omitted unless set.
385 <entry><methodname>setGenerator()</methodname></entry>
388 Allows the setting of a generator. The parameter should be an
389 array containing the keys "name", "version" and "uri". If omitted
390 a default generator will be added referencing
391 <classname>Zend_Feed_Writer</classname>, the current Zend Framework
392 version and the Framework's <acronym>URI</acronym>.
397 <entry><methodname>setCopyright()</methodname></entry>
398 <entry>Sets a copyright notice associated with the feed.</entry>
402 <entry><methodname>addHubs()</methodname></entry>
405 Accepts an array of Pubsubhubbub Hub Endpoints to be rendered in
406 the feed as Atom links so that PuSH Subscribers may subscribe to
407 your feed. Note that you must implement a Pubsubhubbub Publisher in
408 order for real-time updates to be enabled. A Publisher may be
410 <classname>Zend_Feed_Pubsubhubbub_Publisher</classname>.
411 The method <methodname>addHub()</methodname> allows adding
412 a single hub at a time.
417 <entry><methodname>addCategories()</methodname></entry>
420 Accepts an array of categories for rendering, where each element is
421 itself an array whose possible keys include "term", "label" and
422 "scheme". The "term" is a typically a category name suitable for
423 inclusion in a <acronym>URI</acronym>. The "label" may be a human
424 readable category name supporting special characters (it is
425 <acronym>HTML</acronym> encoded during rendering) and is a required key.
426 The "scheme" (called the domain in <acronym>RSS</acronym>) is optional
427 but must be a valid <acronym>URI</acronym>. The method
428 <methodname>addCategory()</methodname> allows adding a single category
434 <entry><methodname>setImage()</methodname></entry>
437 Accepts an array of image metadata for an <acronym>RSS</acronym> image
438 or Atom logo. Atom 1.0 only requires a <acronym>URI</acronym>.
439 <acronym>RSS</acronym> 2.0 requires a <acronym>URI</acronym>,
440 <acronym>HTML</acronym> link, and an image title. <acronym>RSS</acronym>
441 2.0 optionally may send a width, height and image description. The array
442 parameter may contain these using the keys: <property>uri</property>,
443 <property>link</property>, <property>title</property>,
444 <property>description</property>, <property>height</property> and
445 <property>width</property>. The <acronym>RSS</acronym> 2.0
446 <acronym>HTML</acronym> link should point to the feed source's
447 <acronym>HTML</acronym> page.
452 <entry><methodname>createEntry()</methodname></entry>
455 Returns a new instance of
456 <classname>Zend_Feed_Writer_Entry</classname>. This is the Entry level
457 data container. New entries are not automatically assigned to the
458 current feed, so you must explicitly call
459 <methodname>addEntry()</methodname> to add the entry for rendering.
464 <entry><methodname>addEntry()</methodname></entry>
467 Adds an instance of <classname>Zend_Feed_Writer_Entry</classname>
468 to the current feed container for rendering.
473 <entry><methodname>createTombstone()</methodname></entry>
476 Returns a new instance of
477 <classname>Zend_Feed_Writer_Deleted</classname>. This is the Atom 2.0
478 Tombstone level data container. New entries are not automatically
479 assigned to the current feed, so you must explicitly call
480 <methodname>addTombstone()</methodname> to add the deleted entry for
486 <entry><methodname>addTombstone()</methodname></entry>
489 Adds an instance of <classname>Zend_Feed_Writer_Deleted</classname> to
490 the current feed container for rendering.
495 <entry><methodname>removeEntry()</methodname></entry>
498 Accepts a parameter indicating an array index of the entry to remove
504 <entry><methodname>export()</methodname></entry>
507 Exports the entire data hierarchy to an <acronym>XML</acronym> feed. The
508 method has two parameters. The first is the feed type, one of "atom"
509 or "rss". The second is an optional boolean to set whether
510 Exceptions are thrown. The default is <constant>TRUE</constant>.
519 In addition to these setters, there are also matching getters to retrieve data from
520 the Entry data container. For example, <methodname>setImage()</methodname> is
521 matched with a <methodname>getImage()</methodname> method.
525 <!-- remaining feed stuff -->
529 <sect2 id="zend.feed.writer.setting.entry.data.points">
530 <title>Setting Entry Data Points</title>
533 Here's a summary of the Core <acronym>API</acronym> for Entries and Items. You should
534 note it comprises not only the basic <acronym>RSS</acronym> and Atom standards, but
535 also accounts for a number of included Extensions bundled with
536 <classname>Zend_Feed_Writer</classname>. The naming of these
537 Extension sourced methods remain fairly generic - all Extension
538 methods operate at the same level as the Core <acronym>API</acronym> though we do allow
539 you to retrieve any specific Extension object separately if required.
543 The Entry Level <acronym>API</acronym> for data is contained in
544 <classname>Zend_Feed_Writer_Entry</classname>.
548 <title>Entry Level API Methods</title>
553 <entry><methodname>setId()</methodname></entry>
556 Set a unique ID associated with this entry. For Atom 1.0
557 this is an atom:id element, whereas for <acronym>RSS</acronym> 2.0 it is
558 added as a guid element. These are optional so long as a link is
559 added, i.e. the link is set as the ID.
564 <entry><methodname>setTitle()</methodname></entry>
565 <entry>Set the title of the entry.</entry>
569 <entry><methodname>setDescription()</methodname></entry>
570 <entry>Set the text description of the entry.</entry>
574 <entry><methodname>setContent()</methodname></entry>
575 <entry>Set the content of the entry.</entry>
579 <entry><methodname>setLink()</methodname></entry>
582 Set a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
583 containing the same or
584 similar information as this entry (i.e. if the feed is from a blog,
585 it should provide the blog article's <acronym>URI</acronym> where the
586 <acronym>HTML</acronym> version of the entry can be read).
591 <entry><methodname>setFeedLinks()</methodname></entry>
594 Add a link to an <acronym>XML</acronym> feed, whether the feed being
595 generated or an alternate <acronym>URI</acronym> pointing to the same
596 feed but in a different format. At a minimum, it is recommended to
597 include a link to the feed being generated so it has an identifiable
598 final <acronym>URI</acronym> allowing a client to track its location
599 changes without necessitating constant redirects. The parameter is an
600 array of arrays, where each sub-array contains the keys "type" and
601 "uri". The type should be one of "atom", "rss", or "rdf". If a type is
602 omitted, it defaults to the type used when rendering the feed.
607 <entry><methodname>addAuthors()</methodname></entry>
610 Sets the data for authors. The parameter is an array of arrays
611 where each sub-array may contain the keys "name", "email" and
612 "uri". The "uri" value is only applicable for Atom feeds since
613 <acronym>RSS</acronym> contains no facility to show it. For
614 <acronym>RSS</acronym> 2.0, rendering will create two elements - an
615 author element containing the email reference with the name in brackets,
616 and a Dublin Core creator element only containing the name.
621 <entry><methodname>addAuthor()</methodname></entry>
624 Sets the data for a single author following the same
625 format as described above for a single sub-array.
630 <entry><methodname>setDateCreated()</methodname></entry>
633 Sets the date on which this feed was created. Generally
634 only applicable to Atom where it represents the date the resource
635 described by an Atom 1.0 document was created. The expected parameter
636 may be a <acronym>UNIX</acronym> timestamp or a
637 <classname>Zend_Date</classname> object. If omitted, the date
638 used will be the current date and time.
643 <entry><methodname>setDateModified()</methodname></entry>
646 Sets the date on which this feed was last modified. The expected
647 parameter may be a <acronym>UNIX</acronym> timestamp or a
648 <classname>Zend_Date</classname> object. If omitted, the date
649 used will be the current date and time.
654 <entry><methodname>setCopyright()</methodname></entry>
655 <entry>Sets a copyright notice associated with the feed.</entry>
659 <entry><methodname>setCategories()</methodname></entry>
662 Accepts an array of categories for rendering, where each element is
663 itself an array whose possible keys include "term", "label" and
664 "scheme". The "term" is a typically a category name suitable for
665 inclusion in a <acronym>URI</acronym>. The "label" may be a human
666 readable category name supporting special characters (it is encoded
667 during rendering) and is a required key. The "scheme" (called the domain
668 in <acronym>RSS</acronym>) is optional but must be a valid
669 <acronym>URI</acronym>.
674 <entry><methodname>setCommentCount()</methodname></entry>
677 Sets the number of comments associated with this entry. Rendering
678 differs between <acronym>RSS</acronym> and Atom 2.0 depending
679 on the element or attribute needed.
684 <entry><methodname>setCommentLink()</methodname></entry>
687 Seta a link to a <acronym>HTML</acronym> page containing comments
688 associated with this entry.
693 <entry><methodname>setCommentFeedLink()</methodname></entry>
696 Sets a link to a <acronym>XML</acronym> feed containing comments
697 associated with this entry. The parameter is an array containing the
698 keys "uri" and "type", where the type is one of "rdf", "rss" or "atom".
703 <entry><methodname>setCommentFeedLinks()</methodname></entry>
706 Same as <methodname>setCommentFeedLink()</methodname> except it
707 accepts an array of arrays, where each subarray contains the
708 expected parameters of <methodname>setCommentFeedLink()</methodname>.
713 <entry><methodname>setEncoding()</methodname></entry>
716 Sets the encoding of entry text. This will default to
717 <acronym>UTF-8</acronym> which is the preferred encoding.
726 In addition to these setters, there are also matching getters to retrieve data from
727 the Entry data container.