2018-08-21 11:06:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Traits;
|
|
|
|
|
|
|
|
trait StoreTrait
|
|
|
|
{
|
|
|
|
/** @var Illuminate\Support\Collection Collezione degli oggetti disponibili */
|
|
|
|
protected static $collection = null;
|
|
|
|
/** @var bool Controllo sul salvataggio globale */
|
|
|
|
protected static $all = false;
|
|
|
|
|
2018-09-05 10:36:41 +02:00
|
|
|
/** @var int Identificatore dell'oggetto in utilizzo */
|
|
|
|
protected static $current;
|
|
|
|
|
2018-08-21 11:06:37 +02:00
|
|
|
/** @var string Nome della colonna "id" (Primary Key) */
|
|
|
|
protected static $id = 'id';
|
|
|
|
/** @var string Nome della colonna "name" */
|
|
|
|
protected static $name = 'name';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce tutti gli oggetti.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getAll()
|
|
|
|
{
|
|
|
|
if (!self::$all) {
|
|
|
|
self::$collection = self::all();
|
|
|
|
|
|
|
|
self::$all = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'oggetto relativo all'identificativo specificato.
|
|
|
|
*
|
|
|
|
* @param string|int $identifier
|
|
|
|
*
|
|
|
|
* @return StoreTrait
|
|
|
|
*/
|
|
|
|
public static function get($identifier)
|
|
|
|
{
|
|
|
|
// Inizializzazione
|
|
|
|
if (!isset(self::$collection)) {
|
|
|
|
self::$collection = collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ricerca
|
|
|
|
$result = self::$collection->first(function ($item) use ($identifier) {
|
|
|
|
return $item->{self::$name} == $identifier || $item->{self::$id} == $identifier;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!empty($result)) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consultazione Database
|
|
|
|
$result = self::where(self::$id, $identifier)
|
|
|
|
->orWhere(self::$name, $identifier)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
self::$collection->push($result);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2018-09-05 10:36:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'oggetto attualmente impostato.
|
|
|
|
*
|
|
|
|
* @return StoreTrait
|
|
|
|
*/
|
|
|
|
public static function getCurrent()
|
|
|
|
{
|
2018-09-25 16:47:44 +02:00
|
|
|
if (!isset(self::$current)) {
|
2018-09-19 10:44:32 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:36:41 +02:00
|
|
|
return self::get(self::$current);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imposta il modulo attualmente in utilizzo.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*/
|
|
|
|
public static function setCurrent($id)
|
|
|
|
{
|
|
|
|
self::$current = $id;
|
|
|
|
}
|
2018-08-21 11:06:37 +02:00
|
|
|
}
|