PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Attribute Collections

In an attempt to simplify return types, with Zend Framework 1.10 return types from the various feed and entry level methods may include an object of type Zend_Feed_Reader_Collection_CollectionAbstract. Despite the special class name which I'll explain below, this is just a simple subclass of SPL's ArrayObject.

The main purpose here is to allow the presentation of as much data as possible from the requested elements, while still allowing access to the most relevant data as a simple array. This also enforces a standard approach to returning such data which previously may have wandered between arrays and objects.

The new class type acts identically to ArrayObject with the sole addition being a new method getValues() which returns a simple flat array containing the most relevant information.

A simple example of this is Zend_Feed_Reader_FeedInterface::getCategories(). When used with any RSS or Atom feed, this method will return category data as a container object called Zend_Feed_Reader_Collection_Category. The container object will contain, per category, three fields of data: term, scheme and label. The "term" is the basic category name, often machine readable (i.e. plays nice with URIs). The scheme represents a categorisation scheme (usually a URI identifier) also known as a "domain" in RSS 2.0. The "label" is a human readable category name which supports HTML entities. In RSS 2.0, there is no label attribute so it is always set to the same value as the term for convenience.

To access category labels by themselves in a simple value array, you might commit to something like:

<?php
$feed 
Zend_Feed_Reader::import('http://www.example.com/atom.xml');
$categories $feed->getCategories();
$labels = array();
foreach (
$categories as $cat) {
    
$labels[] = $cat['label']
}

It's a contrived example, but the point is that the labels are tied up with other information.

However, the container class allows you to access the "most relevant" data as a simple array using the getValues() method. The concept of "most relevant" is obviously a judgement call. For categories it means the category labels (not the terms or schemes) while for authors it would be the authors' names (not their email addresses or URIs). The simple array is flat (just values) and passed through array_unique() to remove duplication.

<?php
$feed 
Zend_Feed_Reader::import('http://www.example.com/atom.xml');
$categories $feed->getCategories();
$labels $categories->getValues();

The above example shows how to extract only labels and nothing else thus giving simple access to the category labels without any additional work to extract that data by itself.

Zend Framework