Browse code

Restauracie maju vlastne triedy, podpora callbackov parsovania html

Cinan Rakosnik authored on 22/08/2013 at 09:32:48
Showing 8 changed files
... ...
@@ -11,15 +11,41 @@ require_once "iOutput.php";
11 11
 
12 12
 class Output implements iOutput {
13 13
     protected $output = "";
14
-    public  $newLine = "<br>\n";
15 14
 
16 15
     public function log($message) {
17 16
         $this->output .= $message;
18 17
     }
19 18
 
20
-    public function newLine() {
21
-        return $this->newLine;
22
-    }
23
-
24 19
     public function display(){}
20
+
21
+	public function addMealHeader($restaurant)
22
+	{
23
+		// TODO: Implement addHeader() method.
24
+	}
25
+
26
+	public function startListing()
27
+	{
28
+		// TODO: Implement startListing() method.
29
+	}
30
+
31
+	public function endListing()
32
+	{
33
+		// TODO: Implement endListing() method.
34
+	}
35
+
36
+	public function addMeal($meal)
37
+	{
38
+		// TODO: Implement addMeal() method.
39
+	}
40
+
41
+	public function emptyRestaurant()
42
+	{
43
+		// TODO: Implement empty_restaurant() method.
44
+	}
45
+
46
+
47
+	public function addHeader(Restaurant $restaurant)
48
+	{
49
+		// TODO: Implement addHeader() method.
50
+	}
25 51
 }
