[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/spout/src/Spout/Reader/ODS/Helper/ -> CellValueFormatter.php (source)

   1  <?php
   2  
   3  namespace Box\Spout\Reader\ODS\Helper;
   4  
   5  /**
   6   * Class CellValueFormatter
   7   * This class provides helper functions to format cell values
   8   *
   9   * @package Box\Spout\Reader\ODS\Helper
  10   */
  11  class CellValueFormatter
  12  {
  13      /** Definition of all possible cell types */
  14      const CELL_TYPE_STRING = 'string';
  15      const CELL_TYPE_FLOAT = 'float';
  16      const CELL_TYPE_BOOLEAN = 'boolean';
  17      const CELL_TYPE_DATE = 'date';
  18      const CELL_TYPE_TIME = 'time';
  19      const CELL_TYPE_CURRENCY = 'currency';
  20      const CELL_TYPE_PERCENTAGE = 'percentage';
  21      const CELL_TYPE_VOID = 'void';
  22  
  23      /** Definition of XML nodes names used to parse data */
  24      const XML_NODE_P = 'p';
  25      const XML_NODE_S = 'text:s';
  26  
  27      /** Definition of XML attribute used to parse data */
  28      const XML_ATTRIBUTE_TYPE = 'office:value-type';
  29      const XML_ATTRIBUTE_VALUE = 'office:value';
  30      const XML_ATTRIBUTE_BOOLEAN_VALUE = 'office:boolean-value';
  31      const XML_ATTRIBUTE_DATE_VALUE = 'office:date-value';
  32      const XML_ATTRIBUTE_TIME_VALUE = 'office:time-value';
  33      const XML_ATTRIBUTE_CURRENCY = 'office:currency';
  34      const XML_ATTRIBUTE_C = 'text:c';
  35  
  36      /** @var \Box\Spout\Common\Escaper\ODS Used to unescape XML data */
  37      protected $escaper;
  38  
  39      /**
  40       *
  41       */
  42      public function __construct()
  43      {
  44          /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
  45          $this->escaper = new \Box\Spout\Common\Escaper\ODS();
  46      }
  47  
  48      /**
  49       * Returns the (unescaped) correctly marshalled, cell value associated to the given XML node.
  50       * @see http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13
  51       *
  52       * @param \DOMNode $node
  53       * @return string|int|float|bool|\DateTime|\DateInterval|null The value associated with the cell, empty string if cell's type is void/undefined, null on error
  54       */
  55      public function extractAndFormatNodeValue($node)
  56      {
  57          $cellType = $node->getAttribute(self::XML_ATTRIBUTE_TYPE);
  58  
  59          switch ($cellType) {
  60              case self::CELL_TYPE_STRING:
  61                  return $this->formatStringCellValue($node);
  62              case self::CELL_TYPE_FLOAT:
  63                  return $this->formatFloatCellValue($node);
  64              case self::CELL_TYPE_BOOLEAN:
  65                  return $this->formatBooleanCellValue($node);
  66              case self::CELL_TYPE_DATE:
  67                  return $this->formatDateCellValue($node);
  68              case self::CELL_TYPE_TIME:
  69                  return $this->formatTimeCellValue($node);
  70              case self::CELL_TYPE_CURRENCY:
  71                  return $this->formatCurrencyCellValue($node);
  72              case self::CELL_TYPE_PERCENTAGE:
  73                  return $this->formatPercentageCellValue($node);
  74              case self::CELL_TYPE_VOID:
  75              default:
  76                  return '';
  77          }
  78      }
  79  
  80      /**
  81       * Returns the cell String value.
  82       *
  83       * @param \DOMNode $node
  84       * @return string The value associated with the cell
  85       */
  86      protected function formatStringCellValue($node)
  87      {
  88          $pNodeValues = [];
  89          $pNodes = $node->getElementsByTagName(self::XML_NODE_P);
  90  
  91          foreach ($pNodes as $pNode) {
  92              $currentPValue = '';
  93  
  94              foreach ($pNode->childNodes as $childNode) {
  95                  if ($childNode instanceof \DOMText) {
  96                      $currentPValue .= $childNode->nodeValue;
  97                  } else if ($childNode->nodeName === self::XML_NODE_S) {
  98                      $spaceAttribute = $childNode->getAttribute(self::XML_ATTRIBUTE_C);
  99                      $numSpaces = (!empty($spaceAttribute)) ? intval($spaceAttribute) : 1;
 100                      $currentPValue .= str_repeat(' ', $numSpaces);
 101                  }
 102              }
 103  
 104              $pNodeValues[] = $currentPValue;
 105          }
 106  
 107          $escapedCellValue = implode("\n", $pNodeValues);
 108          $cellValue = $this->escaper->unescape($escapedCellValue);
 109          return $cellValue;
 110      }
 111  
 112      /**
 113       * Returns the cell Numeric value from the given node.
 114       *
 115       * @param \DOMNode $node
 116       * @return int|float The value associated with the cell
 117       */
 118      protected function formatFloatCellValue($node)
 119      {
 120          $nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_VALUE);
 121          $nodeIntValue = intval($nodeValue);
 122          $cellValue = ($nodeIntValue == $nodeValue) ? $nodeIntValue : floatval($nodeValue);
 123          return $cellValue;
 124      }
 125  
 126      /**
 127       * Returns the cell Boolean value from the given node.
 128       *
 129       * @param \DOMNode $node
 130       * @return bool The value associated with the cell
 131       */
 132      protected function formatBooleanCellValue($node)
 133      {
 134          $nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_BOOLEAN_VALUE);
 135          // !! is similar to boolval()
 136          $cellValue = !!$nodeValue;
 137          return $cellValue;
 138      }
 139  
 140      /**
 141       * Returns the cell Date value from the given node.
 142       *
 143       * @param \DOMNode $node
 144       * @return \DateTime|null The value associated with the cell or NULL if invalid date value
 145       */
 146      protected function formatDateCellValue($node)
 147      {
 148          try {
 149              $nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_DATE_VALUE);
 150              return new \DateTime($nodeValue);
 151          } catch (\Exception $e) {
 152              return null;
 153          }
 154      }
 155  
 156      /**
 157       * Returns the cell Time value from the given node.
 158       *
 159       * @param \DOMNode $node
 160       * @return \DateInterval|null The value associated with the cell or NULL if invalid time value
 161       */
 162      protected function formatTimeCellValue($node)
 163      {
 164          try {
 165              $nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_TIME_VALUE);
 166              return new \DateInterval($nodeValue);
 167          } catch (\Exception $e) {
 168              return null;
 169          }
 170      }
 171  
 172      /**
 173       * Returns the cell Currency value from the given node.
 174       *
 175       * @param \DOMNode $node
 176       * @return string The value associated with the cell (e.g. "100 USD" or "9.99 EUR")
 177       */
 178      protected function formatCurrencyCellValue($node)
 179      {
 180          $value = $node->getAttribute(self::XML_ATTRIBUTE_VALUE);
 181          $currency = $node->getAttribute(self::XML_ATTRIBUTE_CURRENCY);
 182  
 183          return "$value $currency";
 184      }
 185  
 186      /**
 187       * Returns the cell Percentage value from the given node.
 188       *
 189       * @param \DOMNode $node
 190       * @return int|float The value associated with the cell
 191       */
 192      protected function formatPercentageCellValue($node)
 193      {
 194          // percentages are formatted like floats
 195          return $this->formatFloatCellValue($node);
 196      }
 197  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1