- Add Horde_Date_Parser_Result for holding complex results from
authorChuck Hagenbuch <chuck@horde.org>
Mon, 29 Jun 2009 02:48:17 +0000 (22:48 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 29 Jun 2009 02:48:17 +0000 (22:48 -0400)
  Horde_Date_Parser#parse()

- Replace the 'guess' option with a generic 'return' option that allows you to
  pick a Horde_Date, Horde_Date_Span, or Horde_Date_Parser_Result

- Move the guess() function to the Horde_Date_Parser_Result object (maybe it
  should actually be on the span itself?)

- Horde_Date_Parser_Result can return the tagged or untagged text from the
  original string.

framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base.php
framework/Date_Parser/lib/Horde/Date/Parser/Result.php [new file with mode: 0644]
framework/Date_Parser/package.xml
framework/Date_Parser/test/Horde/Date/Parser/Locale/BaseTest.php
framework/Date_Parser/test/Horde/Date/Parser/ParserTest.php
framework/Date_Parser/test/Horde/Date/Parser/ResultTest.php [new file with mode: 0644]

index 6ee13e0..45ba8ad 100644 (file)
@@ -14,8 +14,8 @@ class Horde_Date_Parser_Locale_Base
 
     /**
     # Parses a string containing a natural language date or time. If the parser
-    # can find a date or time, either a Time or Chronic::Span will be returned
-    # (depending on the value of <tt>:guess</tt>). If no date or time can be found,
+    # can find a date or time, either a Horde_Date or Horde_Date_Span will be returned
+    # (depending on the value of <tt>:return</tt>). If no date or time can be found,
     # +nil+ will be returned.
     #
     # Options are:
@@ -28,17 +28,20 @@ class Horde_Date_Parser_Locale_Base
     #     past. Specify <tt>:future</tt> or omit to set a future context.
     #
     # [<tt>:now</tt>]
-    #     Time (defaults to Time.now)
+    #     Time (defaults to time())
     #
-    #     By setting <tt>:now</tt> to a Time, all computations will be based off
-    #     of that time instead of Time.now
+    #     By setting <tt>:now</tt> to a Horde_Date, all computations will be based off
+    #     of that time instead of time().
     #
-    # [<tt>:guess</tt>]
-    #     +true+ or +false+ (defaults to +true+)
+    # [<tt>:return</tt>]
+    #     'result', 'span', or 'date' (defaults to 'date')
     #
     #     By default, the parser will guess a single point in time for the
     #     given date or time. If you'd rather have the entire time span returned,
-    #     set <tt>:guess</tt> to +false+ and a Chronic::Span will be returned.
+    #     set <tt>:return</tt> to 'span' and a Horde_Date_Span will be returned.
+    #     If you want the entire result, including tokens (for retrieving the text
+    #     that was or was not tagged, for example), set <tt>:return</tt> to 'result'
+    #     and you will get a result object.
     #
     # [<tt>:ambiguousTimeRange</tt>]
     #     Integer or <tt>:none</tt> (defaults to <tt>6</tt> (6am-6pm))
@@ -57,7 +60,7 @@ class Horde_Date_Parser_Locale_Base
         $defaultOptions = array(
             'context' => 'future',
             'now' => new Horde_Date(time()),
-            'guess' => true,
+            'return' => 'date',
             'ambiguousTimeRange' => 6,
         );
         $options = array_merge($defaultOptions, $this->args, $specifiedOptions);
@@ -94,16 +97,20 @@ class Horde_Date_Parser_Locale_Base
         }
 
         // strip any non-tagged tokens
-        $tokens = array_values(array_filter($tokens, create_function('$t', 'return $t->tagged();')));
+        $taggedTokens = array_values(array_filter($tokens, create_function('$t', 'return $t->tagged();')));
 
         // do the heavy lifting
-        $span = $this->tokensToSpan($tokens, $options);
-
-        // guess a time within a span if required
-        if ($options['guess']) {
-            return $this->guess($span);
-        } else {
-            return $span;
+        $span = $this->tokensToSpan($taggedTokens, $options);
+
+        // generate the result and return it, the span, or a guessed time within the span
+        $result = new Horde_Date_Parser_Result($span, $tokens);
+        switch ($options['return']) {
+        case 'result':
+            return $result;
+        case 'span':
+            return $result->span;
+        case 'date':
+            return $result->guess();
         }
     }
 
@@ -186,22 +193,6 @@ class Horde_Date_Parser_Locale_Base
         return array_map(create_function('$w', 'return new Horde_Date_Parser_Token($w);'), preg_split('/\s+/', $text));
     }
 
-    /**
-     * Guess a specific time within the given span
-     */
-    public function guess($span)
-    {
-        if (! $span instanceof Horde_Date_Span) {
-            return null;
-        }
-
-        if ($span->width() > 1) {
-            return $span->begin->add($span->width() / 2);
-        } else {
-            return $span->begin;
-        }
-    }
-
     public function initDefinitions()
     {
         if ($this->definitions) { return; }
diff --git a/framework/Date_Parser/lib/Horde/Date/Parser/Result.php b/framework/Date_Parser/lib/Horde/Date/Parser/Result.php
new file mode 100644 (file)
index 0000000..415b77c
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+class Horde_Date_Parser_Result
+{
+    public $span;
+    public $tokens = array();
+
+    public function __construct($span, $tokens)
+    {
+        $this->span = $span;
+        $this->tokens = $tokens;
+    }
+
+    /**
+     * Guess a specific time within the given span
+     */
+    public function guess()
+    {
+        if (! $this->span instanceof Horde_Date_Span) {
+            return null;
+        }
+
+        if ($this->span->width() > 1) {
+            return $this->span->begin->add($this->span->width() / 2);
+        } else {
+            return $this->span->begin;
+        }
+    }
+
+    public function taggedText()
+    {
+        $taggedTokens = array_values(array_filter($this->tokens, create_function('$t', 'return $t->tagged();')));
+        return implode(' ', array_map(create_function('$t', 'return $t->word;'), $taggedTokens));
+    }
+
+    public function untaggedText()
+    {
+        $untaggedTokens = array_values(array_filter($this->tokens, create_function('$t', 'return ! $t->tagged();')));
+        return implode(' ', array_map(create_function('$t', 'return $t->word;'), $untaggedTokens));
+    }
+
+}
index 7489d7b..7c5ece3 100644 (file)
@@ -57,6 +57,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
        </dir> <!-- /lib/Horde/Date/Parser/Locale -->
        <file name="Exception.php" role="php" />
        <file name="Handler.php" role="php" />
+       <file name="Result.php" role="php" />
        <file name="Token.php" role="php" />
       </dir> <!-- /lib/Horde/Date/Parser -->
       <file name="Parser.php" role="php" />
@@ -95,6 +96,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Date/Parser/Locale/De/Separator.php" as="Horde/Date/Parser/Locale/De/Separator.php" />
    <install name="lib/Horde/Date/Parser/Locale/De/Timezone.php" as="Horde/Date/Parser/Locale/De/Timezone.php" />
    <install name="lib/Horde/Date/Parser/Locale/De.php" as="Horde/Date/Parser/Locale/De.php" />
+   <install name="lib/Horde/Date/Parser/Result.php" as="Horde/Date/Parser/Result.php" />
    <install name="lib/Horde/Date/Parser/Token.php" as="Horde/Date/Parser/Token.php" />
    <install name="lib/Horde/Date/Parser.php" as="Horde/Date/Parser.php" />
   </filelist>
index 5c2d33a..b146f95 100644 (file)
@@ -335,7 +335,7 @@ class Horde_Date_Parser_Locale_BaseTest extends Horde_Test_Case
         $time = $this->parser->parse("this year");
         $this->assertEquals(new Horde_Date(2006, 10, 24, 12, 30), $time);
 
-        $span = $this->parser->parse("this year", array('context' => 'past', 'guess' => false));
+        $span = $this->parser->parse("this year", array('context' => 'past', 'return' => 'span'));
         $this->assertEquals(new Horde_Date_Span('2006-01-01 00:00:00', '2006-08-16 00:00:00'), $span);
     }
 
@@ -657,7 +657,7 @@ class Horde_Date_Parser_Locale_BaseTest extends Horde_Test_Case
 
     public function testParseGuess_o_r_g_r()
     {
-        $span = $this->parser->parse("3rd month next year", array('guess' => false));
+        $span = $this->parser->parse("3rd month next year", array('return' => 'span'));
         $this->assertEquals(new Horde_Date_Span('2007-03-01 00:00:00', '2007-04-01 00:00:00'), $span);
 
         $time = $this->parser->parse("3rd thursday this september");
@@ -675,15 +675,15 @@ class Horde_Date_Parser_Locale_BaseTest extends Horde_Test_Case
 
     public function testParseSpan()
     {
-        $span = $this->parser->parse('friday', array('guess' => false));
+        $span = $this->parser->parse('friday', array('return' => 'span'));
         $this->assertEquals(new Horde_Date(2006, 8, 18), $span->begin);
         $this->assertEquals(new Horde_Date(2006, 8, 19), $span->end);
 
-        $span = $this->parser->parse('november', array('guess' => false));
+        $span = $this->parser->parse('november', array('return' => 'span'));
         $this->assertEquals(new Horde_Date(2006, 11, 1), $span->begin);
         $this->assertEquals(new Horde_Date(2006, 12, 1), $span->end);
 
-        $span = $this->parser->parse('weekend', array('guess' => false));
+        $span = $this->parser->parse('weekend', array('return' => 'span'));
         $this->assertEquals(new Horde_Date(2006, 8, 19), $span->begin);
         $this->assertEquals(new Horde_Date(2006, 8, 21), $span->end);
     }
index ad30392..823aefb 100644 (file)
@@ -47,18 +47,4 @@ class Horde_Date_Parser_ParserTest extends Horde_Test_Case
         $this->assertEquals(2, count($tokens));
     }
 
-    public function testGuess()
-    {
-        $parser = Horde_Date_Parser::factory();
-
-        $span = new Horde_Date_Span(new Horde_Date('2006-08-16 00:00:00'), new Horde_Date('2006-08-17 00:00:00'));
-        $this->assertEquals(new Horde_Date('2006-08-16 12:00:00'), $parser->guess($span));
-
-        $span = new Horde_Date_Span(new Horde_Date('2006-08-16 00:00:00'), new Horde_Date('2006-08-17 00:00:01'));
-        $this->assertEquals(new Horde_Date('2006-08-16 12:00:00'), $parser->guess($span));
-
-        $span = new Horde_Date_Span(new Horde_Date('2006-11-01 00:00:00'), new Horde_Date('2006-12-01 00:00:00'));
-        $this->assertEquals(new Horde_Date('2006-11-16 00:00:00'), $parser->guess($span));
-    }
-
 }
