Skip to content

Commit

Permalink
Issue #1 - example URLs
Browse files Browse the repository at this point in the history
Issue #2 - event in both formats
  • Loading branch information
jarofgreen committed Jul 26, 2015
1 parent 2b33701 commit aed0fd8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 54 deletions.
75 changes: 41 additions & 34 deletions src/JMBTechnologyLimited/HTMLIsAnEvent/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ function __construct($html, $url)
$dom = new Dom();
$dom->load($html, array( 'strict' => false ));

$idsOfRootElements = array();

foreach($dom->find('[itemtype="http://schema.org/Event"]') as $node) {

$idsOfRootElements[] = $node->id();

$event = new Event();

//$places = $node->find('[itemtype="http://schema.org/Place"]');
Expand Down Expand Up @@ -86,53 +90,56 @@ function __construct($html, $url)

foreach($dom->find('.h-event') as $node) {

$event = new Event();
if (!in_array($node->id(), $idsOfRootElements)) {

$locations = $node->find(".p-location");
foreach($locations as $location) {
$location->getParent()->removeChild($location->id());
}
$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)));
}

$urlContents = $node->find('.u-url a, a.u-url');
if ($urlContents->count() > 0) {
foreach($urlContents as $urlContent) {
$event->addUrl(new URL(html_entity_decode($urlContent->getAttribute("href"))));
$nameContents = $node->find('.p-name');
if ($nameContents->count() > 0) {
$event->setTitle(html_entity_decode($nameContents[0]->text(true)));
}
}

$startContents = $node->find('time.dt-start');
if ($startContents->count() > 0) {
if ($startContents[0]->getAttribute("datetime")) {
$event->setStart(new \DateTime($startContents[0]->getAttribute("datetime"), new \DateTimeZone("UTC")));
} else if ($startContents[0] instanceof Dom\HtmlNode && $startContents[0]->text(true)) {
$event->setStart(new \DateTime($startContents[0]->text(true), new \DateTimeZone("UTC")));
$urlContents = $node->find('.u-url a, a.u-url');
if ($urlContents->count() > 0) {
foreach($urlContents as $urlContent) {
$event->addUrl(new URL(html_entity_decode($urlContent->getAttribute("href"))));
}
}
}

$endContents = $node->find('time.dt-end');
if ($endContents->count() > 0) {
if ($endContents[0]->getAttribute("datetime")) {
$event->setEnd(new \DateTime($endContents[0]->getAttribute("datetime"), new \DateTimeZone("UTC")));
} else if ($endContents[0] instanceof Dom\HtmlNode && $endContents[0]->text(true)) {
$event->setEnd(new \DateTime($endContents[0]->text(true), new \DateTimeZone("UTC")));
$startContents = $node->find('time.dt-start');
if ($startContents->count() > 0) {
if ($startContents[0]->getAttribute("datetime")) {
$event->setStart(new \DateTime($startContents[0]->getAttribute("datetime"), new \DateTimeZone("UTC")));
} else if ($startContents[0] instanceof Dom\HtmlNode && $startContents[0]->text(true)) {
$event->setStart(new \DateTime($startContents[0]->text(true), new \DateTimeZone("UTC")));
}
}
}

$descriptionContents = $node->find('.p-description');
if ($descriptionContents->count() > 0) {
$event->setDescriptionText(html_entity_decode($descriptionContents[0]->text(true)));
$event->setDescriptionHTML(html_entity_decode($descriptionContents[0]->innerHtml()));
}
$endContents = $node->find('time.dt-end');
if ($endContents->count() > 0) {
if ($endContents[0]->getAttribute("datetime")) {
$event->setEnd(new \DateTime($endContents[0]->getAttribute("datetime"), new \DateTimeZone("UTC")));
} else if ($endContents[0] instanceof Dom\HtmlNode && $endContents[0]->text(true)) {
$event->setEnd(new \DateTime($endContents[0]->text(true), new \DateTimeZone("UTC")));
}
}

$descriptionContents = $node->find('.p-description');
if ($descriptionContents->count() > 0) {
$event->setDescriptionText(html_entity_decode($descriptionContents[0]->text(true)));
$event->setDescriptionHTML(html_entity_decode($descriptionContents[0]->innerHtml()));
}


$this->events[] = $event;
$this->events[] = $event;

}
}


Expand Down
21 changes: 1 addition & 20 deletions tests/JMBTechnologyLimited/HTMLIsAnEvent/BothTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function testFile1() {

$events = $parser->getEvents();

$this->assertEquals(2, count($events));
$this->assertEquals(1, count($events));


############################### Event
Expand All @@ -43,25 +43,6 @@ function testFile1() {
$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());
}

}
Expand Down

0 comments on commit aed0fd8

Please sign in to comment.