Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
12 / 20 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ColumnStrategy | |
60.00% |
12 / 20 |
|
25.00% |
1 / 4 |
12.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| addHashEntry | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
| toArray | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| generateDuplicateName | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\Export\Extractor\Strategy; |
| 24 | |
| 25 | use oat\taoQtiItem\model\Export\Extractor\HashEntry; |
| 26 | |
| 27 | class ColumnStrategy implements Strategy |
| 28 | { |
| 29 | /** @var array */ |
| 30 | private $dataArray = []; |
| 31 | |
| 32 | /** @var string */ |
| 33 | private $column; |
| 34 | |
| 35 | /** @var boolean */ |
| 36 | private $hasOnlyOneProperty; |
| 37 | |
| 38 | /** @var int */ |
| 39 | private $keyOccurence; |
| 40 | |
| 41 | /** |
| 42 | * @param bool $hasOnlyOneProperty |
| 43 | * @param string $column |
| 44 | */ |
| 45 | public function __construct($hasOnlyOneProperty, $column) |
| 46 | { |
| 47 | $this->hasOnlyOneProperty = $hasOnlyOneProperty; |
| 48 | $this->column = $column; |
| 49 | $this->keyOccurence = 1; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @inheritdoc |
| 54 | */ |
| 55 | public function addHashEntry(HashEntry $hashEntry) |
| 56 | { |
| 57 | $key = $hashEntry->getKey(); |
| 58 | if (array_key_exists($key, $this->dataArray)) { |
| 59 | $newKeyName = $this->generateDuplicateName($key); |
| 60 | $this->dataArray[$newKeyName] = $hashEntry->getValue(); |
| 61 | } else { |
| 62 | $this->dataArray[$key] = $hashEntry->getValue(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @inheritdoc |
| 68 | */ |
| 69 | public function toArray() |
| 70 | { |
| 71 | if (empty($this->dataArray)) { |
| 72 | return []; |
| 73 | } |
| 74 | |
| 75 | if ($this->hasOnlyOneProperty) { |
| 76 | return $this->dataArray; |
| 77 | } |
| 78 | return [ |
| 79 | $this->column => $this->dataArray |
| 80 | ]; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param string $key |
| 85 | * @return string |
| 86 | */ |
| 87 | protected function generateDuplicateName($key) |
| 88 | { |
| 89 | $newKey = $key . ' (' . $this->keyOccurence . ')'; |
| 90 | |
| 91 | if (array_key_exists($newKey, $this->dataArray)) { |
| 92 | $this->keyOccurence++; |
| 93 | return $this->generateDuplicateName($key); |
| 94 | } |
| 95 | |
| 96 | return $newKey; |
| 97 | } |
| 98 | } |