Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| HasIndexes | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
7 | |
0.00% |
0 / 1 |
| getIndexTable | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| replaceIndexes | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
7 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadataBuilder\Concerns; |
| 3 | |
| 4 | use Apie\Core\Utils\ConverterUtils; |
| 5 | use Apie\Core\ValueObjects\Utils; |
| 6 | use ReflectionClass; |
| 7 | use ReflectionProperty; |
| 8 | |
| 9 | trait HasIndexes |
| 10 | { |
| 11 | abstract public static function getIndexTable(): ReflectionClass; |
| 12 | |
| 13 | /** |
| 14 | * @param array<string, int> $indexes |
| 15 | */ |
| 16 | public function replaceIndexes(array $indexes): void |
| 17 | { |
| 18 | $current = isset($this->_indexes) ? Utils::toArray($this->_indexes) : []; |
| 19 | $offset = 0; |
| 20 | $refProperty = 'ref_' . (new ReflectionClass($this))->getShortName(); |
| 21 | $total = array_reduce( |
| 22 | $indexes, |
| 23 | function (float $carry, float $item) { |
| 24 | return $carry + $item; |
| 25 | }, |
| 26 | 0 |
| 27 | ); |
| 28 | foreach ($indexes as $search => $tf) { |
| 29 | if (isset($current[$offset])) { |
| 30 | if ($current[$offset]->text !== $search || $current[$offset]->tf !== $tf) { |
| 31 | $current[$offset]->text = $search; |
| 32 | $current[$offset]->priority = 1; |
| 33 | $current[$offset]->idf = 1; |
| 34 | $current[$offset]->tf = $tf / $total; |
| 35 | } |
| 36 | } else { |
| 37 | $current[$offset] = static::getIndexTable()->newInstance($search, 1, 1, $tf / $total); |
| 38 | } |
| 39 | |
| 40 | $current[$offset]->$refProperty = $this; |
| 41 | $offset++; |
| 42 | } |
| 43 | for (;$offset < count($current);$offset++) { |
| 44 | $current[$offset]->$refProperty = null; |
| 45 | } |
| 46 | $current = array_slice($current, 0, $offset); |
| 47 | $this->_indexes = ConverterUtils::dynamicCast($current, (new ReflectionProperty($this, '_indexes'))->getType()); |
| 48 | } |
| 49 | } |