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