Zeta Components Manual :: Docs For Class ezcFeed
Feed::ezcFeed
Class ezcFeed
Class defining a feed.
A feed has a type (eg. RSS1, RSS2 or ATOM). The feed type defines which processor is used to parse and generate that type.
The following feed processors are supported by the Feed component:
- ATOM (ezcFeedAtom) - RFC4287
- RSS1 (ezcFeedRss1) - Specifications
- RSS2 (ezcFeedRss2) - Specifications
The following modules are supported by the Feed component:
- Content (ezcFeedContentModule) - Specifications
- CreativeCommons (ezcFeedCreativeCommonsModule) - Specifications
- DublinCore (ezcFeedDublinCoreModule) - Specifications
- Geo (ezcFeedGeoModule) - Specifications
- GeoRss (ezcFeedGeoRssModule) - Specifications
- iTunes (ezcFeedITunesModule) - Specifications
A feed object can be created in different ways:
- by calling the constructor (with the optional feed type). Example:
- by parsing an existing XML file or URL. The feed type of the resulting ezcFeed object will be autodetected. Example:
- by parsing an XML document stored in a string variable. The feed type of the resulting ezcFeed object will be autodetected. Example:
Parsing a feed (in the following examples $feed is an existing ezcFeed object):
- get a value from the feed object. Example:
- $title = $feed->title->__toString();
- iterate over the items in the feed. Example:
- <?php
- // retrieve the titles from the feed items
- foreach ( $feed->item as $item )
- {
- $titles[] = $item->title->__toString();
- }
- parse a module. Example of parsing the Geo module (ezcFeedGeoModule):
- <?php
- $locations = array();
- foreach ( $feed->item as $item )
- {
- if ( isset( $item->Geo ) )
- {
- $locations[] = array(
- 'title' => $item->title->__toString(),
- 'alt' => isset( $item->Geo->alt ) ? $item->Geo->alt->__toString() : null,
- 'lat' => isset( $item->Geo->lat ) ? $item->Geo->lat->__toString() : null,
- 'long' => isset( $item->Geo->long ) ? $item->Geo->long->__toString() : null
- );
- }
- }
- ?>
- iterate over the loaded modules in a feed item. Example:
- <?php
- // display the names and namespaces of the modules loaded in the feed item $item
- {
- echo $moduleName . ':' . $module->getNamespace();
- }
- ?>
Generating a feed:
- create a feed object. Example:
- set a value to the feed object. Example:
- $feed->title = 'News';
- add a new item to the feed. Example:
- <?php
- $item->title = 'Item title';
- ?>
- add a new module to the feed item. Example:
- <?php
- $content->encoded = 'text content which will be encoded';
- ?>
- generate an XML document from the ezcFeed object. The result string should be saved to a file, and a link to a file made accessible to users of the application. Example:
- <?php
- ?>
Note: Assigning values to feed elements should be done in a way that will not break the resulting XML document. In other words, encoding of special characters to HTML entities is not done by default, and the developer is responsible with calling htmlentities() himself when assigning values to feed elements. Example: if the feed title contains the "&" character, it is the responsability of the developer to encode it properly as "&".
Example of creating a feed with a user-defined type:
- <?php
- // add properties to $feed
- ?>
In the above example, myOpmlHandler extends ezcFeedProcessor and implements ezcFeedParser.
Example of creating a feed with a user-defined module:
- <?php
- // add properties for the Slash module to $slash
- ?>
In the above example mySlashHandler extends ezcFeedModule.
Source for this file: /Feed/src/feed.php
Version: | //autogentag// |
Todo: | parse() and parseContent() should(?) handle common broken XML files (for example if the first line is not <?xml version="1.0"?>) |
Constants
GENERATOR_URI
= 'http://ezcomponents.org/docs/tutorials/Feed'
|
The uri of the feed generator, to be included in the generated feeds. |
GENERATOR_VERSION
= '//autogentag//'
|
The version of the feed generator, to be included in the generated feeds. |
Properties
array(ezcFeedPersonElement) | read/write |
$author
Author(s) of the feed. Equivalents: ATOM-author (required, multiple), RSS1-none, RSS2-managingEditor (optional, recommended, single). |
array(ezcFeedCategoryElement) | read/write |
$category
Categories for the feed. Equivalents: ATOM-category (optional, multiple), RSS1-none, RSS2-category (optional, multiple). |
ezcFeedCloudElement | read/write |
$cloud
Allows processes to register with a cloud to be notified of updates to the channel, implementing a lightweight publish-subscribe protocol for RSS feeds. Equivalents: ATOM-none, RSS1-none, RSS2-cloud (optional, not recommended, single). |
array(ezcFeedPersonElement) | read/write |
$contributor
Contributor(s) for the feed. Equivalents: ATOM-contributor (optional, not recommended, multiple), RSS1-none, RSS2-none. |
ezcFeedTextElement | read/write |
$copyright
Copyright information for the feed. Equivalents: ATOM-rights (optional, single), RSS1-none, RSS2-copyright (optional, single). |
ezcFeedTextElement | read/write |
$description
A short description of the feed. Equivalents: ATOM-subtitle (required, single), RSS1-description (required, single), RSS2-description (required, single). |
ezcFeedTextElement | read/write |
$docs
An URL that points to the documentation for the format used in the feed file. Equivalents: ATOM-none, RSS1-none, RSS2-docs (optional, not recommended, single) - usual value is http://www.rssboard.org/rss-specification. |
ezcFeedGeneratorElement | read/write |
$generator
Indicates the software used to generate the feed. Equivalents: ATOM-generator (optional, single), RSS1-none, RSS2-generator (optional, single). |
ezcFeedImageElement | read/write |
$icon
An icon for a feed, similar with favicon.ico for websites. Equivalents: ATOM-icon (optional, not recommended, single), RSS1-none, RSS2-none. |
ezcFeedIdElement | read/write |
$id
A universally unique and permanent identifier for a feed. For example, it can be an Internet domain name. Equivalents: ATOM-id (required, single), RSS1-about (required, single), RSS2-id (optional, single). |
ezcFeedImageElement | read/write |
$image
An image associated with the feed. Equivalents: ATOM-logo (optional, single), RSS1-image (optional, single), RSS2-image (optional, single). |
array(ezcFeedEntryElement) | read |
$item
Feed items (entries). Equivalents: ATOM-entry (optional, recommended, multiple), RSS1-item (required, multiple), RSS2-item (required, multiple). |
ezcFeedTextElement | read/write |
$language
The language for the feed. Equivalents: ATOM-xml:lang attribute for title, description, copyright, content, comments (optional, single) - accessed as language through ezcFeed, RSS1-none, RSS2-language (optional, single). |
array(ezcFeedLinkElement) | read/write |
$link
URLs to the HTML websites corresponding to the channel. Equivalents: ATOM-link (required one link with rel='self', multiple), RSS1-link (required, single), RSS2-link (required, single). |
ezcFeedDateElement | read/write |
$published
The time the feed was published. Equivalents: ATOM-none, RSS1-none, RSS2-pubDate (optional, not recommended, single). |
ezcFeedTextElement | read/write |
$rating
The PICS rating for the channel. Equivalents: ATOM-none, RSS1-none, RSS2-rating (optional, not recommended, single). |
ezcFeedSkipDaysElement | read/write |
$skipDays
A hint for aggregators telling them which days they can skip when reading the feed. Equivalents: ATOM-none, RSS1-none, RSS2-skipDays (optional, not recommended, single). |
ezcFeedSkipHoursElement | read/write |
$skipHours
A hint for aggregators telling them which hours they can skip when reading the feed. Equivalents: ATOM-none, RSS1-none, RSS2-skipHours (optional, not recommended, single). |
ezcFeedTextInputElement | read/write |
$textInput
Specifies a text input box that can be displayed with the feed. Equivalents: ATOM-none, RSS1-textinput (optional, not recommended, single), RSS2-textInput (optional, not recommended, single). |
ezcFeedTextElement | read/write |
$title
Human readable title for the feed. For example, it can be the same as the website title. Equivalents: ATOM-title (required, single), RSS1-title (required, single), RSS2-title (required, single). |
ezcFeedTextElement | read/write |
$ttl
Number of minutes that indicates how long a channel can be cached before refreshing from the source. Equivalents: ATOM-none, RSS1-none, RSS2-ttl (optional, not recommended, single). |
ezcFeedDateElement | read/write |
$updated
The last time the feed was updated. Equivalents: ATOM-updated (required, single), RSS1-none, RSS2-lastBuildDate (optional, recommended, single). |
ezcFeedPersonElement | read/write |
$webMaster
The email address of the webmaster responsible for the feed. Equivalents: ATOM-none, RSS1-none, RSS2-webMaster (optional, not recommended, single). |
Method Summary
public static array(string=>string) |
getSupportedModules(
)
Returns the supported feed modules. |
public static array(string=>string) |
getSupportedModulesPrefixes(
)
Returns the supported feed modules prefixes. |
public static array(string) |
getSupportedTypes(
)
Returns the supported feed types. |
public static ezcFeed |
parse(
$uri
)
Parses the XML document in the $uri and returns an ezcFeed object with the type autodetected from the XML document. |
public static ezcFeed |
parseContent(
$content
)
Parses the XML document stored in $content and returns an ezcFeed object with the type autodetected from the XML document. |
public static void |
registerFeed(
$name
, $class
)
Adds the feed type $name to the supported list of feed types. |
public static void |
registerModule(
$name
, $class
, $namespacePrefix
)
Adds the module $name to the supported list of modules. |
public static void |
unregisterFeed(
$name
)
Removes a previously registered feed type from the list of supported feed types. |
public static void |
unregisterModule(
$name
)
Removes a previously registered module from the list of supported modules. |
public ezcFeed |
__construct(
[ $type
= null] )
Creates a new feed object. |
public ezcFeedElement|null |
add(
$name
)
Adds a new ezcFeedElement element with name $name and returns it. |
public ezcFeedModule |
addModule(
$name
)
Adds a new module to this item and returns it. |
public string |
generate(
[ $type
= null] )
Generates and returns an XML document of type $type from the current object. |
public string |
getContentType(
)
Returns the feed content type of this feed object (eg. 'application/rss+xml'). |
public string |
getFeedType(
)
Returns the feed type of this feed object (eg. 'rss2'). |
public ezcFeedModule |
getModule(
$name
)
Returns the loaded module $name. |
public array(ezcFeedModule) |
getModules(
)
Returns an array with all the modules loaded at feed-level. |
public bool |
hasModule(
$name
)
Returns true if the module $name is loaded, false otherwise. |
public void |
setModule(
$name
, $module
)
Associates the module $module with the name $name. |
Methods
getSupportedModules
Returns the supported feed modules.
The array returned is (default):
- array(
- 'Content' => 'ezcFeedContentModule',
- 'CreativeCommons' => 'ezcFeedCreativeCommonsModule',
- 'DublinCore' => 'ezcFeedDublinCoreModule',
- 'Geo' => 'ezcFeedGeoModule',
- 'GeoRss' => 'ezcFeedGeoRssModule',
- 'iTunes' => 'ezcFeedITunesModule'
- );
If the function registerModule was used to add another supported module type to ezcFeed, it will show up in the returned array as well.
getSupportedModulesPrefixes
Returns the supported feed modules prefixes.
The array returned is (default):
- array(
- 'content' => 'Content',
- 'creativeCommons' => 'CreativeCommons',
- 'dc' => 'DublinCore',
- 'geo' => 'Geo',
- 'georss' => 'GeoRss',
- 'itunes' => 'iTunes'
- );
If the function registerModule was used to add another supported module type to ezcFeed, it will show up in the returned array as well.
getSupportedTypes
Returns the supported feed types.
The array returned is (default):
- array(
- 'rss1' => 'ezcFeedRss1',
- 'rss2' => 'ezcFeedRss2',
- 'atom' => 'ezcFeedAtom'
- );
If the function registerFeed was used to add another supported feed type to ezcFeed, it will show up in the returned array as well.
parse
Parses the XML document in the $uri and returns an ezcFeed object with the type autodetected from the XML document.
Example of parsing an XML document stored at an URL:
Example of parsing an XML document protected with HTTP authentication:
If trying to parse an XML document protected with HTTP authentication without providing a valid username and password, the exception ezcFeedParseErrorException will be thrown.
Example of parsing an XML document stored in a local file:
Parameters:
Name | Type | Description |
---|---|---|
$uri |
string | An URI which stores an XML document |
Exceptions:
Type | Description |
---|---|
ezcBaseFileNotFoundException |
If the XML file at $uri could not be found. |
ezcFeedParseErrorException |
If the content at $uri is not a valid XML document. |
parseContent
Parses the XML document stored in $content and returns an ezcFeed object with the type autodetected from the XML document.
Example of parsing an XML document stored in a string:
- // $xmlString contains a valid XML string
Parameters:
Name | Type | Description |
---|---|---|
$content |
string | A string variable which stores an XML document |
Exceptions:
Type | Description |
---|---|
ezcFeedParseErrorException |
If $content is not a valid XML document. |
registerFeed
Adds the feed type $name to the supported list of feed types.
After registering a feed type, it can be used to create or parse feed documents.
Example of creating a feed with a user-defined type:
- // add properties for the Opml feed type to $feed
In the above example, myOpmlHandler extends ezcFeedProcessor and implements ezcFeedParser.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The feed type (eg. 'opml' ) |
$class |
string | The handler class for this feed type (eg. 'myOpmlHandler') |
registerModule
Adds the module $name to the supported list of modules.
After registering a module, it can be used to create or parse feed documents.
Example of creating a feed with a user-defined module:
- // add properties for the Slash module to $slash
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The module name (eg. 'Slash' ) |
$class |
string | The handler class for this module (eg. 'mySlashHandler') |
$namespacePrefix |
string | The XML namespace prefix for this module (eg. 'slash') |
unregisterFeed
Removes a previously registered feed type from the list of supported feed types.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The name of the feed type to remove (eg. 'opml') |
unregisterModule
Removes a previously registered module from the list of supported modules.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The name of the module to remove (eg. 'Slash') |
__construct
Creates a new feed object.
The $type value is used when calling generate() without specifying a feed type to output.
Parameters:
Name | Type | Description |
---|---|---|
$type |
string | The type of feed to create |
Exceptions:
Type | Description |
---|---|
ezcFeedUnsupportedTypeException |
if the feed type $type is not supported |
add
Adds a new ezcFeedElement element with name $name and returns it.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The element name |
Exceptions:
Type | Description |
---|---|
ezcFeedUnsupportedElementException |
if the element $name is not supported |
addModule
Adds a new module to this item and returns it.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The name of the module to add |
generate
Generates and returns an XML document of type $type from the current object.
If the type was defined when creating the ezcFeed object, then that type will be used if no type is specified when calling generate().
If no type was specified when calling the constructor and no type was specified when calling generate then an exception will be thrown.
Parameters:
Name | Type | Description |
---|---|---|
$type |
string | The feed type to generate |
Exceptions:
Type | Description |
---|---|
ezcFeedUnsupportedTypeException |
if the feed type $type is not supported |
getContentType
Returns the feed content type of this feed object (eg. 'application/rss+xml').
getFeedType
Returns the feed type of this feed object (eg. 'rss2').
getModule
Returns the loaded module $name.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The name of the module to return |
getModules
Returns an array with all the modules loaded at feed-level.
hasModule
Returns true if the module $name is loaded, false otherwise.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The name of the module to check if loaded for this item |
setModule
Associates the module $module with the name $name.
Parameters:
Name | Type | Description |
---|---|---|
$name |
string | The name of the module associate |
$module |
ezcFeedModule | The module to set under the name $name |