Calc_Cell.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Wrap an OpenDocument spreadsheet cell
  4. *
  5. * Requires PHP 5.2
  6. *
  7. * @version $Id: Calc_Cell.php,v 1.3 2007-09-09 19:50:22 marand Exp $
  8. * @license CeCILL 2.0
  9. * @copyright 2005-2006 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 ($cellNode instanceof DOMElement)
  32. {
  33. $this->dataType = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value-type');
  34. if ($cellNode->childNodes->length == 0)
  35. {
  36. $this->dataType = Calc_Cell::TYPE_EMPTY;
  37. $this->dataValue = NULL;
  38. $this->displayValue = '';
  39. }
  40. else
  41. {
  42. $p = $cellNode->childNodes->item(0);
  43. $this->displayValue = $p->textContent;
  44. unset($p);
  45. /**
  46. * Note: cells with office:value-type==string don't have a office:value
  47. */
  48. if ($this->dataType == Calc_Cell::TYPE_STRING)
  49. {
  50. //$this->dataValue = $this->displayValue;
  51. }
  52. else
  53. {
  54. $this->dataValue = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value');
  55. }
  56. }
  57. }
  58. /* echo "children: " . $cellNode->childNodes->length
  59. . ", type = " . $this->dataType
  60. . ", value = " . substr($this->dataValue, 0, 20)
  61. . ", display = <" . substr($this->displayValue, 0, 20)
  62. . ">" . PHP_EOL; */
  63. }
  64. /**
  65. * Convert a PHP value to a wrapped OpenDocument cell value
  66. *
  67. * @param mixed $x
  68. */
  69. function set($x = NULL)
  70. {
  71. $type = gettype($x);
  72. switch ($type)
  73. {
  74. case 'NULL':
  75. $this->dataType = Calc_Cell::TYPE_EMPTY;
  76. $this->dataValue = NULL;
  77. $this->displayValue = NULL;
  78. break;
  79. case 'string':
  80. $this->dataType = Calc_Cell::TYPE_STRING;
  81. $this->dataValue = NULL;
  82. $this->displayValue = $x;
  83. break;
  84. case 'double':
  85. $this->dataType = Calc_Cell::TYPE_FLOAT;
  86. $this->dataValue = $x;
  87. $this->displayValue = number_format($x);
  88. break;
  89. default:
  90. $this->set();
  91. throw new Exception('Calc_Cell::set unsupported value type ', $type);
  92. break;
  93. }
  94. }
  95. /**
  96. * implement magic method __toString
  97. *
  98. * @return string
  99. */
  100. function __toString()
  101. {
  102. return (string) $this->displayValue;
  103. }
  104. }
  105. error_reporting($erCalc_Cell);
  106. unset($erCalc_Cell);