Parser.php
3c27a3e7
 <?php
 /**
  * Created by IntelliJ IDEA.
  * User: nosko
  * Date: 1/26/13
  * Time: 10:57 PM
  * To change this template use File | Settings | File Templates.
  */
 class Parser {
 
     private $restaurants = array();
     private $logger;
 
17fef192
     function __construct(iOutput $logger, $restaurants) {
3c27a3e7
         $this->logger = $logger;
 
         if (empty($restaurants) || !is_array($restaurants)) {
             throw new InvalidArgumentException("Array argument required");
         }
 
         $this->restaurants = $restaurants;
     }
 
     public function parse() {
         foreach ($this->restaurants as $restaurant) {
e421c421
 	        /** @var $restaurant Restaurant */
3c27a3e7
 
             $source = file_get_contents($restaurant->getUrl());
 
             if ($source === false) {
e421c421
 	            continue;
3c27a3e7
             }
 
             $dom = new DOMDocument();
             @$dom->loadHTML($source);
             $xpath_obj = new DOMXPath($dom);
 
e421c421
 	        $this->logger->addHeader($restaurant);
3c27a3e7
 
             foreach ($restaurant->getXpaths() as $name => $xpath) {
 
                 $found = $xpath_obj->query($xpath);
 
                 if ($found === false || $found->length === 0) {
                     continue;
                 }
 
e421c421
                 $this->logger->addMealHeader($name);
                 $this->logger->startListing();
3c27a3e7
 
e421c421
                 $parsed = $restaurant->runCallbackParser($found, $this->logger);
 
 	            if ($parsed === false)
 	            {
 	                $exists = false;
 	                foreach ($found->item(0)->childNodes as $elem) {
 
 	                    $meal = trim($elem->nodeValue);
 	                    if (!empty($meal)) {
 		                    $this->logger->addMeal($meal);
 	                        $exists = true;
 	                    }
 	                }
 	            }
06317e47
 
e421c421
 	            $this->logger->endListing();
 
                 if ($parsed === '' OR (isset($exists) AND !$exists)) {
 	                $this->logger->emptyRestaurant();
3c27a3e7
                 }
             }
         }
     }
 }