1
0

Cell.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Wrap an OpenDocument spreadsheet cell
  4. *
  5. * Requires PHP 5.3
  6. *
  7. * @license CeCILL 2.0
  8. * @copyright 2005-2012 Ouest Systemes Informatiques
  9. */
  10. namespace OSInet\Open_Document\Calc;
  11. use OSInet\Open_Document\Calc\Reader;
  12. /**
  13. * OpenDocument Spreadsheet cell
  14. */
  15. class Cell {
  16. /**
  17. * Values of the 'office:value-type' attribute
  18. */
  19. const TYPE_STRING = 'string';
  20. const TYPE_FLOAT = 'float';
  21. const TYPE_EMPTY = 'empty'; // used if attribute is missing
  22. public $dataType;
  23. public $dataValue;
  24. public $displayValue;
  25. /**
  26. * @param \DOMNode $cellNode
  27. */
  28. function __construct(\DOMNode $cellNode) {
  29. if ($cellNode instanceof \DOMElement) {
  30. $this->dataType = $cellNode->getAttributeNS(Reader::NS_OFFICE, 'value-type');
  31. if ($cellNode->childNodes->length == 0) {
  32. $this->dataType = self::TYPE_EMPTY;
  33. $this->dataValue = NULL;
  34. $this->displayValue = '';
  35. }
  36. else {
  37. $p = $cellNode->childNodes->item(0);
  38. $this->displayValue = $p->textContent;
  39. unset($p);
  40. /**
  41. * Note: cells with office:value-type==string don't have a office:value
  42. */
  43. if ($this->dataType == self::TYPE_STRING) {
  44. //$this->dataValue = $this->displayValue;
  45. }
  46. else {
  47. $this->dataValue = $cellNode->getAttributeNS(Reader::NS_OFFICE, 'value');
  48. }
  49. }
  50. }
  51. /* echo "children: " . $cellNode->childNodes->length
  52. . ", type = " . $this->dataType
  53. . ", value = " . substr($this->dataValue, 0, 20)
  54. . ", display = <" . substr($this->displayValue, 0, 20)
  55. . ">" . PHP_EOL; */
  56. }
  57. /**
  58. * Convert a PHP value to a wrapped OpenDocument cell value
  59. *
  60. * @param mixed $x
  61. */
  62. function set($x = NULL) {
  63. $type = gettype($x);
  64. switch ($type) {
  65. case 'NULL':
  66. $this->dataType = self::TYPE_EMPTY;
  67. $this->dataValue = NULL;
  68. $this->displayValue = NULL;
  69. break;
  70. case 'string':
  71. $this->dataType = self::TYPE_STRING;
  72. $this->dataValue = NULL;
  73. $this->displayValue = $x;
  74. break;
  75. case 'double':
  76. $this->dataType = self::TYPE_FLOAT;
  77. $this->dataValue = $x;
  78. $this->displayValue = number_format($x);
  79. break;
  80. default:
  81. $this->set();
  82. throw new \Exception('Cell::set unsupported value type ', $type);
  83. break;
  84. }
  85. }
  86. /**
  87. * implement magic method __toString
  88. *
  89. * @return string
  90. */
  91. function __toString() {
  92. return (string) $this->displayValue;
  93. }
  94. }