Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license http://framework.zend.com/license/new-bsd New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Feed\Writer;
|
Chris@0
|
11
|
Chris@0
|
12 use Traversable;
|
Chris@0
|
13
|
Chris@0
|
14 abstract class FeedFactory
|
Chris@0
|
15 {
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Create and return a Feed based on data provided.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @param array|Traversable $data
|
Chris@0
|
20 * @throws Exception\InvalidArgumentException
|
Chris@0
|
21 * @return Feed
|
Chris@0
|
22 */
|
Chris@0
|
23 public static function factory($data)
|
Chris@0
|
24 {
|
Chris@12
|
25 if (! is_array($data) && ! $data instanceof Traversable) {
|
Chris@0
|
26 throw new Exception\InvalidArgumentException(sprintf(
|
Chris@0
|
27 '%s expects an array or Traversable argument; received "%s"',
|
Chris@0
|
28 __METHOD__,
|
Chris@0
|
29 (is_object($data) ? get_class($data) : gettype($data))
|
Chris@0
|
30 ));
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 $feed = new Feed();
|
Chris@0
|
34
|
Chris@0
|
35 foreach ($data as $key => $value) {
|
Chris@0
|
36 // Setters
|
Chris@0
|
37 $key = static::convertKey($key);
|
Chris@0
|
38 $method = 'set' . $key;
|
Chris@0
|
39 if (method_exists($feed, $method)) {
|
Chris@0
|
40 switch ($method) {
|
Chris@0
|
41 case 'setfeedlink':
|
Chris@12
|
42 if (! is_array($value)) {
|
Chris@0
|
43 // Need an array
|
Chris@0
|
44 break;
|
Chris@0
|
45 }
|
Chris@12
|
46 if (! array_key_exists('link', $value) || ! array_key_exists('type', $value)) {
|
Chris@0
|
47 // Need both keys to set this correctly
|
Chris@0
|
48 break;
|
Chris@0
|
49 }
|
Chris@0
|
50 $feed->setFeedLink($value['link'], $value['type']);
|
Chris@0
|
51 break;
|
Chris@0
|
52 default:
|
Chris@0
|
53 $feed->$method($value);
|
Chris@0
|
54 break;
|
Chris@0
|
55 }
|
Chris@0
|
56 continue;
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 // Entries
|
Chris@0
|
60 if ('entries' == $key) {
|
Chris@0
|
61 static::createEntries($value, $feed);
|
Chris@0
|
62 continue;
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 return $feed;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Normalize a key
|
Chris@0
|
71 *
|
Chris@0
|
72 * @param string $key
|
Chris@0
|
73 * @return string
|
Chris@0
|
74 */
|
Chris@0
|
75 protected static function convertKey($key)
|
Chris@0
|
76 {
|
Chris@0
|
77 $key = str_replace('_', '', strtolower($key));
|
Chris@0
|
78 return $key;
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Create and attach entries to a feed
|
Chris@0
|
83 *
|
Chris@0
|
84 * @param array|Traversable $entries
|
Chris@0
|
85 * @param Feed $feed
|
Chris@0
|
86 * @throws Exception\InvalidArgumentException
|
Chris@0
|
87 * @return void
|
Chris@0
|
88 */
|
Chris@0
|
89 protected static function createEntries($entries, Feed $feed)
|
Chris@0
|
90 {
|
Chris@12
|
91 if (! is_array($entries) && ! $entries instanceof Traversable) {
|
Chris@0
|
92 throw new Exception\InvalidArgumentException(sprintf(
|
Chris@0
|
93 '%s::factory expects the "entries" value to be an array or Traversable; received "%s"',
|
Chris@0
|
94 get_called_class(),
|
Chris@0
|
95 (is_object($entries) ? get_class($entries) : gettype($entries))
|
Chris@0
|
96 ));
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 foreach ($entries as $data) {
|
Chris@12
|
100 if (! is_array($data) && ! $data instanceof Traversable && ! $data instanceof Entry) {
|
Chris@0
|
101 throw new Exception\InvalidArgumentException(sprintf(
|
Chris@0
|
102 '%s expects an array, Traversable, or Zend\Feed\Writer\Entry argument; received "%s"',
|
Chris@0
|
103 __METHOD__,
|
Chris@0
|
104 (is_object($data) ? get_class($data) : gettype($data))
|
Chris@0
|
105 ));
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 // Use case 1: Entry item
|
Chris@0
|
109 if ($data instanceof Entry) {
|
Chris@0
|
110 $feed->addEntry($data);
|
Chris@0
|
111 continue;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 // Use case 2: iterate item and populate entry
|
Chris@0
|
115 $entry = $feed->createEntry();
|
Chris@0
|
116 foreach ($data as $key => $value) {
|
Chris@0
|
117 $key = static::convertKey($key);
|
Chris@0
|
118 $method = 'set' . $key;
|
Chris@12
|
119 if (! method_exists($entry, $method)) {
|
Chris@0
|
120 continue;
|
Chris@0
|
121 }
|
Chris@0
|
122 $entry->$method($value);
|
Chris@0
|
123 }
|
Chris@0
|
124 $feed->addEntry($entry);
|
Chris@0
|
125 }
|
Chris@0
|
126 }
|
Chris@0
|
127 }
|