Skip to content

Commit

Permalink
Improve hour parser algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
mklkj committed Apr 3, 2018
1 parent 4cbcbf2 commit d782f59
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 40 deletions.
52 changes: 36 additions & 16 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,8 @@ private function getHourWithLessons(NodeList $rowCells, int $index): array

/** @var Element $current */
$current = $rowCells->get($index);
$spans = $current->find('span[style]');

foreach ($spans as $group) {
$hour['lessons'][] = array_merge($this->getLesson($group), ['diversion' => true]);
}

$subject = $current->findXPath('./*[@class="p"]');

if ($current->findXPath('./br')->count() && $spans->count() === 0 && $subject->count() > 1) {
foreach (explode('<br>', $current->getOuterHtml()) as $item) {
$doc = new Document();
$doc->html(str_replace('<td class="l">', '', $item));
$hour['lessons'][] = $this->getLesson($doc->find('body')->first());
}
} elseif ($subject->count() > 0) {
$hour['lessons'][] = $this->getLesson($current);
}
$hour['lessons'] = $this->getExtractedLessons($current);

$className = $current->findXPath('./*[@class="o"]');
if ($className->count() > 1) {
Expand Down Expand Up @@ -129,6 +114,41 @@ private function getHourWithLessons(NodeList $rowCells, int $index): array
return $hour;
}

private function getExtractedLessons(Element $current): array
{
$lessons = [];

$chunks = explode('<br>', $current->getOuterHtml());
$spans = $current->find('span[style]');
$subject = $current->findXPath('./*[@class="p"]');

if ($spans->count() === 0 && $subject->count() > 0 & \count($chunks) === 0) { // simple one lesson in hour without division
$lessons[] = $this->getLesson($current);
} elseif ($spans->count() > 0 && $subject->count() === 0) { // simply two or more groups with division
foreach ($spans as $group) {
$lessons[] = array_merge($this->getLesson($group), ['diversion' => true]);
}
} elseif ($subject->count() > 0 && \count($chunks) > 0) {
foreach ($chunks as $item) {
$doc = new Document();
$doc->html($item);

$span = $doc->find('span[style]');
$cell = $doc->find('.l');
$body = $doc->find('body');
if ($span->count() > 0) {
$lessons[] = array_merge($this->getLesson($span->first()), ['diversion' => true]);
} elseif ($cell->count() > 0) {
$lessons[] = $this->getLesson($cell->first());
} elseif ($body->count() > 0) {
$lessons[] = $this->getLesson($body->first());
}
}
}

return $lessons;
}

