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

NodesCollection::push()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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