1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Mappers\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\Interfaces\IFileFormat; |
7
|
|
|
use kalanis\kw_mapper\MapperException; |
8
|
|
|
use kalanis\kw_mapper\Mappers\AMapper; |
9
|
|
|
use kalanis\kw_mapper\Storage\Shared; |
10
|
|
|
use kalanis\kw_mapper\Storage\Storage; |
11
|
|
|
use kalanis\kw_storage\StorageException; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AStorage |
16
|
|
|
* @package kalanis\kw_mapper\Mappers\Storage |
17
|
|
|
* The path is separated |
18
|
|
|
* - storage has first half which is usually static |
19
|
|
|
* - content has second half which can be changed by circumstances |
20
|
|
|
*/ |
21
|
|
|
abstract class AStorage extends AMapper |
22
|
|
|
{ |
23
|
|
|
use Storage\TStorage; |
24
|
|
|
use Shared\TFormat; |
25
|
|
|
|
26
|
22 |
|
public function getAlias(): string |
27
|
|
|
{ |
28
|
22 |
|
return $this->getSource(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param IFileFormat|null $format |
33
|
|
|
* @throws MapperException |
34
|
|
|
* @return array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> |
35
|
|
|
*/ |
36
|
10 |
|
protected function loadFromStorage(?IFileFormat $format = null): array |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
10 |
|
$format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat()); |
40
|
10 |
|
return $format->unpack($this->getStorage()->read($this->getSource())); |
41
|
1 |
|
} catch (StorageException $ex) { |
42
|
1 |
|
throw new MapperException('Unable to read from source', 0, $ex); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array<string|int, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool>>> $content |
48
|
|
|
* @param IFileFormat|null $format |
49
|
|
|
* @throws MapperException |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
8 |
|
protected function saveToStorage(array $content, ?IFileFormat $format = null): bool |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
8 |
|
$format = $format ?: Shared\FormatFiles\Factory::getInstance()->getFormatClass($this->getFormat()); |
56
|
8 |
|
return $this->getStorage()->write($this->getSource(), $format->pack($content)); |
57
|
1 |
|
} catch (StorageException $ex) { |
58
|
1 |
|
throw new MapperException('Unable to write into source', 0, $ex); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|