diff --git a/framework/Date_Parser/test/Horde/Date/Parser/ResultTest.php b/framework/Date_Parser/test/Horde/Date/Parser/ResultTest.php
new file mode 100644 (file)
index 0000000..59589a7
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/**
+ * @category   Horde
+ * @package    Horde_Date
+ * @subpackage UnitTests
+ */
+
+/**
+ * @category   Horde
+ * @package    Horde_Date
+ * @subpackage UnitTests
+ */
+class Horde_Date_Parser_ResultTest extends Horde_Test_Case
+{
+    public function testGuess()
+    {
+        $result = new Horde_Date_Parser_Result(null, null);
+
+        $result->span = new Horde_Date_Span(new Horde_Date('2006-08-16 00:00:00'), new Horde_Date('2006-08-17 00:00:00'));
+        $this->assertEquals(new Horde_Date('2006-08-16 12:00:00'), $result->guess($span));
+
+        $result->span = new Horde_Date_Span(new Horde_Date('2006-08-16 00:00:00'), new Horde_Date('2006-08-17 00:00:01'));
+        $this->assertEquals(new Horde_Date('2006-08-16 12:00:00'), $result->guess($span));
+
+        $result->span = new Horde_Date_Span(new Horde_Date('2006-11-01 00:00:00'), new Horde_Date('2006-12-01 00:00:00'));
+        $this->assertEquals(new Horde_Date('2006-11-16 00:00:00'), $result->guess($span));
+    }
+
+}