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 AtomParser 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('entry');
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->setLink($node, $item);
60: $this->setAuthor($node, $item);
61: $this->setDate($node, $item);
62: $this->setTags($node, $item);
63: $this->executeAggregators($node, $item);
64:
65: return $item;
66: }
67:
68: 69: 70:
71: protected function getPropertiesMapping()
72: {
73: return array(
74: 'setId' => 'id',
75: 'setName' => 'title',
76: 'setIntro' => 'content',
77: 'setContent' => 'content',
78: );
79: }
80:
81: 82: 83: 84:
85: protected function setLink(DOMElement $node, Item $item)
86: {
87: $nodeList = $node->getElementsByTagName('link');
88: if ($nodeList->length) {
89: foreach ($nodeList as $nodeResult) {
90: if ($nodeResult->getAttribute('type') != 'text/html') {
91: continue;
92: }
93: $item->setSource($nodeResult->getAttribute('href'));
94:
95: break;
96: }
97: }
98: }
99:
100: 101: 102: 103:
104: protected function setAuthor(DOMElement $node, Item $item)
105: {
106: $nodeList = $node->getElementsByTagName('author');
107: if ($nodeList->length) {
108: foreach ($nodeList->item(0)->childNodes as $nodeResult) {
109: if ($nodeResult->nodeName != 'email') {
110: continue;
111: }
112: $item->setAuthor($nodeResult->nodeValue);
113:
114: break;
115: }
116: }
117: }
118:
119: 120: 121: 122:
123: protected function setDate(DOMElement $node, Item $item)
124: {
125: $value = $this->getNodeValueByTagName($node, 'published');
126: if ($value) {
127: if (strtotime($value)) {
128: $item->setDate(new DateTime($value));
129: }
130: }
131: }
132:
133: 134: 135: 136:
137: protected function setTags(DOMElement $node, Item $item)
138: {
139: $tags = $this->getNodePropertyByTagName($node, 'category', 'term');
140: foreach ($tags as $tag) {
141: $item->addTag($tag);
142: }
143: }
144: }
145: