From 2b33701645c6e372df7a162340a7bd67247d1d8f Mon Sep 17 00:00:00 2001 From: James Date: Sun, 26 Jul 2015 15:24:14 +0100 Subject: [PATCH] Issue #1 - another example URL --- .../HTMLIsAnEvent/Parser.php | 42 ++- .../HTMLIsAnEvent/BothTest.php | 68 ++++ .../HTMLIsAnEvent/data/both1.html | 322 ++++++++++++++++++ 3 files changed, 419 insertions(+), 13 deletions(-) create mode 100644 tests/JMBTechnologyLimited/HTMLIsAnEvent/BothTest.php create mode 100644 tests/JMBTechnologyLimited/HTMLIsAnEvent/data/both1.html diff --git a/src/JMBTechnologyLimited/HTMLIsAnEvent/Parser.php b/src/JMBTechnologyLimited/HTMLIsAnEvent/Parser.php index 0d3106e..8702934 100644 --- a/src/JMBTechnologyLimited/HTMLIsAnEvent/Parser.php +++ b/src/JMBTechnologyLimited/HTMLIsAnEvent/Parser.php @@ -33,15 +33,16 @@ function __construct($html, $url) $event = new Event(); + //$places = $node->find('[itemtype="http://schema.org/Place"]'); $nameMetas = $node->find('meta[itemprop="name"]'); - if ($nameMetas) { + if ($nameMetas->count() > 0) { $event->setTitle(html_entity_decode($nameMetas->getAttribute("content"))); - } - - $nameContents = $node->find('div[itemprop="name"]'); - if ($nameContents->count() > 0) { - $event->setTitle(html_entity_decode($nameContents[0]->text(true))); + } else { + $nameContents = $node->find('[itemprop="name"]'); + if ($nameContents->count() > 0) { + $event->setTitle(html_entity_decode($nameContents[0]->text(true))); + } } $urlContents = $node->find('a[itemprop="url"]'); @@ -51,15 +52,24 @@ function __construct($html, $url) } } - $startContents = $node->find('time[itemprop="startDate"]'); - if ($startContents->count() > 0) { - $event->setStart(new \DateTime($startContents[0]->getAttribute("datetime"), new \DateTimeZone("UTC"))); + $startMetas = $node->find('meta[itemprop="startDate"]'); + if ($startMetas->count() > 0) { + $event->setStart(new \DateTime($startMetas[0]->getAttribute("content"), new \DateTimeZone("UTC"))); + } else { + $startContents = $node->find('time[itemprop="startDate"]'); + if ($startContents->count() > 0) { + $event->setStart(new \DateTime($startContents[0]->getAttribute("datetime"), new \DateTimeZone("UTC"))); + } } - - $endContents = $node->find('time[itemprop="endDate"]'); - if ($endContents->count() > 0) { - $event->setEnd(new \DateTime($endContents[0]->getAttribute("datetime"), new \DateTimeZone("UTC"))); + $endMetas = $node->find('meta[itemprop="endDate"]'); + if ($endMetas->count() > 0) { + $event->setEnd(new \DateTime($startMetas[0]->getAttribute("content"), new \DateTimeZone("UTC"))); + } else { + $endContents = $node->find('time[itemprop="endDate"]'); + if ($endContents->count() > 0) { + $event->setEnd(new \DateTime($endContents[0]->getAttribute("datetime"), new \DateTimeZone("UTC"))); + } } $descriptionContents = $node->find('p[itemprop="description"]'); @@ -78,6 +88,12 @@ function __construct($html, $url) $event = new Event(); + $locations = $node->find(".p-location"); + foreach($locations as $location) { + $location->getParent()->removeChild($location->id()); + } + + $nameContents = $node->find('.p-name'); if ($nameContents->count() > 0) { $event->setTitle(html_entity_decode($nameContents[0]->text(true))); diff --git a/tests/JMBTechnologyLimited/HTMLIsAnEvent/BothTest.php b/tests/JMBTechnologyLimited/HTMLIsAnEvent/BothTest.php new file mode 100644 index 0000000..18f3eef --- /dev/null +++ b/tests/JMBTechnologyLimited/HTMLIsAnEvent/BothTest.php @@ -0,0 +1,68 @@ + + */ +class BothTest extends \PHPUnit_Framework_TestCase { + + function testFile1() { + + $parser = new Parser(file_get_contents(__DIR__.DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR."both1.html"), "http://example.com"); + + $events = $parser->getEvents(); + + $this->assertEquals(2, count($events)); + + + ############################### Event + + $event1 = $events[0]; + + + $this->assertEquals("IndieWebCamp 2015",$event1->getTitle()); + $this->assertEquals(1, $event1->getUrlsCount()); + $this->assertEquals("http://indiewebcamp.com/2015",$event1->getUrls()[0]->getUrl()); + + + $this->assertNotNull($event1->getStart()); + $this->assertEquals("2015-07-11T16:30:00+00:00",$event1->getStart()->format("c")); + $this->assertEquals("+00:00",$event1->getStart()->getTimezone()->getName()); + + $this->assertNotNull($event1->getEnd()); + $this->assertEquals("2015-07-11T16:30:00+00:00",$event1->getEnd()->format("c")); + $this->assertEquals("+00:00",$event1->getEnd()->getTimezone()->getName()); + + $this->assertNull($event1->getDescriptionHtml()); + $this->assertNull($event1->getDescriptionText()); + + ############################### Event + + $event2 = $events[1]; + + $this->assertEquals("IndieWebCamp 2015",$event2->getTitle()); + $this->assertEquals(1, $event2->getUrlsCount()); + $this->assertEquals("http://indiewebcamp.com/2015",$event2->getUrls()[0]->getUrl()); + + + $this->assertNotNull($event2->getStart()); + $this->assertEquals("2015-07-11T09:30:00+00:00",$event2->getStart()->format("c")); + $this->assertEquals("UTC",$event2->getStart()->getTimezone()->getName()); + + $this->assertNotNull($event2->getEnd()); + $this->assertEquals("2015-07-12T18:00:00+00:00",$event2->getEnd()->format("c")); + $this->assertEquals("UTC",$event2->getEnd()->getTimezone()->getName()); + + $this->assertNull($event2->getDescriptionHtml()); + $this->assertNull($event2->getDescriptionText()); + } + +} + diff --git a/tests/JMBTechnologyLimited/HTMLIsAnEvent/data/both1.html b/tests/JMBTechnologyLimited/HTMLIsAnEvent/data/both1.html new file mode 100644 index 0000000..9e69f4b --- /dev/null +++ b/tests/JMBTechnologyLimited/HTMLIsAnEvent/data/both1.html @@ -0,0 +1,322 @@ + + + + + + + + + + + + + + IndieWebCamp 2015 » Calagator: Portland's Tech Calendar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ + + + + + + + +Export or edit this event... + + + +
+
+
+

