Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.41% covered (warning)
56.41%
44 / 78
42.86% covered (danger)
42.86%
6 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
File
56.41% covered (warning)
56.41%
44 / 78
42.86% covered (danger)
42.86%
6 / 14
157.60
0.00% covered (danger)
0.00%
0 / 1
 create
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 write
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 writeAppend
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 isWriteable
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 read
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 move
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 isReadable
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 name
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 filename
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 extension
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 removeExtension
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 directory
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 parse
81.25% covered (warning)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
9.53
 parseList
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Projom\Util;
6
7use Projom\Util\Dir;
8use Projom\Util\Json;
9use Projom\Util\Yaml;
10
11class File
12{
13    private array $cache = [];
14
15    public static function create(string $fullFilePath): bool
16    {
17        // If the path is pointing at a directory, cant do much.
18        if (is_dir($fullFilePath))
19            return false;
20
21        // If file already exists, no need to create.
22        if (file_exists($fullFilePath))
23            return true;
24
25        $isCreated = touch($fullFilePath);
26        return $isCreated;
27    }
28
29    public static function write(string $fullFilePath, mixed $data, int $flags = LOCK_EX): bool
30    {
31        if (!static::isWriteable($fullFilePath))
32            return false;
33
34        $result = file_put_contents($fullFilePath, $data, $flags);
35        if ($result === false)
36            return false;
37
38        return true;
39    }
40
41    public static function writeAppend(string $fullFilePath, mixed $data): bool
42    {
43        if (!static::isWriteable($fullFilePath))
44            return false;
45
46        $result = file_put_contents($fullFilePath, $data, LOCK_EX | FILE_APPEND);
47        if ($result === false)
48            return false;
49
50        return true;
51    }
52
53    public static function isWriteable(string $fullFilePath): bool
54    {
55        if (!is_file($fullFilePath))
56            return false;
57
58        $isWriteable = is_writeable($fullFilePath);
59        return $isWriteable;
60    }
61
62    public static function read(string $fullFilePath): null|string
63    {
64        if (!static::isReadable($fullFilePath))
65            return null;
66
67        $contents = file_get_contents($fullFilePath);
68        if ($contents === false)
69            return null;
70
71        return $contents;
72    }
73
74    public static function move(string $from, string $to): bool
75    {
76        if (!static::isReadable($from))
77            return false;
78
79        $dir = static::directory($to);
80        if (!Dir::isReadable($dir))
81            return false;
82
83        $isMoved = rename($from, $to);
84        return $isMoved;
85    }
86
87    public static function isReadable(string $fullFilePath): bool
88    {
89        if (!$fullFilePath)
90            return false;
91        if (!is_file($fullFilePath))
92            return false;
93        if (!is_readable($fullFilePath))
94            return false;
95        return true;
96    }
97
98    public static function name(string $fullFilePath): string
99    {
100        $fileName = pathinfo($fullFilePath, PATHINFO_BASENAME);
101        return $fileName;
102    }
103
104    public static function filename(string $fullFilePath): string
105    {
106        $fileName = pathinfo($fullFilePath, PATHINFO_FILENAME);
107        return $fileName;
108    }
109
110    public static function extension(string $fullFilePath): string
111    {
112        $extension = pathinfo($fullFilePath, PATHINFO_EXTENSION);
113        return $extension;
114    }
115
116    public static function removeExtension(string $name): string
117    {
118        $extension = static::extension($name);
119        $fileName = str_replace('.' . $extension, '', $name);
120        return $fileName;
121    }
122
123    public static function directory(string $fullFilePath): string
124    {
125        $directory = dirname($fullFilePath);
126        return $directory;
127    }
128
129    public static function parse(string $fullFilePath, bool $useCache = false): null|array
130    {
131        if (!$fullFilePath)
132            return [];
133
134        if ($useCache)
135            if ($cachedFile = static::$cache[$fullFilePath] ?? false)
136                return $cachedFile;
137
138        $filename = File::name($fullFilePath);
139        $extension = File::extension($filename);
140
141        $parsedFile = match ($extension) {
142            'json' => Json::parseFile($fullFilePath),
143            'yml', 'yaml' => Yaml::parseFile($fullFilePath),
144            'txt' => [static::read($fullFilePath)],
145            default => null,
146        };
147
148        if ($useCache)
149            static::$cache[$fullFilePath] = $parsedFile;
150
151        return $parsedFile;
152    }
153
154    public static function parseList(array $fileList, bool $useCache = false): array
155    {
156        $parsedFileList = [];
157
158        foreach ($fileList as $fullFilePath) {
159
160            $isReadable = static::isReadable($fullFilePath);
161            if (!$isReadable)
162                continue;
163
164            $fileData = static::parse($fullFilePath, $useCache);
165            $fileName = static::filename($fullFilePath);
166            $parsedFileList[$fileName] = $fileData;
167        }
168
169        return $parsedFileList;
170    }
171}