2018-03-19 15:30:16 +01:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-03-19 15:30:16 +01:00
|
|
|
|
|
|
|
namespace Util;
|
|
|
|
|
2020-01-17 17:39:06 +01:00
|
|
|
use Carbon\Carbon;
|
2019-01-10 20:10:47 +01:00
|
|
|
use Illuminate\Database\Capsule\Manager;
|
|
|
|
|
2018-03-19 15:30:16 +01:00
|
|
|
/**
|
|
|
|
* Classe dedicata alla gestione e all'interpretazione delle stringhe personalizzate.
|
|
|
|
*
|
|
|
|
* @since 2.3
|
|
|
|
*/
|
|
|
|
class Generator
|
|
|
|
{
|
|
|
|
/** @var array Elenco delle varabili da sostituire nel pattern */
|
|
|
|
protected static $replaces = [
|
|
|
|
'YYYY' => [
|
|
|
|
'date' => 'Y',
|
|
|
|
],
|
|
|
|
'yy' => [
|
|
|
|
'date' => 'y',
|
|
|
|
],
|
|
|
|
'm' => [
|
|
|
|
'date' => 'm',
|
|
|
|
],
|
|
|
|
'd' => [
|
|
|
|
'date' => 'd',
|
|
|
|
],
|
|
|
|
'H' => [
|
|
|
|
'date' => 'H',
|
|
|
|
],
|
|
|
|
'i' => [
|
|
|
|
'date' => 'i',
|
|
|
|
],
|
|
|
|
's' => [
|
|
|
|
'date' => 'd',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2018-12-07 12:10:55 +01:00
|
|
|
* Genera un pattern sulla base del precedente.
|
2018-03-19 15:30:16 +01:00
|
|
|
*
|
2018-12-23 14:01:59 +01:00
|
|
|
* @param string $pattern
|
|
|
|
* @param string $last
|
|
|
|
* @param int $quantity
|
|
|
|
* @param array $values
|
2018-03-19 15:30:16 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-01-03 17:09:49 +01:00
|
|
|
public static function generate($pattern, $last = null, $quantity = 1, $values = [], $date = null)
|
2018-03-19 15:30:16 +01:00
|
|
|
{
|
|
|
|
// Costruzione del pattern
|
2020-01-03 17:09:49 +01:00
|
|
|
$result = self::complete($pattern, $values, $date);
|
2018-03-19 15:30:16 +01:00
|
|
|
$length = substr_count($result, '#');
|
|
|
|
|
|
|
|
// Individuazione dei valori precedenti
|
|
|
|
$previous = self::read($pattern, $last);
|
|
|
|
|
|
|
|
$number = 1;
|
|
|
|
if (isset($previous['number'])) {
|
|
|
|
$number = intval($previous['number']) + $quantity;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = preg_replace('/#{1,}/', str_pad($number, $length, '0', STR_PAD_LEFT), $result);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Completa un determinato pattern con le informazioni di base.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-01-03 17:09:49 +01:00
|
|
|
public static function complete($pattern, $values = [], $date = null)
|
2018-03-19 15:30:16 +01:00
|
|
|
{
|
|
|
|
// Costruzione del pattern
|
2020-01-03 17:09:49 +01:00
|
|
|
$replaces = array_merge(self::getReplaces($date), $values);
|
2018-12-07 12:10:55 +01:00
|
|
|
|
|
|
|
$keys = array_keys($replaces);
|
2018-03-19 15:30:16 +01:00
|
|
|
$values = array_column($replaces, 'value');
|
|
|
|
|
2018-12-07 12:10:55 +01:00
|
|
|
$result = str_replace($keys, $values, $pattern);
|
2018-03-19 15:30:16 +01:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce i valori utilizzati sul pattern.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-03 17:09:49 +01:00
|
|
|
public static function read($pattern, $string, $date = null)
|
2018-03-19 15:30:16 +01:00
|
|
|
{
|
|
|
|
// Costruzione del pattern
|
2020-01-03 17:09:49 +01:00
|
|
|
$replaces = self::getReplaces($date);
|
2018-03-19 15:30:16 +01:00
|
|
|
|
|
|
|
$replaces['#'] = [
|
|
|
|
'regex' => '(?<number>[0-9]+)',
|
|
|
|
];
|
|
|
|
|
|
|
|
$values = array_column($replaces, 'regex');
|
|
|
|
|
|
|
|
$pattern = preg_replace('/#{1,}/', '#', $pattern);
|
2019-01-25 11:02:36 +01:00
|
|
|
$pattern = str_replace('\\#', '#', preg_quote($pattern, '/'));
|
2018-03-19 15:30:16 +01:00
|
|
|
$pattern = str_replace(array_keys($replaces), array_values($values), $pattern);
|
|
|
|
|
|
|
|
// Individuazione dei valori
|
2022-03-25 18:47:13 +01:00
|
|
|
preg_match('/^'.$pattern.'/', (string) $string, $m);
|
2018-03-19 15:30:16 +01:00
|
|
|
|
|
|
|
return array_filter($m, 'is_string', ARRAY_FILTER_USE_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'elenco delle variabili da sostituire normalizzato per l'utilizzo.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-03 17:09:49 +01:00
|
|
|
public static function getReplaces($date = null)
|
2018-03-19 15:30:16 +01:00
|
|
|
{
|
|
|
|
$replaces = self::$replaces;
|
|
|
|
|
2020-01-17 17:39:06 +01:00
|
|
|
$date_object = new Carbon($date);
|
2020-01-03 17:09:49 +01:00
|
|
|
|
2018-03-19 15:30:16 +01:00
|
|
|
foreach ($replaces as $key => $value) {
|
|
|
|
if (!isset($replaces[$key]['value'])) {
|
|
|
|
if (isset($replaces[$key]['date'])) {
|
2020-01-17 17:39:06 +01:00
|
|
|
$replaces[$key]['value'] = $date_object->format($replaces[$key]['date']);
|
2018-03-19 15:30:16 +01:00
|
|
|
} else {
|
|
|
|
$replaces[$key]['value'] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($replaces[$key]['regex'])) {
|
|
|
|
$replaces[$key]['regex'] = '(?<'.preg_quote($key).'>.{'.strlen($replaces[$key]['value']).'})';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $replaces;
|
|
|
|
}
|
2018-12-07 12:10:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Interpreta una data specifica per la sostituzione nei pattern.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function dateToPattern($date)
|
|
|
|
{
|
|
|
|
$replaces = self::$replaces;
|
|
|
|
|
|
|
|
$date = strtotime($date);
|
|
|
|
$results = [];
|
|
|
|
|
|
|
|
foreach ($replaces as $key => $value) {
|
|
|
|
if (isset($replaces[$key]['date'])) {
|
|
|
|
$results[$key]['value'] = date($replaces[$key]['date'], $date);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce la maschera specificata per il segmento indicato.
|
|
|
|
*
|
|
|
|
* @param int $id_segment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getMaschera($id_segment)
|
|
|
|
{
|
|
|
|
$database = database();
|
|
|
|
|
|
|
|
$maschera = $database->fetchOne('SELECT pattern FROM zz_segments WHERE id = :id_segment', [
|
|
|
|
':id_segment' => $id_segment,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $maschera['pattern'];
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:09:49 +01:00
|
|
|
public static function getPreviousFrom($maschera, $table, $field, $where = [], $date = null)
|
2019-01-10 20:10:47 +01:00
|
|
|
{
|
|
|
|
$order = static::getMascheraOrder($maschera, $field);
|
|
|
|
|
2020-01-03 17:09:49 +01:00
|
|
|
$maschera = Generator::complete($maschera, [], $date);
|
2019-01-10 20:10:47 +01:00
|
|
|
$maschera = str_replace('#', '%', $maschera);
|
|
|
|
|
2022-10-25 10:00:32 +02:00
|
|
|
$query = Manager::table($table)->select($field)->where($field, 'like', $maschera)->orderByRaw($order);
|
2019-01-10 20:10:47 +01:00
|
|
|
|
|
|
|
foreach ($where as $and) {
|
|
|
|
$query->whereRaw($and);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $query->first();
|
|
|
|
|
2020-09-23 11:47:59 +02:00
|
|
|
return isset($result->{$field}) ? $result->{$field} : null;
|
2019-01-10 20:10:47 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 12:10:55 +01:00
|
|
|
/**
|
|
|
|
* Metodo per l'individuazione del tipo di ordine da impostare per la corretta interpretazione della maschera.
|
|
|
|
* Esempi:
|
|
|
|
* - maschere con testo iniziale (FT-####-YYYY) necessitano l'ordinamento alfabetico
|
|
|
|
* - maschere di soli numeri (####-YYYY) è necessario l'ordinamento numerico forzato.
|
|
|
|
*
|
|
|
|
* @param string $maschera
|
2019-01-10 20:10:47 +01:00
|
|
|
* @param string $field
|
2018-12-07 12:10:55 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-01-10 20:10:47 +01:00
|
|
|
protected static function getMascheraOrder($maschera, $field)
|
2018-12-07 12:10:55 +01:00
|
|
|
{
|
2019-01-10 20:10:47 +01:00
|
|
|
// Query di default
|
|
|
|
$query = $field.' DESC';
|
|
|
|
|
2018-12-07 12:10:55 +01:00
|
|
|
// Estraggo blocchi di caratteri standard
|
2020-09-23 18:36:33 +02:00
|
|
|
preg_match('/[#]+/', $maschera, $matches);
|
2018-12-07 12:10:55 +01:00
|
|
|
|
2020-09-23 18:36:33 +02:00
|
|
|
if (!empty($matches)) {
|
|
|
|
$pos1 = strpos($maschera, (string) $matches[0]);
|
|
|
|
if ($pos1 == 0) {
|
|
|
|
$query = 'CAST('.$field.' AS UNSIGNED) DESC';
|
|
|
|
}
|
2018-12-07 12:10:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2018-03-19 15:30:16 +01:00
|
|
|
}
|