From 9afef951067cfacb0e3a808e33670ae67e3e0583 Mon Sep 17 00:00:00 2001 From: Silvio Date: Fri, 20 Aug 2010 15:35:06 -0300 Subject: Using magic methods at CinisisDisplayHelper --- apps/field.php | 6 +- apps/index.php | 2 +- apps/repetition.php | 6 +- apps/subfield.php | 8 +- classes/helpers/CinisisDisplayHelper.php | 222 ++++++++++++++++++++----------- 5 files changed, 152 insertions(+), 92 deletions(-) diff --git a/apps/field.php b/apps/field.php index 3d36df6..697bd90 100644 --- a/apps/field.php +++ b/apps/field.php @@ -12,8 +12,8 @@ $fid = CinisisHttpHelper::get_numeric_arg('fid'); // Draw the document. $display = new CinisisDisplayHelper('Field finder'); -$form = $display->form_input_text('entry', $entry); -$form .= $display->form_input_text('fid', $fid); +$form = $display->formInputText('entry', $entry); +$form .= $display->formInputText('fid', $fid); $display->form($form, basename(__FILE__)); // Get a db instance. @@ -31,7 +31,7 @@ if ($isis) { // Format output. echo "
\n";
   echo "Selected field: $fid: ". $field['name'] ."\n";
-  echo "Showing entry ". $display->entry_link($entry) ." from ". $isis->entries ." total entries.\n";
+  echo "Showing entry ". $display->entryLink($entry) ." from ". $isis->entries ." total entries.\n";
   echo "Repetitions found: ". count($result[$field['name']]) .".\n";
   echo "\n";
   print_r($result[$field['name']]);
diff --git a/apps/index.php b/apps/index.php
index cd5e5bb..fd8877b 100644
--- a/apps/index.php
+++ b/apps/index.php
@@ -11,7 +11,7 @@ $entry = CinisisHttpHelper::get_numeric_arg('entry');
 
 // Draw the document.
 $display = new CinisisDisplayHelper('Isis Navigator');
-$display->form($display->form_input_text('entry', $entry));
+$display->form($display->formInputText('entry', $entry));
 
 // Get a db instance.
 $isis = new Cinisis();
diff --git a/apps/repetition.php b/apps/repetition.php
index a45e6f4..9e47361 100644
--- a/apps/repetition.php
+++ b/apps/repetition.php
@@ -12,8 +12,8 @@ $fid   = CinisisHttpHelper::get_numeric_arg('fid');
 
 // Draw the document.
 $display = new CinisisDisplayHelper('Repetition finder');
-$form    = $display->form_input_text('entry', $entry);
-$form   .= $display->form_input_text('fid', $fid);
+$form    = $display->formInputText('entry', $entry);
+$form   .= $display->formInputText('fid', $fid);
 $display->form($form, basename(__FILE__));
 
 // Get a db instance.
