1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10: namespace FastFeed\Parser;
11:
12: use DOMElement;
13: use DateTime;
14: use FastFeed\Item;
15: use FastFeed\Exception\RuntimeException;
16:
17: 18: 19:
20: class extends AbstractParser implements ParserInterface
21: {
22:
23: 24: 25: 26: 27: 28: 29: 30:
31: public function getNodes($content)
32: {
33: $items = array();
34: $document = $this->createDocumentFromXML($content);
35: $nodes = $document->getElementsByTagName('item');
36: if ($nodes->length) {
37: foreach ($nodes as $node) {
38: try {
39: $item = $this->create($node);
40: $items[] = $item;
41: } catch (\Exception $e) {
42: throw new RuntimeException($e->getMessage());
43: }
44: }
45: }
46:
47: return $items;
48: }
49:
50: 51: 52: 53: 54:
55: public function create(DOMElement $node)
56: {
57: $item = new Item();
58: $this->setProperties($node, $item);
59: $this->setDate($node, $item);
60: $this->setTags($node, $item);
61: $this->executeAggregators($node, $item);
62:
63: return $item;
64: }
65:
66: 67: 68:
69: protected function getPropertiesMapping()
70: {
71: return array(
72: 'setId' => 'link',
73: 'setName' => 'title',
74: 'setIntro' => 'description',
75: 'setContent' => 'description',
76: 'setSource' => 'link',
77: 'setAuthor' => 'author'
78: );
79: }
80:
81: 82: 83: 84:
85: protected function setDate(DOMElement $node, Item $item)
86: {
87: $value = $this->getNodeValueByTagName($node, 'pubDate');
88: if ($value) {
89: if (strtotime($value)) {
90: $item->setDate(new DateTime($value));
91: }
92: }
93: }
94:
95: 96: 97: 98:
99: protected function setTags(DOMElement $node, Item $item)
100: {
101: $tags = $this->getNodeValuesByTagName($node, 'category');
102: foreach ($tags as $tag) {
103: $item->addTag($tag);
104: }
105: }
106: }
107: