1+ <?php
2+
3+ /**
4+ * MIT License
5+ *
6+ * Copyright (c) 2018 Samuel CHEMLA
7+ *
8+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9+ * of this software and associated documentation files (the "Software"), to deal
10+ * in the Software without restriction, including without limitation the rights
11+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+ * copies of the Software, and to permit persons to whom the Software is
13+ * furnished to do so, subject to the following conditions:
14+ *
15+ * The above copyright notice and this permission notice shall be included in all
16+ * copies or substantial portions of the Software.
17+ *
18+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+ * SOFTWARE.
25+ */
26+
27+ namespace PhpBg \DvbPsi \Context ;
28+
29+ use PhpBg \DvbPsi \Exception ;
30+ use PhpBg \DvbPsi \Tables \Nit ;
31+
32+ /**
33+ * Class NitAggregator
34+ * Aggregates all NIT segments
35+ */
36+ class NitAggregator
37+ {
38+ public $ networkId ;
39+
40+ /**
41+ * @var Nit[]
42+ */
43+ public $ segments = [];
44+
45+ protected $ version ;
46+
47+ /**
48+ * Aggregate a new EIT
49+ *
50+ * @param Nit $nit
51+ * @throws Exception
52+ * @return bool Return true if the EIT was unknown and has been aggregated, false otherwise
53+ */
54+ public function add (Nit $ nit ): bool
55+ {
56+ if (!isset ($ this ->networkId )) {
57+ $ this ->networkId = $ nit ->networkId ;
58+ } else {
59+ if ($ this ->networkId !== $ nit ->networkId ) {
60+ throw new Exception ("NIT and aggregator mismatch " );
61+ }
62+ }
63+ if (!isset ($ this ->version ) || $ nit ->versionNumber > $ this ->version || ($ nit ->versionNumber === 0 && $ this ->version === 31 )) {
64+ // Version update (or initial collection of nit)
65+ $ this ->version = $ nit ->versionNumber ;
66+ $ this ->segments = [];
67+ }
68+ if (!isset ($ this ->segments [$ nit ->sectionNumber ])) {
69+ $ this ->segments [$ nit ->sectionNumber ] = $ nit ;
70+ }
71+ if (count ($ this ->segments ) >= $ nit ->lastSectionNumber + 1 ) {
72+ // NIT aggregation is complete
73+ return true ;
74+ }
75+ return false ;
76+ }
77+
78+
79+ public function __toString ()
80+ {
81+ $ str = '' ;
82+ foreach ($ this ->segments as $ segment ) {
83+ echo "{$ segment }\n" ;
84+ }
85+ return $ str ;
86+ }
87+ }
0 commit comments