You are here

public static function FeedFactory::factory in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-feed/src/Writer/FeedFactory.php \Zend\Feed\Writer\FeedFactory::factory()

Create and return a Feed based on data provided.

Parameters

array|Traversable $data:

Return value

Feed

Throws

Exception\InvalidArgumentException

File

vendor/zendframework/zend-feed/src/Writer/FeedFactory.php, line 23

Class

FeedFactory

Namespace

Zend\Feed\Writer

Code

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) {

    // Setters
    $key = static::convertKey($key);
    $method = 'set' . $key;
    if (method_exists($feed, $method)) {
      switch ($method) {
        case 'setfeedlink':
          if (!is_array($value)) {

            // Need an array
            break;
          }
          if (!array_key_exists('link', $value) || !array_key_exists('type', $value)) {

            // Need both keys to set this correctly
            break;
          }
          $feed
            ->setFeedLink($value['link'], $value['type']);
          break;
        default:
          $feed
            ->{$method}($value);
          break;
      }
      continue;
    }

    // Entries
    if ('entries' == $key) {
      static::createEntries($value, $feed);
      continue;
    }
  }
  return $feed;
}