Browse Source

Replaced is_a by instanceof
Fixed cell constructor: previous logic was incorrect in most cases

Frederic G. Marand 16 years ago
parent
commit
f3d7af71de
1 changed files with 40 additions and 24 deletions
  1. 40 24
      ooo/Calc_Cell.php

+ 40 - 24
ooo/Calc_Cell.php

@@ -4,7 +4,7 @@
  *
  * Requires PHP 5.2
  *
- * @version $Id: Calc_Cell.php,v 1.2 2007-08-06 06:17:06 marand Exp $
+ * @version $Id: Calc_Cell.php,v 1.3 2007-09-09 19:50:22 marand Exp $
  * @license CeCILL 2.0
  * @copyright 2005-2006 Ouest Systemes Informatiques
  */
@@ -22,40 +22,56 @@ class Calc_Cell
   const TYPE_STRING = 'string';
   const TYPE_FLOAT  = 'float';
   const TYPE_EMPTY  = 'empty'; // used if attribute is missing
-  
+
   public $dataType;
   public $dataValue;
   public $displayValue;
-  
+
   /**
    * @param DOMNode $cellNode
    */
   function __construct(DOMNode $cellNode)
     {
-    if (is_a($cellNode, 'DOMElement'))
+    if ($cellNode instanceof DOMElement)
       {
       $this->dataType  = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value-type');
-      /**
-       * Note: cells with office:value-type==string don't have a office:value 
-       * attribute, but it is clearer to just fetch it and get NULL than test.
-       * Maybe more sophistication will be needed in the future
-       */
-      $this->dataValue = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value');
-      $p = $cellNode->childNodes->item(0); 
-      $this->displayValue = $p->textContent;
-      unset($p);     
-      }
-    else
-      {
-      $this->dataType  = Calc_Cell::TYPE_EMPTY;
-      $this->dataValue = NULL;
-      $this->displayValue = '';
+
+      if ($cellNode->childNodes->length == 0)
+        {
+        $this->dataType  = Calc_Cell::TYPE_EMPTY;
+        $this->dataValue = NULL;
+        $this->displayValue = '';
+        }
+      else
+        {
+        $p = $cellNode->childNodes->item(0);
+        $this->displayValue = $p->textContent;
+        unset($p);
+
+        /**
+         * Note: cells with office:value-type==string don't have a office:value
+         */
+        if ($this->dataType == Calc_Cell::TYPE_STRING)
+          {
+          //$this->dataValue = $this->displayValue;
+          }
+        else
+          {
+          $this->dataValue = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value');
+          }
+        }
       }
+    /* echo "children: " . $cellNode->childNodes->length
+     . ", type = "    . $this->dataType
+     . ", value = "   . substr($this->dataValue, 0, 20)
+     . ", display = <" . substr($this->displayValue, 0, 20)
+     . ">" . PHP_EOL; */
+
     }
-    
+
   /**
    * Convert a PHP value to a wrapped OpenDocument cell value
-   * 
+   *
    * @param mixed $x
    */
   function set($x = NULL)
@@ -64,7 +80,7 @@ class Calc_Cell
     switch ($type)
       {
       case 'NULL':
-        $this->dataType = Calc_Cell::TYPE_EMPTY; 
+        $this->dataType = Calc_Cell::TYPE_EMPTY;
         $this->dataValue = NULL;
         $this->displayValue = NULL;
         break;
@@ -84,10 +100,10 @@ class Calc_Cell
         break;
       }
     }
-   
+
   /**
    * implement magic method __toString
-   * 
+   *
    * @return string
    */
   function __toString()