Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
168 righe
4.9 KiB
168 righe
4.9 KiB
<?php |
|
class Integrazione { |
|
public $name = "Integrazione"; |
|
public $settings = []; |
|
public $validation = []; |
|
|
|
const PROTECTED_VARIABLES = ["name", "settings", "validation"]; |
|
const INFO_NAME = "settings_integrazioni"; |
|
|
|
public final function __construct() |
|
{ |
|
$this->loadSettings(); |
|
} |
|
|
|
public function loadSettings() { |
|
if (!empty($_SESSION["ente"])){ |
|
$settings = json_decode($_SESSION["ente"]->getInfo()[self::INFO_NAME]); |
|
foreach ($this as $key => $value) { |
|
if (!in_array($key, self::PROTECTED_VARIABLES)) { |
|
$this->settings[$key] = ""; |
|
} |
|
} |
|
|
|
if (!empty($settings->{$this->name})) { |
|
$keys = array_keys((array) $settings->{$this->name}); |
|
foreach ( $keys as $_key) { |
|
$_value = $settings->{$this->name}->{$_key}; |
|
if (!in_array($_key, self::PROTECTED_VARIABLES)) { |
|
if (isset($settings->{$this->name}->{$_key})) { |
|
$this->settings[$_key] = $_value; |
|
$this->{$_key} = $_value; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* Add content to inferface before body |
|
* @return void |
|
*/ |
|
public function beforeBody() { |
|
return; |
|
} |
|
|
|
/** |
|
* Add content to inferface after body |
|
* @return void |
|
*/ |
|
public function afterBody() { |
|
return; |
|
} |
|
|
|
/** |
|
* Check if setting's configuration is valid |
|
* @return bool |
|
*/ |
|
public function isValidConfiguration() : bool { |
|
if (empty($_SESSION["ente"])) { |
|
return false; |
|
} |
|
$mandatory_settings = (array_filter($this->validation, function($v){return !empty($v["rel"]) && substr($v["rel"],0,1) == "S";})); |
|
$mandatory_settings = array_keys($mandatory_settings); |
|
foreach ($mandatory_settings as $param) { |
|
if (!isset($this->{$param})) { |
|
return false; |
|
} |
|
} |
|
return true; |
|
} |
|
|
|
/** |
|
* convert self name to snake_case |
|
* @return string |
|
*/ |
|
public function snake_case() : string { |
|
return self::to_snake_case($this->slug()); |
|
} |
|
|
|
/** |
|
* convert string to snake_case |
|
* @param string $string |
|
* @return string |
|
*/ |
|
public static function to_snake_case(string $string) : string { |
|
return str_replace("-","_", self::to_slug($string)); |
|
} |
|
|
|
/** |
|
* convert self name to slug |
|
* @return string |
|
*/ |
|
public function slug() : string { |
|
return self::to_slug($this->name); |
|
} |
|
|
|
/** |
|
* convert string to slug |
|
* @param string $string |
|
* @return string |
|
*/ |
|
public static function to_slug(string $string) : string { |
|
$slug = preg_replace('/([a-z])([A-Z])/', '$1-$2', $string); |
|
$slug = strtolower($slug); |
|
$slug = preg_replace('/[^a-z0-9]+/', '-', $slug); |
|
$slug = trim($slug, '-'); |
|
return $slug; |
|
} |
|
|
|
/** |
|
* convert text from slug to pascal_case |
|
* @param string $slug |
|
* @return string |
|
*/ |
|
public static function unslugify(string $slug) : string { |
|
$words = str_replace(['-', '_'], ' ', $slug); |
|
$pascal_case = str_replace(' ', '', ucwords($words)); |
|
return $pascal_case; |
|
} |
|
|
|
/** |
|
* Load integration |
|
* @param array $modulo |
|
* @param Ente $ente |
|
* @return void |
|
*/ |
|
public static function loadIntegration(array $modulo, Ente &$ente) : void { |
|
if (!class_exists($modulo["class"])) { |
|
throw new IntegrationClassAbsence($modulo["class"]); |
|
} |
|
$integration_instance = (new ReflectionClass($modulo["class"]))->newInstance(); |
|
if (!$integration_instance instanceof Integrazione) { |
|
throw new IntegrationClassIncompatible($modulo["class"]); |
|
} |
|
$ente->{str_replace("-","",$integration_instance->snake_case())} = $integration_instance; |
|
} |
|
} |
|
|
|
/** |
|
* Raised when Class is incompatible with Integrazione::class |
|
*/ |
|
class IntegrationClassIncompatible extends Exception { |
|
public function __construct($class = "Class", Throwable $previous = null) { |
|
$code = 1; |
|
$message = "Integration {$class}::class isn't Integration derivated class"; |
|
parent::__construct($message, $code, $previous); |
|
} |
|
|
|
public function __toString() { |
|
return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
|
} |
|
} |
|
|
|
/** |
|
* Raised when Integration derived class isn't loaded |
|
*/ |
|
class IntegrationClassAbsence extends Exception { |
|
public function __construct($class = "Class", Throwable $previous = null) { |
|
$code = 0; |
|
$message = "Integration {$class}::class not loaded"; |
|
parent::__construct($message, $code, $previous); |
|
} |
|
|
|
public function __toString() { |
|
return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
|
} |
|
} |