26 52
\ No newline at end of file
... ...
@@ -23,18 +23,19 @@ class Parser {
23 23
 
24 24
     public function parse() {
25 25
         foreach ($this->restaurants as $restaurant) {
26
+	        /** @var $restaurant Restaurant */
26 27
 
27 28
             $source = file_get_contents($restaurant->getUrl());
28 29
 
29 30
             if ($source === false) {
30
-                throw new RuntimeException("Can't read source address: " . $restaurant->getUrl());
31
+	            continue;
31 32
             }
32 33
 
33 34
             $dom = new DOMDocument();
34 35
             @$dom->loadHTML($source);
35 36
             $xpath_obj = new DOMXPath($dom);
36 37
 
37
-            $this->logger->log("Restaurant: <a href='{$restaurant->getUrl()}'>" . $restaurant->getName() . "</a>".$this->logger->newLine());
38
+	        $this->logger->addHeader($restaurant);
38 39
 
39 40
             foreach ($restaurant->getXpaths() as $name => $xpath) {
40 41
 
... ...
@@ -44,25 +45,30 @@ class Parser {
44 44
                     continue;
45 45
                 }
46 46
 
47
-                $this->logger->log("<b>$name</b>");
48
-                $this->logger->log("<ul>");
49
-                $exists = false;
50
-                foreach ($found->item(0)->childNodes as $elem) {
47
+                $this->logger->addMealHeader($name);
48
+                $this->logger->startListing();
51 49
 
52
-                    $meal = trim($elem->nodeValue);
53
-                    if (!empty($meal)) {
54
-                        $this->logger->log("\t<li>$meal</li>\n");
55
-                        $exists = true;
56
-                    }
57
-                }
58
-                $this->logger->log("</ul>");
50
+                $parsed = $restaurant->runCallbackParser($found, $this->logger);
51
+
52
+	            if ($parsed === false)
53
+	            {
54
+	                $exists = false;
55
+	                foreach ($found->item(0)->childNodes as $elem) {
56
+
57
+	                    $meal = trim($elem->nodeValue);
58
+	                    if (!empty($meal)) {
59
+		                    $this->logger->addMeal($meal);
60
+	                        $exists = true;
61
+	                    }
62
+	                }
63
+	            }
59 64
 
60
-                if (!$exists) {
61
-                    $this->logger->log("<i>Hovno. Nic nemaju.</i>" . $this->logger->newLine());
65
+	            $this->logger->endListing();
66
+
67
+                if ($parsed === '' OR (isset($exists) AND !$exists)) {
68
+	                $this->logger->emptyRestaurant();
62 69
                 }
63 70
             }
64
-
65
-            $this->logger->log($this->logger->newLine());
66 71
         }
67 72
     }
68 73
 }
... ...
@@ -10,11 +10,29 @@
10 10
 /**
11 11
  * Class representing restaurant
12 12
  */
13
-class Restaurant {
13
+abstract class Restaurant {
14 14
 
15
-    private $url;
16
-    private $name;
17
-    private $xpaths = array();
15
+    protected $url;
16
+	protected $name;
17
+	protected $xpaths = array();
18
+
19
+	function __construct($name, $url, $xpaths) {
20
+
21
+		if (empty($url) || empty($xpaths)) {
22
+			throw new InvalidArgumentException("All argument are required");
23
+		}
24
+
25
+		$this->name = $name;
26
+		$this->url = $url;
27
+
28
+		if (is_string($xpaths)) {
29
+			$this->xpaths[] = $xpaths;
30
+		} else if (is_array($xpaths)) {
31
+			$this->xpaths = $xpaths;
32
+		} else {
33
+			throw new InvalidArgumentException("Xpaths must be string or array");
34
+		}
35
+	}
18 36
 
19 37
     public function getName() {
20 38
         return $this->name;
... ...
@@ -28,26 +46,7 @@ class Restaurant {
28 28
         return $this->xpaths;
29 29
     }
30 30
 
31
-    /**
32
-     * @param $name
33
-     * @param $url
34
-     * @param $xpaths
35
-     */
36
-    function __construct($name, $url, $xpaths) {
37
-
38
-        if (empty($url) || empty($xpaths)) {
39
-            throw new InvalidArgumentException("All argument are required");
40
-        }
41
-
42
-        $this->name = $name;
43
-        $this->url = $url;
44
-
45
-        if (is_string($xpaths)) {
46
-            $this->xpaths[] = $xpaths;
47
-        } else if (is_array($xpaths)) {
48
-            $this->xpaths = $xpaths;
49
-        } else {
50
-            throw new InvalidArgumentException("Xpaths must be string or array");
51
-        }
52
-    }
31
+	public function runCallbackParser($parentNode, iOutput $logger) {
32
+		return FALSE;
33
+	}
53 34
 }
... ...
@@ -9,4 +9,14 @@
9 9
 interface iOutput {
10 10
     public function log($message);
11 11
     public function display();
12
+
13
+	public function addMealHeader($restaurant);
14
+	public function startListing();
15
+	public function endListing();
16
+
17
+	public function addMeal($meal);
18
+
19
+	public function emptyRestaurant();
20
+
21
+	public function addHeader(Restaurant $restaurant);
12 22
 }
... ...
@@ -4,12 +4,10 @@ require_once "Restaurant.php";
4 4
 require_once "Output.php";
5 5
 require_once "Parser.php";
6 6
 
7
-define('CONFIG_FILE', 'config.ini');
7
+require_once "restaurants/Pulitzer.php";
8
+require_once "restaurants/Twenties.php";
8 9
 
9
-$twenties = new Restaurant("Twenties", "http://www.twenties.sk/", ["Polievka aj zradlo" => '//*[@id="article"]/div[2]/p[1]']);
10
-$pulitzer = new Restaurant("Pulitzer", "http://www.pulitzer.sk",  ["Tekute predjedlo" => '//*[@id="soups"]', "Zradlo" => '//*[@id="meals"]']);
11
-#todo http://www.irish-pub.sk/sk/denne-menu
12
-#todo http://www.mexicana.sk/dennemenu_sk.htm
10
+define('CONFIG_FILE', 'config.ini');
13 11
 
14 12
 $data = parse_ini_file(CONFIG_FILE);
15 13
 $outputType = ucfirst($data['output']);
... ...
@@ -18,6 +16,13 @@ $outputClass = sprintf('%sOutput', $outputType);
18 18
 require_once sprintf("outputs/%s.php", $outputClass);
19 19
 $output = new $outputClass();
20 20
 
21
-$parser     = new Parser($output, array($twenties, $pulitzer));
21
+$restaurants[] = new Pulitzer();
22
+$restaurants[] = new Twenties();
23
+
24
+#todo http://www.irish-pub.sk/sk/denne-menu
25
+#todo http://www.mexicana.sk/dennemenu_sk.htm
26
+
27
+$parser = new Parser($output, $restaurants);
22 28
 $parser->parse();
23
-$output->display();
24 29
\ No newline at end of file
30
+
31
+$output->display();
... ...
@@ -7,9 +7,41 @@
7 7
  * To change this template use File | Settings | File Templates.
8 8
  */
9 9
 
10
-class HtmlOutput extends Output  {
10
+class HtmlOutput extends Output {
11
+
12
+	private $newLine = "<br>\n";
11 13
 
12 14
     public function display(){
13 15
         echo $this->output;
14 16
     }
17
+
18
+	public function addMealHeader($restaurant)
19
+	{
20
+		$this->log("<strong>$restaurant</strong>");
21
+	}
22
+
23
+	public function startListing()
24
+	{
25
+		$this->log("<ul>");
26
+	}
27
+
28
+	public function endListing()
29
+	{
30
+		$this->log("</ul>");
31
+	}
32
+
33
+	public function addMeal($meal)
34
+	{
35
+		$this->log("\t<li>$meal</li>\n");
36
+	}
37
+
38
+	public function emptyRestaurant()
39
+	{
40
+		$this->log("<i>Hovno. Nic nemaju.</i>" . $this->newLine . $this->newLine);
41
+	}
42
+
43
+	public function addHeader(Restaurant $restaurant) {
44
+		$this->log("Restaurant: <a href='{$restaurant->getUrl()}'>" . $restaurant->getName() . "</a><br/>");
45
+	}
46
+
15 47
 }
16 48
\ No newline at end of file
17 49
new file mode 100644
... ...
@@ -0,0 +1,11 @@
0
+<?php
1
+class Pulitzer extends Restaurant {
2
+
3
+	function __construct() {
4
+		parent::__construct(
5
+			"Pulitzer",
6
+			"http://www.pulitzer.sk",
7
+			["Tekute predjedlo" => '//*[@id="soups"]', "Zradlo" => '//*[@id="meals"]']
8
+		);
9
+	}
10
+}
0 11
new file mode 100644
... ...
@@ -0,0 +1,12 @@
0
+<?php
1
+class Twenties extends Restaurant {
2
+
3
+	function __construct() {
4
+
5
+		parent::__construct(
6
+			"Twenties",
7
+			"http://www.twenties.sk/",
8
+			["Polievka aj zradlo" => '//*[@id="article"]/div[2]/p[1]']
9
+		);
10
+	}
11
+}