Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.16% |
24 / 38 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
| Base | |
63.16% |
24 / 38 |
|
63.64% |
7 / 11 |
46.20 | |
0.00% |
0 / 1 |
| parseData | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| readDataFile | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| getDataAsArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getHash | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getData | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| loadData | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getItemList | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| setItemList | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| fetchId | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| setAccessInstance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| access | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package 115Mandant |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsdldb\File; |
| 9 | |
| 10 | use BO\Zmsdldb\Exception; |
| 11 | |
| 12 | /** |
| 13 | * Common methods shared by access classes |
| 14 | * |
| 15 | */ |
| 16 | abstract class Base |
| 17 | { |
| 18 | protected $data = []; |
| 19 | /** |
| 20 | * lazy loaded item list, use getItemList() to access this |
| 21 | * |
| 22 | * @var \BO\Zmsdldb\Collection\Base $itemList |
| 23 | */ |
| 24 | private $itemList = null; |
| 25 | |
| 26 | /** |
| 27 | * @var String $dataFile |
| 28 | */ |
| 29 | protected $dataFile = ''; |
| 30 | |
| 31 | /** |
| 32 | * @var String $locale Format like 'en' |
| 33 | * |
| 34 | */ |
| 35 | protected $locale = 'de'; |
| 36 | |
| 37 | /** |
| 38 | * @var \BO\Zmsdldb\AbstractAccess $accessInstance |
| 39 | */ |
| 40 | private $accessInstance = null; |
| 41 | |
| 42 | abstract protected function parseData($data); |
| 43 | |
| 44 | public function __construct($dataFile, $locale = "de") |
| 45 | { |
| 46 | $this->dataFile = $dataFile; |
| 47 | $this->locale = $locale; |
| 48 | } |
| 49 | |
| 50 | public function readDataFile() |
| 51 | { |
| 52 | if (empty($this->data)) { |
| 53 | $jsonFile = $this->dataFile; |
| 54 | if (!is_readable($jsonFile)) { |
| 55 | throw new Exception("Cannot read $jsonFile"); |
| 56 | } |
| 57 | $data = json_decode(file_get_contents($jsonFile), true); |
| 58 | if (!$data) { |
| 59 | throw new Exception("Could not decide $jsonFile"); |
| 60 | } |
| 61 | $this->data = $data; |
| 62 | } |
| 63 | return $this->data; |
| 64 | } |
| 65 | |
| 66 | public function getDataAsArray() |
| 67 | { |
| 68 | try { |
| 69 | $data = $this->readDataFile(); |
| 70 | |
| 71 | return $data['data']; |
| 72 | } catch (\Exception $e) { |
| 73 | throw $e; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public function getHash() |
| 78 | { |
| 79 | try { |
| 80 | $data = $this->readDataFile(); |
| 81 | |
| 82 | return $data['hash']; |
| 83 | } catch (\Exception $e) { |
| 84 | throw $e; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public function getData() |
| 89 | { |
| 90 | try { |
| 91 | $data = $this->readDataFile(); |
| 92 | |
| 93 | return $data; |
| 94 | } catch (\Exception $e) { |
| 95 | throw $e; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function loadData() |
| 100 | { |
| 101 | try { |
| 102 | $data = $this->readDataFile(); |
| 103 | $this->itemList = $this->parseData($data); |
| 104 | } catch (\Exception $e) { |
| 105 | throw $e; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public function getItemList() |
| 110 | { |
| 111 | if (null === $this->itemList) { |
| 112 | $this->loadData(); |
| 113 | } |
| 114 | return $this->itemList; |
| 115 | } |
| 116 | |
| 117 | protected function setItemList($list) |
| 118 | { |
| 119 | $this->itemList = $list; |
| 120 | return $this; |
| 121 | } |
| 122 | |
| 123 | public function fetchId($itemId) |
| 124 | { |
| 125 | $itemList = $this->getItemList(); |
| 126 | |
| 127 | if (! $itemId || !$itemList instanceof \BO\Zmsdldb\Collection\Base || !$itemList->offsetExists($itemId)) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return $itemList[$itemId]; |
| 132 | } |
| 133 | |
| 134 | public function setAccessInstance(\BO\Zmsdldb\AbstractAccess $accessInstance) |
| 135 | { |
| 136 | $this->accessInstance = $accessInstance; |
| 137 | } |
| 138 | |
| 139 | public function access() |
| 140 | { |
| 141 | return $this->accessInstance; |
| 142 | } |
| 143 | } |