private function getLesson(Element $cell): array
{
$teacher = $cell->findXPath('./*[@class="n"]');
Expand Down
29 changes: 17 additions & 12 deletions tests/EqualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wulkanowy\Tests\TimetableParser;

use DOMWrap\Document;
use Mihaeu\HtmlFormatter;
use PHPUnit\Framework\TestCase;
use Wulkanowy\TimetableParser\Table;
Expand All @@ -10,25 +11,23 @@ class EqualsTest extends TestCase
{
public function testClass(): void
{
$expected = file_get_contents(__DIR__.'/fixtures/oddzial.html');
$actual = $this->getGeneratedHTML(__DIR__.'/fixtures/oddzial.html');
$remote = file_get_contents(__DIR__.'/fixtures/oddzial.html');
$generated = $this->getGeneratedHTML($remote);

$this->assertEquals($this->format($expected), $this->format($actual));
$this->assertEquals($this->format($remote), $this->format($generated));
}

public function testRoom(): void
{
$expected = file_get_contents(__DIR__.'/fixtures/sala.html');
$actual = $this->getGeneratedHTML(__DIR__.'/fixtures/sala.html');
$remote = file_get_contents(__DIR__.'/fixtures/sala.html');
$actual = $this->getGeneratedHTML($remote);

$this->assertEquals($this->format($expected), $this->format($actual));
$this->assertEquals($this->format($remote), $this->format($actual));
}

private function getGeneratedHTML(string $filename)
private function getGeneratedHTML(string $html)
{
$table = (new Table(
file_get_contents($filename)
))->getTable();
$table = (new Table($html))->getTable();

ob_start();
require __DIR__.'/template.html.php';
Expand All @@ -43,8 +42,14 @@ private function format($html): string
$dom = new \DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadHTML($html);
@$dom->loadHTML($html);

return str_replace([PHP_EOL.' '.PHP_EOL], PHP_EOL, HtmlFormatter::format($dom->saveHTML(), ''));
$html = str_replace([PHP_EOL.' '.PHP_EOL], PHP_EOL, HtmlFormatter::format($dom->saveHTML(), ''));

$doc = new Document();
$doc->html($html);
$doc->find('.tabela');

return $doc->find('.tabela')->first()->getHtml();
}
}
27 changes: 15 additions & 12 deletions tests/template.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<?php if ($lesson['diversion']): ?><span style="font-size:85%"><?php endif; ?>

<?php if ($lesson['teacher']['value'] && empty($lesson['room']['value'])): ?>
<?php if ($lesson['teacher']['value'] && empty($lesson['room']['name'])): ?>
<a href="n<?= $lesson['teacher']['value']; ?>.html" class="n"><?= $lesson['teacher']['name']; ?></a>
<?php endif; ?>

Expand All @@ -62,7 +62,7 @@
<span class="p"><?=trim($subject[1]); ?></span>
<?php elseif ($lesson['subject']): ?>
<span class="p"><?=$lesson['subject']; ?></span>
<?php if (empty($lesson['room']['value'])): ?>
<?php if (empty($lesson['room']['name'])): ?>
<br>
<?php endif; ?>
<?php endif; ?>
Expand All @@ -71,10 +71,19 @@
<a href="n<?= $lesson['teacher']['value']; ?>.html" class="n"><?= $lesson['teacher']['name']; ?></a>
<?php endif; ?>

<?php if ($lesson['room']['value']): ?>
<a href="s<?=$lesson['room']['value']; ?>.html" class="s"><?= $lesson['room']['name']; ?></a>
<?php if ($lesson['room']['name']): ?>
<?php if ($lesson['room']['value']): ?>
<a href="s<?=$lesson['room']['value']; ?>.html" class="s"><?= $lesson['room']['name']; ?></a>
<?php else: ?>
<span class="s"><?=$lesson['room']['name']; ?></span>
<?php endif; ?>
<?php endif; ?>

<?php if (is_array($lesson['className']) &&
$table['typeName'] === 'nauczyciela' &&
$lesson === end($lessons)
): ?><br><?php endif; ?>

<?php if ($lesson['diversion']): ?>
</span>
<?php endif; ?>
Expand All @@ -93,21 +102,15 @@
</td>
</tr>
<tr>
<td align="left">
<?=$table['description']; ?>
</td></tr>
<td align="left"><?=$table['description']; ?></td></tr>
<tr>
<td align="left">
<a href="javascript:window.print()">Drukuj plan</a>
</td>
<td class="op" align="right">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="right">
wygenerowano <?=$table['generated']; ?><br>
za pomocą programu
<a href="http://www.vulcan.edu.pl/dla_szkol/optivum/plan_lekcji/Strony/wstep.aspx" target="_blank">Plan lekcji Optivum</a><br>
firmy <a href="http://www.vulcan.edu.pl/" target="_blank">VULCAN</a></td>
<td align="right">wygenerowano <?=$table['generated']; ?><br>za pomocą programu<a href="http://www.vulcan.edu.pl/dla_szkol/optivum/plan_lekcji/Strony/wstep.aspx" target="_blank">Plan lekcji Optivum</a><br>firmy <a href="http://www.vulcan.edu.pl/" target="_blank">VULCAN</a></td>
<td>
<img border="0" src="../images/plan_logo.gif" style="margin-left:10" alt="logo programu Plan lekcji Optivum" width="40" height="40">
</td>
Expand Down

0 comments on commit d782f59

Please sign in to comment.