-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
MediaFilesMeta.php
106 lines (87 loc) · 2.83 KB
/
MediaFilesMeta.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Foundation\Media;
use Atomastic\Macroable\Macroable;
use function arrays;
use function filesystem;
use function flextype;
class MediaFilesMeta
{
use Macroable;
/**
* Update file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
* @param string $value Field value
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function update(string $id, string $field, string $value): bool
{
$fileData = serializers()->yaml()->decode(filesystem()->file($this->getFileMetaLocation($id))->get());
if (arrays($fileData)->has($field)) {
$fileData = arrays($fileData)->set($field, $value);
return (bool) filesystem()->file($this->getFileMetaLocation($id))->put(serializers()->yaml()->encode($fileData->toArray()));
}
return false;
}
/**
* Add file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
* @param string $value Field value
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function add(string $id, string $field, string $value): bool
{
$fileData = serializers()->yaml()->decode(filesystem()->file($this->getFileMetaLocation($id))->get());
if (! arrays($fileData)->has($field)) {
$fileData = arrays($fileData)->set($field, $value);
return (bool) filesystem()->file($this->getFileMetaLocation($id))->put(serializers()->yaml()->encode($fileData->toArray()));
}
return false;
}
/**
* Delete file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function delete(string $id, string $field): bool
{
$fileData = serializers()->yaml()->decode(filesystem()->file($this->getFileMetaLocation($id))->get());
if (arrays($fileData)->has($field)) {
$fileData = arrays($fileData)->delete($field);
return (bool) filesystem()->file($this->getFileMetaLocation($id))->put(serializers()->yaml()->encode($fileData->toArray()));
}
return false;
}
/**
* Get file meta location
*
* @param string $id Unique identifier of the file.
*
* @return string entry file location
*
* @access public
*/
public function getFileMetaLocation(string $id): string
{
return PATH['project'] . '/media/.meta/' . $id . '.yaml';
}
}