Browse code

Vytvorena trieda na logovanie spracovania, nie je viac zodpovednostou parsera

nosko authored on 26/01/2013 at 14:38:51
Showing 1 changed files
... ...
@@ -3,25 +3,21 @@
3 3
 /**
4 4
  * Class representing restaurant
5 5
  */
6
-class Restaurant
7
-{
6
+class Restaurant {
8 7
 
9 8
     private $url;
10 9
     private $name;
11 10
     private $xpaths = array();
12 11
 
13
-    public function getName()
14
-    {
12
+    public function getName() {
15 13
         return $this->name;
16 14
     }
17 15
 
18
-    public function getUrl()
19
-    {
16
+    public function getUrl() {
20 17
         return $this->url;
21 18
     }
22 19
 
23
-    public function getXpaths()
24
-    {
20
+    public function getXpaths() {
25 21
         return $this->xpaths;
26 22
     }
27 23
 
... ...
@@ -30,8 +26,7 @@ class Restaurant
30 30
      * @param $url
31 31
      * @param $xpaths
32 32
      */
33
-    function __construct($name, $url, $xpaths)
34
-    {
33
+    function __construct($name, $url, $xpaths) {
35 34
 
36 35
         if (empty($url) || empty($xpaths)) {
37 36
             throw new InvalidArgumentException("All argument are required");
... ...
@@ -51,13 +46,49 @@ class Restaurant
51 51
 
52 52
 }
53 53
 
54
+interface iOutput {
55
+    public function log($message);
56
+    public function display();
57
+}
58
+
59
+class Output implements iOutput {
60
+    protected $output = "";
61
+
62
+    public function log($message) {
63
+        $this->output .= $message;
64
+    }
65
+
66
+    public function display(){}
67
+}
68
+
69
+class HtmlOutput extends Output  {
70
+
71
+    public function display(){
72
+        echo $this->output;
73
+    }
74
+}
75
+
76
+class EmailOutput {
77
+
78
+    public function display(){
79
+        // Send mail
80
+    }
81
+}
82
+
54 83
 
55 84
 class Parser {
56 85
 
57 86
     private $restaurants = array();
87
+    private $logger;
88
+
89
+    function __construct($logger, $restaurants) {
90
+
91
+        // Check if logger implements iOutput interface
92
+        if (  !in_array("iOutput", class_implements($logger)) ) {
93
+            throw new InvalidArgumentException("Logger class must implement interface iOutput");
94
+        }
58 95
 
59
-    function __construct($restaurants)
60
-    {
96
+        $this->logger = $logger;
61 97
 
62 98
         if (empty($restaurants) || !is_array($restaurants)) {
63 99
             throw new InvalidArgumentException("Array argument required");
... ...
@@ -66,8 +97,7 @@ class Parser {
66 66
         $this->restaurants = $restaurants;
67 67
     }
68 68
 
69
-    public function parse()
70
-    {
69
+    public function parse() {
71 70
         foreach ($this->restaurants as $restaurant) {
72 71
 
73 72
             $source = file_get_contents($restaurant->getUrl());
... ...
@@ -80,7 +110,7 @@ class Parser {
80 80
             @$dom->loadHTML($source);
81 81
             $xpath_obj = new DOMXPath($dom);
82 82
 
83
-            echo("Restaurant: <a href='{$restaurant->getUrl()}'>" . $restaurant->getName() . "</a><br>\n");
83
+            $this->logger->log("Restaurant: <a href='{$restaurant->getUrl()}'>" . $restaurant->getName() . "</a><br>\n");
84 84
 
85 85
             foreach ($restaurant->getXpaths() as $name => $xpath) {
86 86
 
... ...
@@ -90,22 +120,22 @@ class Parser {
90 90
                     continue;
91 91
                 }
92 92
 
93
-                echo "<b>$name</b>";
93
+                $this->logger->log("<b>$name</b>");
94 94
                 $exists = false;
95 95
                 foreach ($found->item(0)->childNodes as $elem) {
96 96
 
97 97
                     $meal = trim($elem->nodeValue);
98 98
                     if (!empty($meal)) {
99
-                        echo("\t<br>$meal\n");
99
+                        $this->logger->log("\t<br>$meal\n");
100 100
                         $exists = true;
101 101
                     }
102 102
                 }
103 103
                 if (!$exists) {
104
-                    echo "<br>{}";
104
+                    $this->logger->log("<br>{}");
105 105
                 }
106
-                echo "<br>";
106
+                $this->logger->log("<br>");
107 107
             }
108
-            echo "<br>";
108
+            $this->logger->log("<br>");
109 109
         }
110 110
     }
111 111
 
... ...
@@ -121,8 +151,10 @@ $slovak = new Restaurant("Slovak pub", "http://www.arcaderestaurant.sk/articles/
121 121
         "Polievka" => '//*[@id="table2"]/tbody/tr[2]'));
122 122
 
123 123
 
124
-$parser = new Parser(array($twenties, $pulitzer, $slovak));
124
+$htmlOutput = new HtmlOutput();
125
+$parser = new Parser($htmlOutput, array($twenties, $pulitzer, $slovak));
125 126
 $parser->parse();
127
+$htmlOutput->display();
126 128
 
127 129
 
128 130
 
... ...
@@ -172,7 +204,7 @@ foreach ($sites as $restaurant => $site)
172 172
                 }
173 173
 
174 174
                 $found_meal = FALSE;
175
-                foreach ($oh_my_god_here_are_saved_meals->item(0)->childNodes as $elem)
175
+        foreach ($oh_my_god_here_are_saved_meals->item(0)->childNodes as $elem)
176 176
                 {
177 177
                         $meal_meal_meal = trim($elem->nodeValue);
178 178
                         if (! empty($meal_meal_meal))