FeedFactory.php in Zircon Profile 8
File
vendor/zendframework/zend-feed/src/Writer/FeedFactory.php
View source
<?php
namespace Zend\Feed\Writer;
use Traversable;
abstract class FeedFactory {
public static function factory($data) {
if (!is_array($data) && !$data instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf('%s expects an array or Traversable argument; received "%s"', __METHOD__, is_object($data) ? get_class($data) : gettype($data)));
}
$feed = new Feed();
foreach ($data as $key => $value) {
$key = static::convertKey($key);
$method = 'set' . $key;
if (method_exists($feed, $method)) {
switch ($method) {
case 'setfeedlink':
if (!is_array($value)) {
break;
}
if (!array_key_exists('link', $value) || !array_key_exists('type', $value)) {
break;
}
$feed
->setFeedLink($value['link'], $value['type']);
break;
default:
$feed
->{$method}($value);
break;
}
continue;
}
if ('entries' == $key) {
static::createEntries($value, $feed);
continue;
}
}
return $feed;
}
protected static function convertKey($key) {
$key = str_replace('_', '', strtolower($key));
return $key;
}
protected static function createEntries($entries, Feed $feed) {
if (!is_array($entries) && !$entries instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf('%s::factory expects the "entries" value to be an array or Traversable; received "%s"', get_called_class(), is_object($entries) ? get_class($entries) : gettype($entries)));
}
foreach ($entries as $data) {
if (!is_array($data) && !$data instanceof Traversable && !$data instanceof Entry) {
throw new Exception\InvalidArgumentException(sprintf('%s expects an array, Traversable, or Zend\\Feed\\Writer\\Entry argument; received "%s"', __METHOD__, is_object($data) ? get_class($data) : gettype($data)));
}
if ($data instanceof Entry) {
$feed
->addEntry($data);
continue;
}
$entry = $feed
->createEntry();
foreach ($data as $key => $value) {
$key = static::convertKey($key);
$method = 'set' . $key;
if (!method_exists($entry, $method)) {
continue;
}
$entry
->{$method}($value);
}
$feed
->addEntry($entry);
}
}
}