Completed
Pull Request — feat/html-splitter (#184)
by Nuno
13:52 queued 09:30
created

NodesCollection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 49
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 4 1
A toArray() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Splitters\HtmlSplitter;
15
16
/**
17
 * @internal
18
 */
19
final class NodesCollection
20
{
21
    /**
22
     * Collection of \Algolia\ScoutExtended\Splitters\HtmlSplitter\NodeCollection
23
     * and int as importance after each\Algolia\ScoutExtended\Splitters\HtmlSplitter\NodeCollection.
24
     *
25
     * @var array
26
     */
27
    private $nodesImportance = [];
28
29
    /**
30
     * String.
31
     */
32
    private const IMPORTANCE = 'importance';
33
34
    /**
35
     * Importance need to be add after to avoid polluted queue.
36
     *
37
     * @param \Algolia\ScoutExtended\Splitters\HtmlSplitter\NodeCollection $nodes
38
     *
39
     * @return void
40
     */
41 5
    public function push(NodeCollection $nodes): void
42
    {
43 5
        $this->nodesImportance[] = $nodes->getNodes();
44 5
        $this->nodesImportance[] = [self::IMPORTANCE => $nodes->importanceWeight($nodes->last(0))];
45 5
    }
46
47
    /**
48
     * Convert to array.
49
     *
50
     * @return array
51
     */
52 5
    public function toArray(): array
53
    {
54 5
        $array = [];
55 5
        foreach ($this->nodesImportance as $nodes) {
56 5
            foreach ($nodes as $node) {
57 5
                if ($node instanceof Node) {
58 5
                    $object[$node->getTag()] = $node->getContent();
59
                } else {
60 5
                    $object[self::IMPORTANCE] = $node;
61 5
                    $array[] = $object;
62 5
                    $object = [];
63
                }
64
            }
65
        }
66
67 5
        return $array;
68
    }
69
}
70