davide.cucurnia@vola.it
2024-02-05 a4fbcbb07a11122ac35c9d793e0bf8a9ae93efed
commit | author | age
9f6455 1 <?php
DC 2
3 namespace App\Vola\Classes;
4
5 use App\Models\MailTemplate;
6 use Illuminate\Support\Facades\Log;
7
8 class Utils
9 {
10
11     public static function genericAppError($methodName,$msg)
12     {
13         \Log::channel('default')->debug(" * Error [$methodName]:\r".$msg);
14         return [
15             "code" => "internal-error",
16             "desc" => "Error processing request"
17         ];
18     }
19
20     /**
21      * @return void
22      */
23     public static function logResponse($result, $params)
24     {
25         if (isset($result["code"])) {
26             if ($result["code"] != "OK") {
27                 \Log::channel('requests_failed')->debug(" * Error managing request:\r" . print_r($params, 1));
28                 \Log::channel('requests_failed')->debug(" * Response returned:\r" . print_r($result, 1) . str_repeat("***", 30) . "\r");
29
30             } else {
31                 \Log::channel('requests_managed')->debug(print_r($params, 1) . "\r" . str_repeat("***", 30) . "\r");
32             }
33         }
34     }
35
36     /**
37      * @param $filename string nome/path del file da verificare
38      * @param string [optional] $extension estenzione che vogliamo controllare(default: ".pdf")
39      * @param bool [optional] $modify se true modifica l'estenzione del file passato(default: true)
40      */
41     public static function hasExtension(string $filename, $extension = ".pdf", $modify = true)
42     {
43         $file_extension = pathinfo($filename, PATHINFO_EXTENSION);
44
45         if ($file_extension !== $extension && $modify) {
46             if ($file_extension !== ""){
47                $filename = substr($filename, 0, strrpos($filename, "."));
48             }
49             $filename .= $extension;
50         }
51
52         return $filename;
53     }
54
55     public static function convertToXML($data, $root = null)
56     {
57         if ($root !== null && $root !== '<isLogged/>') {
58             $root = "<" . $root . "/>";
59         } else {
60             $root = "<isLogged></isLogged>";
61         }
62
63         $xml = new \SimpleXMLElement($root);
64         self::array_to_xml($data, $xml);
65
66         return $xml->asXML();
67     }
68
69     public static function array_to_xml($data, &$xml)
70     {
71         foreach ($data as $key => $value) {
72             if (is_array($value)) {
73                 $subnode = $xml->addChild($key);
74                 self::array_to_xml($value, $subnode);
75             } else {
76                 $xml->addChild("$key", "$value");
77             }
78         }
79     }
80
81     public static function convertXMLStrToArray(string $xml): array
82     {
83         $xmlObject = @simplexml_load_string($xml);
84         $jsonString = json_encode($xmlObject);
85         return json_decode($jsonString,1);
86     }
87
88     public static function getDomain($url){
89         $pieces = parse_url($url);
90         $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
91         if(preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)){
92             return $regs['domain'];
93         }
94         return FALSE;
95     }
96
97 }
98