Grido@master
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Grido
    • Components
      • Actions
      • Columns
      • Filters
    • DataSources
    • PropertyAccessors
    • Translations

Classes

  • Grido\Translations\FileTranslator
  1 <?php
  2 
  3 /**
  4  * This file is part of the Grido (https://github.com/o5/grido)
  5  *
  6  * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
  7  *
  8  * For the full copyright and license information, please view
  9  * the file LICENSE.md that was distributed with this source code.
 10  */
 11 
 12 namespace Grido\Components\Columns;
 13 
 14 /**
 15  * Number column.
 16  *
 17  * @package     Grido
 18  * @subpackage  Components\Columns
 19  * @author      Petr Bugyík
 20  *
 21  * @property array $numberFormat
 22  */
 23 class Number extends Editable
 24 {
 25     /** @var array */
 26     protected $numberFormat = array(
 27         self::NUMBER_FORMAT_DECIMALS => 0,
 28         self::NUMBER_FORMAT_DECIMAL_POINT => '.',
 29         self::NUMBER_FORMAT_THOUSANDS_SEPARATOR => ','
 30     );
 31 
 32     /** @const keys of array $numberFormat */
 33     const NUMBER_FORMAT_DECIMALS = 0;
 34     const NUMBER_FORMAT_DECIMAL_POINT = 1;
 35     const NUMBER_FORMAT_THOUSANDS_SEPARATOR = 2;
 36 
 37     /**
 38      * @param \Grido\Grid $grid
 39      * @param string $name
 40      * @param string $label
 41      * @param int $decimals number of decimal points
 42      * @param string $decPoint separator for the decimal point
 43      * @param string $thousandsSep thousands separator
 44      */
 45     public function __construct($grid, $name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL)
 46     {
 47         parent::__construct($grid, $name, $label);
 48 
 49         $this->setNumberFormat($decimals, $decPoint, $thousandsSep);
 50     }
 51 
 52     /**
 53      * Sets number format. Params are same as internal function number_format().
 54      * @param int $decimals number of decimal points
 55      * @param string $decPoint separator for the decimal point
 56      * @param string $thousandsSep thousands separator
 57      * @return Number
 58      */
 59     public function setNumberFormat($decimals = NULL, $decPoint = NULL, $thousandsSep = NULL)
 60     {
 61         if ($decimals !== NULL) {
 62             $this->numberFormat[self::NUMBER_FORMAT_DECIMALS] = (int) $decimals;
 63         }
 64 
 65         if ($decPoint !== NULL) {
 66             $this->numberFormat[self::NUMBER_FORMAT_DECIMAL_POINT] = $decPoint;
 67         }
 68 
 69         if ($thousandsSep !== NULL) {
 70             $this->numberFormat[self::NUMBER_FORMAT_THOUSANDS_SEPARATOR] = $thousandsSep;
 71         }
 72 
 73         return $this;
 74     }
 75 
 76     /**
 77      * @return array
 78      */
 79     public function getNumberFormat()
 80     {
 81         return $this->numberFormat;
 82     }
 83 
 84     /**
 85      * @param mixed $value
 86      * @return string
 87      */
 88     protected function formatValue($value)
 89     {
 90         $value = parent::formatValue($value);
 91 
 92         $decimals = $this->numberFormat[self::NUMBER_FORMAT_DECIMALS];
 93         $decPoint = $this->numberFormat[self::NUMBER_FORMAT_DECIMAL_POINT];
 94         $thousandsSep = $this->numberFormat[self::NUMBER_FORMAT_THOUSANDS_SEPARATOR];
 95 
 96         return is_numeric($value)
 97             ? number_format($value, $decimals, $decPoint, $thousandsSep)
 98             : $value;
 99     }
100 }
101 
Grido@master API documentation generated by ApiGen