- go back to monday, tuesday, for the DayName repeater; use Horde_Date::DATE_MONDAY...
authorChuck Hagenbuch <chuck@horde.org>
Thu, 5 Feb 2009 04:43:36 +0000 (23:43 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Thu, 5 Feb 2009 05:02:43 +0000 (00:02 -0500)
- add $type to the Repeater subclasses that need it
- use Horde_Date::add()/Horde_Date::sub() in Horde_Date_Span add/sub methods

framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base/Repeater.php
framework/Date_Parser/lib/Horde/Date/Repeater.php
framework/Date_Parser/lib/Horde/Date/Repeater/DayName.php
framework/Date_Parser/lib/Horde/Date/Repeater/DayPortion.php
framework/Date_Parser/lib/Horde/Date/Repeater/MonthName.php
framework/Date_Parser/lib/Horde/Date/Repeater/SeasonName.php
framework/Date_Parser/lib/Horde/Date/Repeater/Time.php
framework/Date_Parser/lib/Horde/Date/Span.php

index b722664..d63c136 100644 (file)
@@ -17,16 +17,16 @@ class Horde_Date_Parser_Locale_Base_Repeater extends Horde_Date_Parser_Tag
     );
 
     public $dayNameScanner = array(
-        '/^m[ou]n(day)?$/' => Horde_Date::DATE_MONDAY,
-        '/^t(ue|eu|oo|u|)s(day)?$/' => Horde_Date::DATE_TUESDAY,
-        '/^tue$/' => Horde_Date::DATE_TUESDAY,
-        '/^we(dnes|nds|nns)day$/' => Horde_Date::DATE_WEDNESDAY,
-        '/^wed$/' => Horde_Date::DATE_WEDNESDAY,
-        '/^th(urs|ers)day$/' => Horde_Date::DATE_THURSDAY,
-        '/^thu$/' => Horde_Date::DATE_THURSDAY,
-        '/^fr[iy](day)?$/' => Horde_Date::DATE_FRIDAY,
-        '/^sat(t?[ue]rday)?$/' => Horde_Date::DATE_SATURDAY,
-        '/^su[nm](day)?$/' => Horde_Date::DATE_SUNDAY,
+        '/^m[ou]n(day)?$/' => 'monday',
+        '/^t(ue|eu|oo|u|)s(day)?$/' => 'tuesday',
+        '/^tue$/' => 'tuesday',
+        '/^we(dnes|nds|nns)day$/' => 'wednesday',
+        '/^wed$/' => 'wednesday',
+        '/^th(urs|ers)day$/' => 'thursday',
+        '/^thu$/' => 'thursday',
+        '/^fr[iy](day)?$/' => 'friday',
+        '/^sat(t?[ue]rday)?$/' => 'saturday',
+        '/^su[nm](day)?$/' => 'sunday',
     );
 
     public $dayPortionScanner = array(
index 43ec167..5fdd964 100644 (file)
@@ -6,6 +6,8 @@
  */
 abstract class Horde_Date_Repeater
 {
+    public $now;
+
     /**
      * returns the width (in seconds or months) of this repeatable.
      */
index fe886e6..9cb50f6 100644 (file)
@@ -5,6 +5,12 @@ class Horde_Date_Repeater_DayName extends Horde_Date_Repeater
     const DAY_SECONDS = 86400;
 
     public $currentDayStart;
+    public $type;
+
+    public function __construct($type)
+    {
+        $this->type = $type;
+    }
 
     public function next($pointer)
     {
@@ -15,7 +21,7 @@ class Horde_Date_Repeater_DayName extends Horde_Date_Repeater
         if (!$this->currentDayStart) {
             $this->currentDayStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + $direction));
 
-            $dayNum = $this->type;
+            $dayNum = $this->_dayNumber($this->type);
             while ($this->currentDayStart->dayOfWeek() != $dayNum) {
                 $this->currentDayStart->day += $direction;
             }
@@ -45,16 +51,24 @@ class Horde_Date_Repeater_DayName extends Horde_Date_Repeater
 
     public function __toString()
     {
-        $dayStrings = array(
-            Horde_Date::DATE_MONDAY => 'monday',
-            Horde_Date::DATE_TUESDAY => 'tuesday',
-            Horde_Date::DATE_WEDNESDAY => 'wednesday',
-            Horde_Date::DATE_THURSDAY => 'thursday',
-            Horde_Date::DATE_FRIDAY => 'friday',
-            Horde_Date::DATE_SATURDAY => 'saturday',
-            Horde_Date::DATE_SUNDAY => 'sunday',
+        return parent::__toString() . '-dayname-' . $this->type;
+    }
+
+    protected function _dayNumber($dayName)
+    {
+        $days = array(
+            'monday' => Horde_Date::DATE_MONDAY,
+            'tuesday' => Horde_Date::DATE_TUESDAY,
+            'wednesday' => Horde_Date::DATE_WEDNESDAY,
+            'thursday' => Horde_Date::DATE_THURSDAY,
+            'friday' => Horde_Date::DATE_FRIDAY,
+            'saturday' => Horde_Date::DATE_SATURDAY,
+            'sunday' => Horde_Date::DATE_SUNDAY,
         );
-        return parent::__toString() . '-dayname-' . $dayStrings[$this->type];
+        if (!isset($days[$dayName])) {
+            throw new InvalidArgumentException('Invalid day name "' . $dayName . '"');
+        }
+        return $days[$dayName];
     }
 
 }
index e03a637..ec872fd 100644 (file)
@@ -23,10 +23,11 @@ class Horde_Date_Repeater_DayPortion extends Horde_Date_Repeater
 
     public $range;
     public $currentSpan;
+    public $type;
 
     public function __construct($type)
     {
-        parent::__construct($type);
+        $this->type = $type;
 
         if (is_int($type)) {
             $this->range = array(($type * 3600), (($type + 12) * 3600));
index e1f21e2..e406f99 100644 (file)
@@ -2,6 +2,12 @@
 class Horde_Date_Repeater_MonthName extends Horde_Date_Repeater
 {
     public $currentMonthStart;
+    public $type;
+
+    public function __construct($type)
+    {
+        $this->type = $type;
+    }
 
     public function next($pointer)
     {
index d462ce1..cffa237 100644 (file)
@@ -10,6 +10,12 @@ class Horde_Date_Repeater_SeasonName extends Horde_Date_Repeater_Season
     public $autumn = array('sep 23', 'dec 21');
     public $winter = array('dec 22', 'mar 19');
     public $spring = array('mar 20', 'jul 20');
+    public $type;
+
+    public function __construct($type)
+    {
+        $this->type = $type;
+    }
 
     public function next($pointer)
     {
index 67147c1..cb1cc45 100644 (file)
@@ -2,6 +2,7 @@
 class Horde_Date_Repeater_Time extends Horde_Date_Repeater
 {
     public $currentTime;
+    public $type;
 
     public function __construct($time, $options = array())
     {
index bfd28f7..6acc5fe 100644 (file)
@@ -39,18 +39,7 @@ class Horde_Date_Span
      */
     public function add($factor)
     {
-        $b = clone($this->begin);
-        $e = clone($this->end);
-        if (is_array($factor)) {
-            foreach ($factor as $property => $value) {
-                $b->$property += $value;
-                $e->$property += $value;
-            }
-        } else {
-            $b->sec += $factor;
-            $e->sec += $factor;
-        }
-        return new Horde_Date_Span($b, $e);
+        return new Horde_Date_Span($this->begin->add($factor), $this->end->add($factor));
     }
 
     /**
@@ -58,15 +47,7 @@ class Horde_Date_Span
      */
     public function sub($factor)
     {
-        if (is_array($factor)) {
-            foreach ($factor as &$value) {
-                $value *= -1;
-            }
-        } else {
-            $factor *= -1;
-        }
-
-        return $this->add($factor);
+        return new Horde_Date_Span($this->begin->sub($factor), $this->end->sub($factor));
     }
 
     public function __toString()