1
0

Calc_Cell.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Wrap an OpenDocument spreadsheet cell
  4. *
  5. * Requires PHP 5.0.3
  6. *
  7. * @version $Id: Calc_Cell.php,v 1.1 2005-02-27 10:58:44 marand Exp $
  8. * @license CeCILL 2.0
  9. * @copyright 2005 Ouest Systemes Informatiques
  10. */
  11. /**
  12. * OpenDocument Spreadsheet cell
  13. */
  14. $erCalc_Cell = error_reporting(E_ALL | E_STRICT);
  15. class Calc_Cell
  16. {
  17. /**
  18. * Values of the 'office:value-type' attribute
  19. */
  20. const TYPE_STRING = 'string';
  21. const TYPE_FLOAT = 'float';
  22. const TYPE_EMPTY = 'empty'; // used if attribute is missing
  23. public $dataType;
  24. public $dataValue;
  25. public $displayValue;
  26. /**
  27. * @param DOMNode $cellNode
  28. */
  29. function __construct(DOMNode $cellNode)
  30. {
  31. if (is_a($cellNode, 'DOMElement'))
  32. {
  33. $this->dataType = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value-type');
  34. /**
  35. * Note: cells with office:value-type==string don't have a office:value
  36. * attribute, but it is clearer to just fetch it and get NULL than test.
  37. * Maybe more sophistication will be needed in the future
  38. */
  39. $this->dataValue = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value');
  40. $p = $cellNode->childNodes->item(0);
  41. $this->displayValue = $p->textContent;
  42. unset($p);
  43. }
  44. else
  45. {
  46. $this->dataType = Calc_Cell::TYPE_EMPTY;
  47. $this->dataValue = NULL;
  48. $this->displayValue = '';
  49. }
  50. }
  51. /**
  52. * Convert a PHP value to a wrapped OpenDocument cell value
  53. *
  54. * @param mixed $x
  55. */
  56. function set($x = NULL)
  57. {
  58. $type = gettype($x);
  59. switch ($type)
  60. {
  61. case 'NULL':
  62. $this->dataType = Calc_Cell::TYPE_EMPTY;
  63. $this->dataValue = NULL;
  64. $this->displayValue = NULL;
  65. break;
  66. case 'string':
  67. $this->dataType = Calc_Cell::TYPE_STRING;
  68. $this->dataValue = NULL;
  69. $this->displayValue = $x;
  70. break;
  71. case 'double':
  72. $this->dataType = Calc_Cell::TYPE_FLOAT;
  73. $this->dataValue = $x;
  74. $this->displayValue = number_format($x);
  75. break;
  76. default:
  77. $this->set();
  78. throw new Exception('Calc_Cell::set unsupported value type ', $type);
  79. break;
  80. }
  81. }
  82. /**
  83. * implement magic method __toString
  84. *
  85. * @return string
  86. */
  87. function __toString()
  88. {
  89. return (string) $this->displayValue;
  90. }
  91. }
  92. error_reporting($erCalc_Cell);
  93. unset($erCalc_Cell);