Issues (4)

src/ImportNfe.php (3 issues)

1
<?php
2
3
namespace AwaCode\ImportNfe;
4
5
6
/**
7
 * Class AwaCode ImportNfe
8
 *
9
 * @author Rhyann Carvalhais <https://github.com/rhyannc>
10
 * @package AwaCode\ImportNfe
11
 */
12
abstract class ImportNfe
13
{
14
    /** @var string */
15
    protected $path;
16
17
    /** @var resource */
18
    protected $file;
19
20
    /** @var string */
21
    protected $name;
22
23
    /** @var string */
24
    protected $ext;
25
26
    /** @var array */
27
    protected static $allowTypes = [];
28
29
    /** @var array */
30
    protected static $extensions = [];
31
32
   /**
33
     * @param string $uploadDir
34
     * @param string $fileTypeDir
35
     * @param bool $monthYearPath
36
     * @example $u = new Upload("storage/uploads", "importadas");
37
     */
38
   public function __construct(string $uploadDir, string $fileTypeDir, string $importDir, string $errorDir, bool $monthYearPath = true)
39
    {
40
        $this->dir($uploadDir);
41
        $this->dir("{$uploadDir}/{$importDir}");
42
        $this->dir("{$uploadDir}/{$errorDir}");
43
        $this->dir("{$uploadDir}/{$fileTypeDir}");
44
45
        $this->pathImport = "{$uploadDir}/{$importDir}";
0 ignored issues
show
Bug Best Practice introduced by
The property pathImport does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        $this->pathError = "{$uploadDir}/{$errorDir}";
0 ignored issues
show
Bug Best Practice introduced by
The property pathError does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
        $this->path = "{$uploadDir}/{$fileTypeDir}";
48
49
        if ($monthYearPath) {
50
            $this->path("{$uploadDir}/{$fileTypeDir}");
51
        }
52
    }
53
54
55
    /**
56
     * @param string $name
57
     * @return string
58
     */
59
    protected function name(string $name): string
60
    {
61
        $name = filter_var(mb_strtolower($name), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
0 ignored issues
show
The constant AwaCode\ImportNfe\FILTER...TIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
62
        $formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª';
63
        $replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyrr                                 ';
64
        $name = str_replace(
65
            ["-----", "----", "---", "--"],
66
            "-",
67
            str_replace(" ", "-", trim(strtr(utf8_decode($name), utf8_decode($formats), $replace)))
68
        );
69
70
        $this->name = "{$name}." . $this->ext;
71
72
        if (file_exists("{$this->path}/{$this->name}") && is_file("{$this->path}/{$this->name}")) {
73
            $this->name = "{$name}-" . time() . ".{$this->ext}";
74
        }
75
        return $this->name;
76
    }
77
78
    /**
79
     * @param string $dir
80
     * @param int $mode
81
     */
82
    protected function dir(string $dir, int $mode = 0755): void
83
    {
84
        if (!file_exists($dir) || !is_dir($dir)) {
85
            mkdir($dir, $mode, true);
86
        }
87
    }
88
89
    /**
90
     * @param string $path
91
     */
92
    protected function path(string $path): void
93
    {
94
        list($yearPath, $mothPath) = explode("/", date("Y/m"));
95
96
        $this->dir("{$path}/{$yearPath}");
97
        $this->dir("{$path}/{$yearPath}/{$mothPath}");
98
        $this->path = "{$path}/{$yearPath}/{$mothPath}";
99
    }
100
101
    /**
102
     * @param array $file
103
     */
104
    protected function ext(array $file): void
105
    {
106
        $this->ext = mb_strtolower(pathinfo($file['name'])['extension']);
107
    }
108
109
    /**
110
     * @param $inputName
111
     * @param $files
112
     * @return array
113
     */
114
115
    /**
116
     * @param $inputName
117
     * @param $files
118
     * @return array
119
     */
120
    public function multiple($inputName, $files): array
121
    {
122
        $gbFiles = [];
123
        $gbCount = count($files[$inputName]["name"]);
124
        $gbKeys = array_keys($files[$inputName]);
125
126
        for ($gbLoop = 0; $gbLoop < $gbCount; $gbLoop++):
127
            foreach ($gbKeys as $key):
128
                $gbFiles[$gbLoop][$key] = $files[$inputName][$key][$gbLoop];
129
            endforeach;
130
        endfor;
131
132
        return $gbFiles;
133
    }
134
135
}