Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
DataBag
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 4
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getPageBundle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getObject
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 stashObject
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\NodeData;
5
6use stdClass;
7use Wikimedia\Parsoid\Utils\PHPUtils;
8
9class DataBag {
10    /**
11     * @var NodeData[] A map of node data-object-id ids to data objects.
12     * This map is used during DOM processing to avoid having to repeatedly
13     * json-parse/json-serialize data-parsoid and data-mw attributes.
14     * This map is initialized when a DOM is created/parsed/refreshed.
15     */
16    private $dataObject;
17
18    /** @var int An id counter for this document used for the dataObject map */
19    private $docId;
20
21    /** @var stdClass the page bundle object into which all data-parsoid and data-mw
22     * attributes will be extracted to for pagebundle API requests.
23     */
24    private $pageBundle;
25
26    /**
27     * FIXME: Figure out a decent interface for updating these depths
28     * without needing to import the various util files.
29     *
30     * Map of start/end meta tag tree depths keyed by about id
31     */
32    public array $transclusionMetaTagDepthMap = [];
33
34    public function __construct() {
35        $this->dataObject = [];
36        $this->docId = 0;
37        $this->pageBundle = (object)[
38            "parsoid" => PHPUtils::arrayToObject( [ "counter" => -1, "ids" => [] ] ),
39            "mw" => PHPUtils::arrayToObject( [ "ids" => [] ] )
40        ];
41    }
42
43    /**
44     * Return this document's pagebundle object
45     * @return stdClass
46     */
47    public function getPageBundle(): stdClass {
48        return $this->pageBundle;
49    }
50
51    /**
52     * Get the data object for the node with data-object-id 'docId'.
53     * This will return null if a non-existent docId is provided.
54     *
55     * @param int $docId
56     * @return NodeData|null
57     */
58    public function getObject( int $docId ): ?NodeData {
59        return $this->dataObject[$docId] ?? null;
60    }
61
62    /**
63     * Stash the data and return an id for retrieving it later
64     * @param NodeData $data
65     * @return int
66     */
67    public function stashObject( NodeData $data ): int {
68        $docId = $this->docId++;
69        $this->dataObject[$docId] = $data;
70        return $docId;
71    }
72}