-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinfo.php
77 lines (60 loc) · 1.75 KB
/
Finfo.php
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
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// finfo
// class with basic logics for managing the finfo extension
final class Finfo extends Root
{
// config
protected static array $config = [
'flag'=>FILEINFO_MIME, // flag lors de l'ouverture
'database'=>null // base de données utilisés
];
// is
// retourne vrai si la resource (php7) ou l'objet (php 8.1) est de type finfo
final public static function is($value):bool
{
return $value instanceof \finfo;
}
// open
// ouvre une resource ou objet finfo
final public static function open(?int $flag=null,?string $database=null)
{
$flag ??= static::$config['flag'];
$database ??= static::$config['database'];
$args = [$flag];
if(is_string($database))
$args[] = $database;
return finfo_open(...$args);
}
// read
// lit le fichier et retourne le mime en string
// retourne null en cas de problème
final public static function read($finfo,string $value,?int $flag=null):?string
{
$return = null;
if(self::is($finfo))
{
$flag ??= FILEINFO_NONE;
$mime = finfo_file($finfo,$value,$flag);
if(is_string($mime))
$return = $mime;
}
return $return;
}
// close
// ferme la resource finfo, n'a pas d'effet en php8
final public static function close($value):bool
{
$return = false;
if(self::is($value))
$return = finfo_close($value);
return $return;
}
}
?>