Beispiel #1
0
 public function output(Pagemill_Data $data, Pagemill_Stream $stream)
 {
     $oldPointer = self::$_pointer;
     $cycle = $data->parseVariables($this->getAttribute('cycle'));
     if ($cycle) {
         $cycle = explode(',', $data->parseVariables($this->getAttribute('cycle')));
     }
     $name = $data->parseVariables($this->getAttribute('name'));
     $times = $data->parseVariables($this->getAttribute('times'));
     $delimiter = $this->getAttribute('delimiter');
     $loopTimes = 0;
     // get as attribute
     $as = $this->getAttribute('as');
     $asKey = null;
     if (strpos($as, ' ') !== false) {
         list($asKey, $as) = explode(' ', $as);
         $as = trim($as);
         $asKey = trim($asKey);
         if (!$as) {
             $as = $asKey;
             $asKey = null;
         }
     }
     $this->_name = $name;
     $this->_originalData = $data->getArray();
     $this->_data = $data;
     $this->_stream = $stream;
     $this->_as = $as;
     $this->_asKey = $asKey;
     $this->_cycle = $cycle;
     $this->_delimiter = $delimiter;
     // if name given...
     if ($name) {
         $children = $data->evaluate($name);
         if (is_null($children) || is_scalar($children)) {
             return;
         }
         if (!is_a($children, 'Pagemill_Data') && (is_array($children) || $children instanceof Countable)) {
             if (count($children) == 0) {
                 return;
             }
         }
         self::$_pointer .= '[' . $name . ']';
         $data->set('pointer', self::$_pointer);
         if (!is_array($children)) {
             if (is_a($children, 'Pagemill_Data')) {
                 $children = $children->getArray();
             } else {
                 if ($children instanceof ArrayObject) {
                     $children = $children->getArrayCopy();
                 } else {
                     if (!$children instanceof Iterator) {
                         // Unrecognized objects throw an exception so developers
                         // can determine whether to modify the object or register
                         // a handler.
                         throw new Exception("Unable to loop over object '{$name}' of class '" . get_class($children) . "'");
                     }
                 }
             }
         }
         if ($this->hasAttribute('sort')) {
             $sort = $data->parseVariables($this->getAttribute('sort'));
             if (!is_array($children)) {
                 $array = array();
                 foreach ($children as $key => $value) {
                     $array[$key] = $value;
                 }
                 $children = $array;
                 // Replace the object with the array so we don't have to
                 // convert it again later
                 $data->set($name, $children);
             }
             $this->_sortKey = $sort;
             usort($children, array($this, '_cmp'));
         }
         $limit = $data->parseVariables($this->getAttribute('limit'));
         if ($limit) {
             // We have to do a numeric iteration.
             $start = null;
             $end = null;
             $parts = explode(',', $data->parseVariables($this->getAttribute('limit')));
             if (count($parts) == 2) {
                 $start = $parts[0];
                 $end = $parts[0] + $parts[1];
             } else {
                 $start = 0;
                 $end = $parts[0];
             }
             if (is_array($children) || $children instanceof Countable) {
                 if (count($children) < $end - $start) {
                     $end = count($children) - $start;
                 }
             }
             if (is_array($children) && isset($children[$start])) {
                 // for $start to $end
                 $loopTimes = $this->_forLimit($children, $start, $end);
             } else {
                 if ($children instanceof SeekableIterator) {
                     // seek to $start and do foreach
                     $loopTimes = $this->_forEachLimit($children, $start, $end);
                 } else {
                     if ($children instanceof ArrayAccess && isset($children[$start])) {
                         // for $start to $end
                         $loopTimes = $this->_forLimit($children, $start, $end);
                     } else {
                         // iterate to lower limit and proccess through upper limit
                         $loopTimes = $this->_forEachLimit($children, $start, $end);
                     }
                 }
             }
         } else {
             $loopTimes = $this->_forEach($children);
         }
         self::$_pointer = $oldPointer;
         $data->set('pointer', self::$_pointer);
     }
     if ($times) {
         $start = $loopTimes;
         $this->_forTimes($start, $times);
     }
 }