Remove horde/Core dependency in horde/Data
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 13 Jul 2010 22:05:44 +0000 (16:05 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Jul 2010 04:31:25 +0000 (22:31 -0600)
framework/Core/lib/Horde/Core/Factory/Data.php
framework/Data/lib/Horde/Data/Base.php
framework/Data/lib/Horde/Data/Csv.php

index af8b728..7221368 100644 (file)
@@ -58,6 +58,10 @@ class Horde_Core_Factory_Data
         $params['browser'] = $this->_injector->getInstance('Horde_Browser');
         $params['vars'] = Horde_Variables::getDefaultVariables();
 
+        if (strcasecmp($driver, 'csv') === 0) {
+            $params['charset'] = $GLOBALS['registry']->getCharset();
+        }
+
         return Horde_Data::factory($driver, $params);
     }
 
index 7a596fc..1327cc5 100644 (file)
@@ -63,14 +63,15 @@ abstract class Horde_Data_Base
      * <pre>
      * 'browser' - (Horde_Browser) A Horde_Browser object.
      * 'cleanup' - (callback) A callback to call at cleanup time.
+     * 'vars' - (Horde_Variables) Form data.
      * </pre>
      *
-     * @throws Horde_Data_Exception
+     * @throws InvalidArgumentException
      */
     public function __construct(array $params = array())
     {
         if (!isset($params['browser'])) {
-            throw new Horde_Data_Exception('Missing browser parameter.');
+            throw new InvalidArgumentException('Missing browser parameter.');
         }
         $this->_browser = $params['browser'];
 
index d4d022e..1afac3f 100644 (file)
 class Horde_Data_Csv extends Horde_Data_Base
 {
     /**
-     * File extension.
+     * Default charset.
      *
      * @var string
      */
-    protected $_extension = 'csv';
+    protected $_charset = null;
 
     /**
      * MIME content type.
@@ -34,6 +34,34 @@ class Horde_Data_Csv extends Horde_Data_Base
     protected $_contentType = 'text/comma-separated-values';
 
     /**
+     * File extension.
+     *
+     * @var string
+     */
+    protected $_extension = 'csv';
+
+    /**
+     * Constructor.
+     *
+     * @param array $params  Optional parameters:
+     * <pre>
+     * 'charset' - (string) The default charset.
+     *             DEFAULT: NONE
+     * </pre>
+     *
+     * @throws InvalidArgumentException
+     */
+    public function __construct(array $params = array())
+    {
+        if (isset($params['charset'])) {
+            $this->_charset = $params['charset'];
+            unset($params['charset']);
+        }
+
+        parent::__construct($params);
+    }
+
+    /**
      * Tries to discover the CSV file's parameters.
      *
      * @param string $filename  The name of the file to investigate.
@@ -88,14 +116,14 @@ class Horde_Data_Csv extends Horde_Data_Base
         if ($header) {
             $head = Horde_File_Csv::read($filename, $conf);
             if (!empty($charset)) {
-                $head = Horde_String::convertCharset($head, $charset, $GLOBALS['registry']->getCharset());
+                $head = Horde_String::convertCharset($head, $charset, $this->_charset);
             }
         }
 
         $data = array();
         while ($line = Horde_File_Csv::read($filename, $conf)) {
             if (!empty($charset)) {
-                $line = Horde_String::convertCharset($line, $charset, $GLOBALS['registry']->getCharset());
+                $line = Horde_String::convertCharset($line, $charset, $this->_charset);
             }
             if (!isset($head)) {
                 $data[] = $line;
@@ -232,7 +260,7 @@ class Horde_Data_Csv extends Horde_Data_Base
                 $line_no = 1;
                 while ($line_no < 3 && $line = fgets($fp)) {
                     if (!empty($_SESSION['import_data']['charset'])) {
-                        $line = Horde_String::convertCharset($line, $_SESSION['import_data']['charset'], $GLOBALS['registry']->getCharset());
+                        $line = Horde_String::convertCharset($line, $_SESSION['import_data']['charset'], $this->_charset);
                     }
                     $newline = Horde_String::length($line) > 100 ? "\n" : '';
                     $_SESSION['import_data']['first_lines'] .= substr($line, 0, 100) . $newline;