Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
DataStorageTrait | |
0.00% |
0 / 41 |
|
0.00% |
0 / 8 |
210 | |
0.00% |
0 / 1 |
getStoredImageUploadIds | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getStoredFileUploadIds | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
replaceImagePaths | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
replaceFilePaths | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
getFileHref | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
getImagesPathForStorage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getFilesPathForStorage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getEntityPathForStorage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Olz\Entity\Common; |
4 | |
5 | use Olz\Utils\WithUtilsTrait; |
6 | |
7 | trait DataStorageTrait { |
8 | use WithUtilsTrait; |
9 | |
10 | /** @return array<non-empty-string> */ |
11 | public function getStoredImageUploadIds(): array { |
12 | $img_path = $this->getImagesPathForStorage(); |
13 | return $this->uploadUtils()->getStoredUploadIds("{$img_path}img/"); |
14 | } |
15 | |
16 | /** @return array<non-empty-string> */ |
17 | public function getStoredFileUploadIds(): array { |
18 | return $this->uploadUtils()->getStoredUploadIds($this->getFilesPathForStorage()); |
19 | } |
20 | |
21 | public function replaceImagePaths(string $html): string { |
22 | $entity_name = $this::getEntityNameForStorage(); |
23 | $entity_id = $this->getEntityIdForStorage(); |
24 | $upload_ids = $this->getStoredImageUploadIds(); |
25 | foreach ($upload_ids as $upload_id) { |
26 | $search = "<img src=\"./{$upload_id}\" alt=\"\" />"; |
27 | $replace = $this->imageUtils()->olzImage($entity_name, $entity_id, $upload_id, 110, 'image'); |
28 | $html = str_replace($search, $replace, $html); |
29 | } |
30 | return $html; |
31 | } |
32 | |
33 | public function replaceFilePaths(string $html): string { |
34 | $data_path = $this->envUtils()->getDataPath(); |
35 | $data_href = $this->envUtils()->getDataHref(); |
36 | $entity_path = $this->getEntityPathForStorage(); |
37 | $upload_ids = $this->getStoredFileUploadIds(); |
38 | foreach ($upload_ids as $upload_id) { |
39 | $file_path = "{$data_path}files/{$entity_path}{$upload_id}"; |
40 | $file_href = "{$data_href}files/{$entity_path}{$upload_id}"; |
41 | $filemtime = is_file($file_path) ? filemtime($file_path) : false; |
42 | $modified = is_int($filemtime) ? date('Y-m-d_H-i-s', $filemtime) : ''; |
43 | $search = "\"./{$upload_id}\""; |
44 | $replace = "\"{$file_href}?modified={$modified}\""; |
45 | $html = str_replace($search, $replace, $html); |
46 | } |
47 | return $html; |
48 | } |
49 | |
50 | public function getFileHref(string $upload_id): string { |
51 | $data_path = $this->envUtils()->getDataPath(); |
52 | $data_href = $this->envUtils()->getDataHref(); |
53 | $entity_path = $this->getEntityPathForStorage(); |
54 | $file_path = "{$data_path}files/{$entity_path}{$upload_id}"; |
55 | $file_href = "{$data_href}files/{$entity_path}{$upload_id}"; |
56 | $filemtime = is_file($file_path) ? filemtime($file_path) : false; |
57 | $modified = is_int($filemtime) ? date('Y-m-d_H-i-s', $filemtime) : ''; |
58 | return "{$file_href}?modified={$modified}"; |
59 | } |
60 | |
61 | /** @return non-empty-string */ |
62 | public function getImagesPathForStorage(): string { |
63 | $data_path = $this->envUtils()->getDataPath(); |
64 | $entity_path = $this->getEntityPathForStorage(); |
65 | return "{$data_path}img/{$entity_path}"; |
66 | } |
67 | |
68 | /** @return non-empty-string */ |
69 | public function getFilesPathForStorage(): string { |
70 | $data_path = $this->envUtils()->getDataPath(); |
71 | $entity_path = $this->getEntityPathForStorage(); |
72 | return "{$data_path}files/{$entity_path}"; |
73 | } |
74 | |
75 | /** @return non-empty-string */ |
76 | public function getEntityPathForStorage(): string { |
77 | $entity_name = $this::getEntityNameForStorage(); |
78 | $entity_id = $this->getEntityIdForStorage(); |
79 | return "{$entity_name}/{$entity_id}/"; |
80 | } |
81 | } |