@@ -31,7 +31,7 @@ if ($isis) {
   // Format output.
   echo "
\n";
   echo "Selected field: $fid: ". $field['name'] ."\n";
-  echo "Showing entry ". $display->entry_link($entry) ." from ". $isis->entries ." total entries.\n";
+  echo "Showing entry ". $display->entryLink($entry) ." from ". $isis->entries ." total entries.\n";
   echo "Repetitions found: ". count($result[$field['name']]) .".\n";
   echo "\n";
   print_r($result[$field['name']]);
diff --git a/apps/subfield.php b/apps/subfield.php
index a7a33f2..ba37291 100644
--- a/apps/subfield.php
+++ b/apps/subfield.php
@@ -13,9 +13,9 @@ $sid   = CinisisHttpHelper::get_textual_arg('sid');
 
 // Draw the document.
 $display = new CinisisDisplayHelper('Subfield finder');
-$form    = $display->form_input_text('entry', $entry);
-$form   .= $display->form_input_text('fid', $fid);
-$form   .= $display->form_input_text('sid', $sid);
+$form    = $display->formInputText('entry', $entry);
+$form   .= $display->formInputText('fid', $fid);
+$form   .= $display->formInputText('sid', $sid);
 $display->form($form, basename(__FILE__));
 
 // Get a db instance.
@@ -35,7 +35,7 @@ if ($isis) {
   echo "
\n";
   echo "Selected field: $fid: ". $field['name'] .".\n";
   echo "Selected subfield: $sid: $subfield.\n";
-  echo "Showing entry ". $display->entry_link($entry) ." from ". $isis->entries ." total entries.\n";
+  echo "Showing entry ". $display->entryLink($entry) ." from ". $isis->entries ." total entries.\n";
   echo "Repetitions found: ". count($result[$field['name']]) .".\n";
   echo "\n";
   print_r($result[$field['name']]);
diff --git a/classes/helpers/CinisisDisplayHelper.php b/classes/helpers/CinisisDisplayHelper.php
index d00ad20..468beb1 100644
--- a/classes/helpers/CinisisDisplayHelper.php
+++ b/classes/helpers/CinisisDisplayHelper.php
@@ -15,13 +15,71 @@ class CinisisDisplayHelper {
     $this->title($title);
   }
 
+  /**
+   * Determine internal method names.
+   *
+   * @param $method
+   *   Method name.
+   *
+   * @return
+   *   Method name.
+   */
+  static function methodName($method) {
+    if (php_sapi_name() == "cli") {
+      return 'cli'. ucfirst($method);
+    }
+    else {
+      return 'web'. ucfirst($method);
+    }
+  }
+
+  /**
+   * Dispatcher, dynamic version.
+   *
+   * @param $method
+   *   Method name.
+   *
+   * @param $arguments
+   *   Argument list.
+   *
+   * @return
+   *   Callback result.
+   */
+  public function __call($method, $arguments) {
+    $method = $this->methodName($method);
+
+    if (method_exists($this, $method)) {
+      return call_user_func_array(array($this, $method), $arguments);
+    }
+  }
+
+  /**
+   * Dispatcher, static version.
+   *
+   * @param $method
+   *   Method name.
+   *
+   * @param $arguments
+   *   Argument list.
+   *
+   * @return
+   *   Callback result.
+   */
+  public static function __callStatic($method, $arguments) {
+    $method = self::methodName($method);
+
+    if (is_callable('self', $method)) {
+      return call_user_func_array(array('self', $method), $arguments);
+    }
+  }
+
   /**
    * Draws a page title.
    *
    * @param $title
    *   Page title;
    */
-  static function title($title) {
+  protected static function webTitle($title) {
     if (php_sapi_name() == "cli") {
       echo "$title\n";
     }
@@ -30,34 +88,37 @@ class CinisisDisplayHelper {
     }
   }
 
+  /**
+   * Draws title, CLI version.
+   *
+   * @param $title
+   *   Page title;
+   */
+  protected static function cliTitle($title) {
+    echo "$title\n";
+  }
+
   /**
    * Draws the page header.
    *
    * @param $title
    *   Page title;
    */
-  static function header($title) {
-    if (php_sapi_name() == "cli") {
-      echo "$title\n";
-    }
-    else {
-      echo '';
-      echo '';
-      echo '';
-      echo '';
-      echo ''. $title .'';
-      echo '';
-      echo '';
-    }
+  protected static function webHeader($title) {
+    echo '';
+    echo '';
+    echo '';
+    echo '';
+    echo ''. $title .'';
+    echo '';
+    echo '';
   }
 
   /**
    * Draws the page footer.
    */
-  static function footer() {
-    if (php_sapi_name() != "cli") {
-      echo '';
-    }
+  protected static function webFooter() {
+    echo '';
   }
 
   /**
@@ -72,14 +133,12 @@ class CinisisDisplayHelper {
    * @param $method
    *   Form method.
    */
-  static function form($content, $action = 'index.php', $method = 'get') {
-    if (php_sapi_name() != "cli") {
-      echo '
'; - echo $content; - echo ''; - echo '
'; - echo '
'; - } + protected static function webForm($content, $action = 'index.php', $method = 'get') { + echo '
'; + echo $content; + echo ''; + echo '
'; + echo '
'; } /** @@ -94,11 +153,7 @@ class CinisisDisplayHelper { * @return * Rendered text input. */ - static function form_input_text($name, $default = null) { - if (php_sapi_name() == "cli") { - return; - } - + protected static function webFormInputText($name, $default = null) { if ($default) { $default = 'value="'. $default .'"'; } @@ -121,11 +176,7 @@ class CinisisDisplayHelper { * @param $extra * Extra parameters. */ - static function navbar($entry, $entries, $action = 'index.php', $extra = NULL) { - if (php_sapi_name() == "cli") { - return; - } - + protected static function webNavbar($entry, $entries, $action = 'index.php', $extra = NULL) { // First / prev links. if ($entry != 1) { $prev = $entry - 1; @@ -156,10 +207,8 @@ class CinisisDisplayHelper { * @return * Formatted link. */ - static function link($action, $args, $title) { - if (php_sapi_name() != "cli") { - return ''. $title .''; - } + protected static function webLink($action, $args, $title) { + return ''. $title .''; } /** @@ -171,28 +220,22 @@ class CinisisDisplayHelper { * @return * Formatted link. */ - static function entry_link($entry) { - if (php_sapi_name() != "cli") { - return self::link('index.php', '?entry='. $entry, $entry); - } + protected static function webEntryLink($entry) { + return self::link('index.php', '?entry='. $entry, $entry); } /** * Draws tags for opening a table. */ - static function open_table() { - if (php_sapi_name() != "cli") { - echo ''; - } + protected static function webOpenTable() { + echo '
'; } /** * Draws tags for closing a table. */ - static function close_table() { - if (php_sapi_name() != "cli") { - echo '
'; - } + protected static function webCloseTable() { + echo ''; } /** @@ -201,13 +244,18 @@ class CinisisDisplayHelper { * @param $text * Inner text. */ - static function h2($text) { - if (php_sapi_name() == "cli") { - echo "$text\n"; - } - else { - echo "

$text

"; - } + protected static function webH2($text) { + echo "

$text

"; + } + + /** + * Draws a h2 element, CLI version. + * + * @param $text + * Inner text. + */ + protected static function cliH2($text) { + echo "$text\n"; } /** @@ -216,25 +264,32 @@ class CinisisDisplayHelper { * @param $text * Inner text. */ - static function h3($text) { - if (php_sapi_name() == "cli") { - echo "$text\n"; - } - else { - echo "

$text

"; - } + protected static function webH3($text) { + echo "

$text

"; + } + + /** + * Draws a h3 element, CLI version. + * + * @param $text + * Inner text. + */ + protected static function cliH3($text) { + echo "$text\n"; } /** * Draws a line break element. */ - static function br() { - if (php_sapi_name() == "cli") { - echo "\n"; - } - else { - echo "
"; - } + protected static function webBr() { + echo "
"; + } + + /** + * Draws a line break element, CLI version. + */ + protected static function cliBr() { + echo "\n"; } /** @@ -243,12 +298,17 @@ class CinisisDisplayHelper { * @param $text * Inner text. */ - static function pre($text) { - if (php_sapi_name() == "cli") { - echo "$text\n"; - } - else { - echo "
\n$text
"; - } - } + protected static function webPre($text) { + echo "
\n$text
"; + } + + /** + * Draws a pre format block element. + * + * @param $text + * Inner text. + */ + protected static function cliPre($text) { + echo "$text\n"; + } } -- cgit v1.2.3