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