introduce UserInterface class, add simple parameter support for web and cli
authorChristian Weiske <cweiske@cweiske.de>
Thu, 23 Jan 2014 16:22:47 +0000 (17:22 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 23 Jan 2014 16:22:47 +0000 (17:22 +0100)
src/bdrem/Cli.php
src/bdrem/UserInterface.php [new file with mode: 0644]
src/bdrem/Web.php
src/bdrem/WebText.php

index 807f336..c38e173 100644 (file)
@@ -1,23 +1,32 @@
 <?php
 namespace bdrem;
 
 <?php
 namespace bdrem;
 
-class Cli
+class Cli extends UserInterface
 {
 {
-    public function run()
+    protected function loadParameters($cfg)
     {
     {
-        $cfg = new Config();
-        $cfg->load();
-        setlocale(LC_TIME, $cfg->locale);
-        $source = $cfg->loadSource();
+        $params = $GLOBALS['argv'];
+        array_shift($params);
+        $storeInto = null;
+        foreach ($params as $param) {
+            if ($storeInto !== null) {
+                $cfg->$storeInto = (int)$param;
+                $storeInto = null;
+                continue;
+            }
 
 
-        $arEvents = $source->getEvents(
-            date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter
-        );
-        usort($arEvents, '\\bdrem\\Event::compare');
-        $this->render($arEvents);
+            if ($param == '--days-after' || $param == '-a') {
+                $storeInto = 'daysAfter';
+                continue;
+            } else if ($param == '--days-before' || $param == '-b') {
+                $storeInto = 'daysBefore';
+                continue;
+            }
+            $storeInto = null;
+        }
     }
 
     }
 
-    public function render($arEvents)
+    protected function render($arEvents)
     {
         $r = new Renderer_Console();
         echo $r->render($arEvents);
     {
         $r = new Renderer_Console();
         echo $r->render($arEvents);
diff --git a/src/bdrem/UserInterface.php b/src/bdrem/UserInterface.php
new file mode 100644 (file)
index 0000000..ffaa279
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+namespace bdrem;
+
+abstract class UserInterface
+{
+    public function run()
+    {
+        $cfg = new Config();
+        $cfg->load();
+        setlocale(LC_TIME, $cfg->locale);
+        $source = $cfg->loadSource();
+
+        $this->loadParameters($cfg);
+        $arEvents = $source->getEvents(
+            date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter
+        );
+        usort($arEvents, '\\bdrem\\Event::compare');
+        $this->render($arEvents);
+    }
+
+    protected function loadParameters($cfg)
+    {
+    }
+
+    abstract protected function render($arEvents);
+}
+?>
\ No newline at end of file
index ac619ce..ab3973a 100644 (file)
@@ -1,26 +1,22 @@
 <?php
 namespace bdrem;
 
 <?php
 namespace bdrem;
 
-class Web
+class Web extends UserInterface
 {
 {
-    public function run()
+    protected function render($arEvents)
     {
     {
-        $cfg = new Config();
-        $cfg->load();
-        setlocale(LC_TIME, $cfg->locale);
-        $source = $cfg->loadSource();
-
-        $arEvents = $source->getEvents(
-            date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter
-        );
-        usort($arEvents, '\\bdrem\\Event::compare');
-        $this->render($arEvents);
+        $r = new Renderer_Html();
+        echo $r->render($arEvents);
     }
 
     }
 
-    public function render($arEvents)
+    protected function loadParameters($cfg)
     {
     {
-        $r = new Renderer_Html();
-        echo $r->render($arEvents);
+        if (isset($_GET['daysBefore'])) {
+            $cfg->daysBefore = (int) $_GET['daysBefore'];
+        }
+        if (isset($_GET['daysAfter'])) {
+            $cfg->daysAfter = (int) $_GET['daysAfter'];
+        }
     }
 }
 ?>
     }
 }
 ?>
index 8f3ff8a..aaace51 100644 (file)
@@ -3,7 +3,7 @@ namespace bdrem;
 
 class WebText extends Web
 {
 
 class WebText extends Web
 {
-    public function render($arEvents)
+    protected function render($arEvents)
     {
         header('Content-type: text/plain; charset=utf-8');
         $r = new Renderer_Console();
     {
         header('Content-type: text/plain; charset=utf-8');
         $r = new Renderer_Console();