1
\$\begingroup\$

I just want some advice regarding code style and best practices. Also I want to know if I'm using constant declaration and closures properly.

This code is used to get the data from here http://consulta.siiau.udg.mx/wco/sspseca.forma_consulta

which is the schedules for classes on my ex-school.

<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\DomCrawler\Crawler;
use Goutte\Client;

class Siiau {
  const URL = 'http://consulta.siiau.udg.mx/wco/sspseca.forma_consulta';
  const SCHEDULE_KEYS = ['time', 'days', 'building', 'classroom'];
  const TIME_KEYS = ['start', 'finish'];
  const KEYS = ['nrc', 'key', 'topic', 'section', 'credits', 
                'limit', 'available', 'schedule', 'professor'];
  const ENTRY_OFFSET = 3;
  const SCHEDULE_OFFSET = 4;

  private $client;
  public $entries;

  public function __construct() {
    $this->client = new Client();
    $this->entries = [];
  }

  public function parse()
  {
    $submitText = 'Consultar';
    $selector = 'body > style + table > tr';
    $form = $this
      ->client
      ->request('POST', self::URL)
      ->selectButton($submitText)->form();
    $crawler = $this->client->submit($form);
    $entries = $crawler
      ->filter($selector)
      ->slice(self::ENTRY_OFFSET);
    $this->entries = $entries->each(function(Crawler $entry, $i)
    {
      $values = $entry
        ->children()
        ->slice(1)
        ->each($this->parseEntry());
      return array_combine(self::KEYS, $values);
    });
  }

  private function parseEntry()
    {
    return function(Crawler $value, $i)
    {
      if (self::KEYS[$i] == 'schedule') {
        $value = $this->parseSchedule($value);
      } elseif (self::KEYS[$i] == 'professor') {
        $value = $this->parseProfessor($value);
        $value = preg_replace('/(\s)+/', ' ', $value->text());
      } else {
        $value = preg_replace('/(\s)+/', ' ', $value->text());
      }
      return $value;
    };
  }

  private function parseSchedule($schedules)
  {
    $selector = 'table > tr';
    $schedules = $schedules
      ->filter($selector)
      ->each(function(Crawler $row, $i) {
        $row = $row
          ->children()
          ->slice(1, self::SCHEDULE_OFFSET)
          ->each(function(Crawler $cell, $i) {
            $value = $cell->text();
            if (self::SCHEDULE_KEYS[$i] == 'days') {
              $value = str_split(preg_replace('/([\.\s])+/', '', $value));

            } elseif (self::SCHEDULE_KEYS[$i] == 'time') {
              $value = explode('-', $value);
              $value = array_combine(self::TIME_KEYS, $value);
            }
            return $value;
          });
        return array_combine(self::SCHEDULE_KEYS, $row);
      });
    return $schedules;
  }
  private function parseProfessor($professor)
  {
    $selector = 'td:last-child';
    return $professor->filter($selector);
  }
}

$siiau = new Siiau();
$siiau->parse();
var_dump($siiau->entries);
?>
\$\endgroup\$
  • \$\begingroup\$ this code is totally dependant of the HTML structure who display the result. Your code would have been more reliable if you could have read the data of these schedules. \$\endgroup\$ – mmm Aug 3 '15 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.