1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_menu\MetaSource; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Traits\TToString; |
8
|
|
|
use kalanis\kw_menu\Interfaces\IMetaFileParser; |
9
|
|
|
use kalanis\kw_menu\Interfaces\IMetaSource; |
10
|
|
|
use kalanis\kw_menu\Interfaces\IMNTranslations; |
11
|
|
|
use kalanis\kw_menu\Menu\Menu; |
12
|
|
|
use kalanis\kw_menu\MenuException; |
13
|
|
|
use kalanis\kw_menu\Traits\TLang; |
14
|
|
|
use kalanis\kw_paths\PathsException; |
15
|
|
|
use kalanis\kw_paths\Stuff; |
16
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
17
|
|
|
use kalanis\kw_storage\StorageException; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Storage |
22
|
|
|
* @package kalanis\kw_menu\MetaSource |
23
|
|
|
* Data source is in passed storage |
24
|
|
|
*/ |
25
|
|
|
class Storage implements IMetaSource |
26
|
|
|
{ |
27
|
|
|
use TLang; |
28
|
|
|
use TToString; |
29
|
|
|
|
30
|
|
|
/** @var string[] */ |
31
|
|
|
protected array $key = []; |
32
|
|
|
protected IStorage $storage; |
33
|
|
|
protected IMetaFileParser $parser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param IStorage $storage |
37
|
|
|
* @param IMetaFileParser $parser |
38
|
|
|
* @param IMNTranslations|null $lang |
39
|
|
|
* @param string[] $metaKey |
40
|
|
|
*/ |
41
|
8 |
|
public function __construct(IStorage $storage, IMetaFileParser $parser, ?IMNTranslations $lang = null, array $metaKey = []) |
42
|
|
|
{ |
43
|
8 |
|
$this->setMnLang($lang); |
44
|
8 |
|
$this->storage = $storage; |
45
|
8 |
|
$this->parser = $parser; |
46
|
8 |
|
$this->key = $metaKey; |
47
|
8 |
|
} |
48
|
|
|
|
49
|
3 |
|
public function setSource(array $metaSource): void |
50
|
|
|
{ |
51
|
3 |
|
$this->key = $metaSource; |
52
|
3 |
|
} |
53
|
|
|
|
54
|
2 |
|
public function exists(): bool |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
2 |
|
return $this->storage->exists(Stuff::arrayToPath($this->key)); |
58
|
1 |
|
} catch (StorageException | PathsException $ex) { |
59
|
1 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
public function load(): Menu |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
3 |
|
$pt = Stuff::arrayToPath($this->key); |
67
|
3 |
|
return $this->parser->unpack($this->toString($pt, $this->storage->read($pt))); |
68
|
2 |
|
} catch (StorageException | PathsException | FilesException $ex) { |
69
|
2 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
public function save(Menu $content): bool |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
3 |
|
return $this->storage->write(Stuff::arrayToPath($this->key), $this->parser->pack($content)); |
77
|
1 |
|
} catch (StorageException | PathsException $ex) { |
78
|
1 |
|
throw new MenuException($ex->getMessage(), $ex->getCode(), $ex); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|