php__phplib/OSInet/Open_Document/Calc/Cell.php
Frederic G. MARAND 6a058cd843 Converted the OSInet ODF Calc reader classes to PSR0
- classes are now namespace.
- added a demo in apps/odf_calc_reader.
- added associated cleanup in Makefile.
2012-05-05 22:58:31 +02:00

103 lines
2.5 KiB
PHP

<?php
/**
* Wrap an OpenDocument spreadsheet cell
*
* Requires PHP 5.3
*
* @license CeCILL 2.0
* @copyright 2005-2012 Ouest Systemes Informatiques
*/
namespace OSInet\Open_Document\Calc;
use OSInet\Open_Document\Calc\Reader;
/**
* OpenDocument Spreadsheet cell
*/
class Cell {
/**
* Values of the 'office:value-type' attribute
*/
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 ($cellNode instanceof \DOMElement) {
$this->dataType = $cellNode->getAttributeNS(Reader::NS_OFFICE, 'value-type');
if ($cellNode->childNodes->length == 0) {
$this->dataType = self::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 == self::TYPE_STRING) {
//$this->dataValue = $this->displayValue;
}
else {
$this->dataValue = $cellNode->getAttributeNS(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) {
$type = gettype($x);
switch ($type) {
case 'NULL':
$this->dataType = self::TYPE_EMPTY;
$this->dataValue = NULL;
$this->displayValue = NULL;
break;
case 'string':
$this->dataType = self::TYPE_STRING;
$this->dataValue = NULL;
$this->displayValue = $x;
break;
case 'double':
$this->dataType = self::TYPE_FLOAT;
$this->dataValue = $x;
$this->displayValue = number_format($x);
break;
default:
$this->set();
throw new \Exception('Cell::set unsupported value type ', $type);
break;
}
}
/**
* implement magic method __toString
*
* @return string
*/
function __toString() {
return (string) $this->displayValue;
}
}