IndieWebCamp 2015

+ + +
through
+
+
+ + Esri Portland R&D Center + + +
+
309 SW 6th Ave, Ste. 600
+ Portland, + OR + 97204, + US + (map) + + + + +
+
+ +

Website

+ + +

Description

+
+

Join us at Esri's R&D Center office in downtown Portland for two days of a BarCamp-style gathering of web creators building and sharing open web technologies to empower users to own their own identities & content, and advance the state of the indie web!

+ +

You’ll learn about ways to empower yourself to own your data, create and publish content on your own site, and only optionally syndicate to third-party silos. Along the way you’ll get a solid grounding in the history and future of Microformats, domain ownership, IndieAuth, WebMention and more.

+ +

The IndieWeb movement is a global community that is building an open set of principles and methods that empower people to take back ownership of identity and data instead of sharecropping on 3rd party websites. Homestead, don’t sharecrop! You’ll learn about ways to empower yourself to own your data, create and publish content on your own site, and only optionally syndicate to third-party silos. Along the way you’ll get a solid grounding in the history and future of Microformats, domain ownership, IndieAuth, WebMention and more!

+ +

Register for your ticket here! https://ti.to/indiewebcamp/2015-portland

+
+
+ +
+

Share

+ +

Tags

+ +
+
+ + +
+ + + + + +
+ + + + + + + +