2017-08-04 16:28:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Intl;
|
|
|
|
|
|
|
|
use DateTime;
|
2017-09-19 16:20:44 +02:00
|
|
|
use NumberFormatter;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
/**
|
2017-08-31 11:32:49 +02:00
|
|
|
* Classe per gestire la formattazione di date e numeri in convenzioni differenti.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @since 2.3
|
|
|
|
*/
|
|
|
|
class Formatter
|
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
protected static $standards = [
|
|
|
|
'timestamp' => 'Y-m-d H:i:s',
|
|
|
|
'date' => 'Y-m-d',
|
|
|
|
'time' => 'H:i:s',
|
2017-09-20 12:14:33 +02:00
|
|
|
'number' => [
|
|
|
|
'decimals' => '.',
|
|
|
|
'thousands' => '',
|
|
|
|
],
|
2017-09-19 16:20:44 +02:00
|
|
|
];
|
|
|
|
|
2017-09-20 12:14:33 +02:00
|
|
|
/** @var NumberFormatter|array Oggetto dedicato alla formattazione dei numeri */
|
2017-09-19 16:20:44 +02:00
|
|
|
protected $numberFormatter;
|
2017-09-20 12:14:33 +02:00
|
|
|
protected $precision;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-20 10:26:15 +02:00
|
|
|
/** @var string Pattern per i timestamp */
|
|
|
|
protected $timestampPattern;
|
2017-08-31 11:32:49 +02:00
|
|
|
/** @var string Pattern per le date */
|
2017-08-04 16:28:16 +02:00
|
|
|
protected $datePattern;
|
2017-08-31 11:32:49 +02:00
|
|
|
/** @var string Pattern per gli orari */
|
2017-08-04 16:28:16 +02:00
|
|
|
protected $timePattern;
|
|
|
|
|
2018-07-19 09:58:28 +02:00
|
|
|
/** @var array Pattern per SQL */
|
|
|
|
protected $sql = [];
|
|
|
|
|
2017-09-20 12:14:33 +02:00
|
|
|
public function __construct($locale, $timestamp = null, $date = null, $time = null, $number = [])
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-20 12:14:33 +02:00
|
|
|
if (class_exists('NumberFormatter')) {
|
|
|
|
$this->numberFormatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
|
|
|
|
} else {
|
|
|
|
$this->numberFormatter = $number;
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
$this->setTimestampPattern($timestamp);
|
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
$this->setDatePattern($date);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
$this->setTimePattern($time);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Restituisce gli elementi di separazione secondo la formattazione in utilizzo.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-05-12 10:50:51 +02:00
|
|
|
public static function getStandardFormats()
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return static::$standards;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 10:01:07 +02:00
|
|
|
/**
|
|
|
|
* Restituisce gli elementi di separazione secondo la formattazione in utilizzo.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function format($value)
|
|
|
|
{
|
|
|
|
if (!empty($value)) {
|
|
|
|
if ($this->isStandardDate($value)) {
|
|
|
|
$value = $this->formatDate($value);
|
|
|
|
} elseif ($this->isStandardTime($value)) {
|
|
|
|
$value = $this->formatTime($value);
|
|
|
|
} elseif ($this->isStandardTimestamp($value)) {
|
|
|
|
$value = $this->formatTimestamp($value);
|
|
|
|
} elseif ($this->isStandardNumber($value)) {
|
|
|
|
$value = $this->formatNumber($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce gli elementi di separazione secondo la formattazione in utilizzo.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function parse($value)
|
|
|
|
{
|
|
|
|
if (!empty($value)) {
|
|
|
|
if ($this->isFormattedDate($value)) {
|
|
|
|
$value = $this->parseDate($value);
|
|
|
|
} elseif ($this->isFormattedTime($value)) {
|
|
|
|
$value = $this->parseTime($value);
|
|
|
|
} elseif ($this->isFormattedTimestamp($value)) {
|
|
|
|
$value = $this->parseTimestamp($value);
|
|
|
|
} elseif ($this->isFormattedNumber($value)) {
|
|
|
|
$value = $this->parseNumber($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
// Gestione della conversione dei numeri
|
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Converte un numero da una formattazione all'altra.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
2018-06-29 13:35:39 +02:00
|
|
|
* @param int $decimals
|
2017-09-19 16:20:44 +02:00
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return string|bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function formatNumber($value, $decimals = null)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$value = trim($value);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-21 10:01:07 +02:00
|
|
|
if (isset($decimals)) {
|
|
|
|
$original = $this->getPrecision();
|
|
|
|
$this->setPrecision($decimals);
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-21 10:01:07 +02:00
|
|
|
if (is_object($this->numberFormatter)) {
|
2017-09-20 12:14:33 +02:00
|
|
|
$result = $this->numberFormatter->format($value);
|
|
|
|
} else {
|
2018-05-12 10:50:51 +02:00
|
|
|
$number = number_format($value, $this->getPrecision(), self::getStandardFormats()['number']['decimals'], self::getStandardFormats()['number']['thousands']);
|
2017-09-20 12:14:33 +02:00
|
|
|
|
2018-05-12 10:50:51 +02:00
|
|
|
$result = $this->customNumber($number, self::getStandardFormats()['number'], $this->getNumberSeparators());
|
2017-09-19 16:20:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 10:01:07 +02:00
|
|
|
if (isset($decimals)) {
|
|
|
|
$this->setPrecision($original);
|
|
|
|
}
|
|
|
|
|
2017-09-19 18:14:23 +02:00
|
|
|
return is_numeric($value) ? $result : false;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Converte un numero da una formattazione all'altra.
|
|
|
|
*
|
|
|
|
* @param string $value
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return float|bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function parseNumber($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2018-06-23 15:41:32 +02:00
|
|
|
$sign = null;
|
2017-09-22 16:50:40 +02:00
|
|
|
if ($value[0] == '+' || $value[0] == '-') {
|
|
|
|
$sign = $value[0];
|
|
|
|
$value = substr($value, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Controllo sull'effettiva natura del numero
|
2017-10-12 20:10:27 +02:00
|
|
|
$number = str_replace(array_values($this->getNumberSeparators()), '', $value);
|
|
|
|
|
|
|
|
$pieces = explode($this->getNumberSeparators()['decimals'], $value);
|
|
|
|
$integer = str_replace(array_values($this->getNumberSeparators()), '', $pieces[0]);
|
|
|
|
|
|
|
|
if (!ctype_digit($number) || (strlen($integer) != strlen((int) $integer))) {
|
2017-09-22 16:50:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = $sign.$value;
|
|
|
|
|
2017-09-20 12:14:33 +02:00
|
|
|
if (is_object($this->numberFormatter)) {
|
2018-10-15 14:29:19 +02:00
|
|
|
$result = $this->numberFormatter->parse($value, NumberFormatter::PATTERN_DECIMAL);
|
2017-09-20 12:14:33 +02:00
|
|
|
} else {
|
2018-05-12 10:50:51 +02:00
|
|
|
$result = $this->customNumber($value, $this->getNumberSeparators(), self::getStandardFormats()['number']);
|
2017-09-20 12:14:33 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 16:36:43 +02:00
|
|
|
$result = is_numeric($result) ? floatval($result) : false;
|
2017-09-21 10:01:07 +02:00
|
|
|
|
2017-09-20 12:14:33 +02:00
|
|
|
return $result;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Controlla se l'elemento indicato è un numero.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
2017-09-19 16:20:44 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function isStandardNumber($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = $this->formatNumber($value);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-24 12:10:22 +02:00
|
|
|
return !is_bool($result);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Controlla se l'elemento indicato è un numero.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function isFormattedNumber($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = $this->parseNumber($value);
|
|
|
|
|
2017-09-24 12:10:22 +02:00
|
|
|
return !is_bool($result);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Imposta la precisione di default per i numeri da formattare.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @param int $decimals
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function setPrecision($decimals)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-20 12:14:33 +02:00
|
|
|
if (is_object($this->numberFormatter)) {
|
|
|
|
$this->numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $decimals);
|
|
|
|
} else {
|
|
|
|
$this->precision = $decimals;
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 10:01:07 +02:00
|
|
|
/**
|
|
|
|
* Restituisce la precisione di default per i numeri da formattare.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPrecision()
|
|
|
|
{
|
|
|
|
return is_object($this->numberFormatter) ? $this->numberFormatter->getAttribute(NumberFormatter::FRACTION_DIGITS) : $this->precision;
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Restituisce gli elementi di separazione secondo la formattazione in utilizzo.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @return array
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function getNumberSeparators()
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return [
|
2017-09-20 12:14:33 +02:00
|
|
|
'decimals' => is_object($this->numberFormatter) ? $this->numberFormatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL) : $this->numberFormatter['decimals'],
|
|
|
|
'thousands' => is_object($this->numberFormatter) ? $this->numberFormatter->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL) : $this->numberFormatter['thousands'],
|
2017-09-19 16:20:44 +02:00
|
|
|
];
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 12:14:33 +02:00
|
|
|
/**
|
|
|
|
* Converte l'elemento in una rappresentazione numerica.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return string|bool
|
2017-09-20 12:14:33 +02:00
|
|
|
*/
|
|
|
|
public function customNumber($value, $current, $format)
|
|
|
|
{
|
|
|
|
$value = trim($value);
|
|
|
|
|
|
|
|
if (strlen($value) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sign = null;
|
|
|
|
if ($value[0] == '+' || $value[0] == '-') {
|
|
|
|
$sign = $value[0];
|
|
|
|
$value = trim(substr($value, 1));
|
|
|
|
} elseif (!is_numeric($value[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($value) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pieces = explode($current['decimals'], $value);
|
|
|
|
if (count($pieces) > 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$integer = $pieces[0];
|
|
|
|
$decimal = (isset($pieces[1])) ? $pieces[1] : null;
|
|
|
|
|
|
|
|
if (!empty($current['thousands'])) {
|
|
|
|
$error = true;
|
|
|
|
if (floor(strlen($integer) / 4) == substr_count($integer, $current['thousands'])) {
|
|
|
|
$values = str_split(strrev($integer), 4);
|
|
|
|
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
if (strlen($value) == 4 && ends_with($value, $current['thousands'])) {
|
|
|
|
$values[$key] = substr($value, 0, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$integer = strrev(implode($values));
|
|
|
|
|
|
|
|
$error = substr_count($integer, $current['thousands']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctype_digit($integer) || (strlen($integer) != strlen((int) $integer)) || (isset($decimal) && !ctype_digit($decimal))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $sign.number_format($integer, 0, '', $format['thousands']);
|
|
|
|
|
|
|
|
if (isset($decimal)) {
|
|
|
|
$result .= $format['decimals'].$decimal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
// Gestione della conversione dei timestamp
|
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Converte un timestamp da una formattazione all'altra.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return string|bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function formatTimestamp($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$object = DateTime::createFromFormat(static::$standards['timestamp'], $value);
|
|
|
|
$result = is_object($object) ? $object->format($this->getTimestampPattern()) : false;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
return $result;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Converte un timestamp da una formattazione all'altra.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return string|bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function parseTimestamp($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$object = DateTime::createFromFormat($this->getTimestampPattern(), $value);
|
|
|
|
$result = is_object($object) ? $object->format(static::$standards['timestamp']) : false;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
return $result;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Controlla se l'elemento indicato è un timestamp.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function isStandardTimestamp($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = $this->formatTimestamp($value);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-24 12:10:22 +02:00
|
|
|
return !is_bool($result);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Controlla se l'elemento indicato è un timestamp.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function isFormattedTimestamp($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = $this->parseTimestamp($value);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-24 12:10:22 +02:00
|
|
|
return !is_bool($result);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Restituisce il formato del timestamp.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @return string
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function getTimestampPattern($type = null)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
return $this->timestampPattern;
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
/**
|
|
|
|
* Imposta il formato del timestamp.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function setTimestampPattern($pattern)
|
|
|
|
{
|
|
|
|
return $this->timestampPattern = $pattern;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
// Gestione della conversione delle date
|
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Converte una data da una formattazione all'altra.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return string|bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function formatDate($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$object = DateTime::createFromFormat(static::$standards['date'], $value);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
// Fallback per la gestione dei timestamp
|
|
|
|
$object = !is_object($object) ? DateTime::createFromFormat(static::$standards['timestamp'], $value) : $object;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = is_object($object) ? $object->format($this->getDatePattern()) : false;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Converte una data da una formattazione all'altra.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return string|bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function parseDate($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$object = DateTime::createFromFormat($this->getDatePattern(), $value);
|
|
|
|
$result = is_object($object) ? $object->format(static::$standards['date']) : false;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Controlla se l'elemento indicato è una data.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @return bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function isStandardDate($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = $this->formatDate($value);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-24 12:10:22 +02:00
|
|
|
return !is_bool($result);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Controlla se l'elemento indicato è una data.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @param string $value
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @return bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function isFormattedDate($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = $this->parseDate($value);
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-24 12:10:22 +02:00
|
|
|
return !is_bool($result);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Restituisce il formato del timestamp.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function getDatePattern($type = null)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-20 10:26:15 +02:00
|
|
|
return $this->datePattern;
|
2017-09-19 16:20:44 +02:00
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
/**
|
|
|
|
* Imposta il formato del timestamp.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function setDatePattern($pattern)
|
|
|
|
{
|
2017-09-20 10:26:15 +02:00
|
|
|
return $this->datePattern = $pattern;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
// Gestione della conversione degli orarii
|
|
|
|
|
2017-08-04 16:28:16 +02:00
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Converte un orario da una formattazione all'altra.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @param string $value
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return string|bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function formatTime($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$object = DateTime::createFromFormat(static::$standards['time'], $value);
|
|
|
|
|
|
|
|
// Fallback per la gestione dei timestamp
|
|
|
|
$object = !is_object($object) ? DateTime::createFromFormat(static::$standards['timestamp'], $value) : $object;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = is_object($object) ? $object->format($this->getTimePattern()) : false;
|
|
|
|
|
|
|
|
return $result;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converte un orario da una formattazione all'altra.
|
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @param string $value
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2018-06-26 16:33:15 +02:00
|
|
|
* @return string|bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function parseTime($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$object = DateTime::createFromFormat($this->getTimePattern(), $value);
|
|
|
|
$result = is_object($object) ? $object->format(static::$standards['time']) : false;
|
2017-08-04 16:28:16 +02:00
|
|
|
|
2017-09-19 16:20:44 +02:00
|
|
|
return $result;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Controlla se l'elemento indicato è un orario.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @param string $value
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @return bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function isStandardTime($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = $this->formatTime($value);
|
|
|
|
|
2017-09-24 12:10:22 +02:00
|
|
|
return !is_bool($result);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Controlla se l'elemento indicato è un orario.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @param string $value
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
2017-09-19 16:20:44 +02:00
|
|
|
* @return bool
|
2017-08-04 16:28:16 +02:00
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function isFormattedTime($value)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-19 16:20:44 +02:00
|
|
|
$result = $this->parseTime($value);
|
|
|
|
|
2017-09-24 12:10:22 +02:00
|
|
|
return !is_bool($result);
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Restituisce il formato del orario.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function getTimePattern($type = null)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-20 10:26:15 +02:00
|
|
|
return $this->timePattern;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-19 16:20:44 +02:00
|
|
|
* Imposta il formato del orario.
|
2017-08-04 16:28:16 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 16:20:44 +02:00
|
|
|
public function setTimePattern($pattern)
|
2017-08-04 16:28:16 +02:00
|
|
|
{
|
2017-09-20 10:26:15 +02:00
|
|
|
return $this->timePattern = $pattern;
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|
2018-07-19 09:58:28 +02:00
|
|
|
|
|
|
|
public function getSQLPatterns()
|
|
|
|
{
|
|
|
|
if (empty($this->sql)) {
|
|
|
|
$formats = [
|
|
|
|
'd' => '%d',
|
|
|
|
'm' => '%m',
|
|
|
|
'Y' => '%Y',
|
|
|
|
'H' => '%H',
|
|
|
|
'i' => '%i',
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = [
|
|
|
|
'timestamp' => $this->getTimestampPattern(),
|
|
|
|
'date' => $this->getDatePattern(),
|
|
|
|
'time' => $this->getTimePattern(),
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($result as $key => $value) {
|
|
|
|
$result[$key] = replace($value, $formats);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sql = $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sql;
|
|
|
|
}
|
2017-08-04 16:28:16 +02:00
|
|
|
}
|