• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sokil / php-vast / 4083975108

pending completion
4083975108

push

github

GitHub
test php 8.2

359 of 375 relevant lines covered (95.73%)

6.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.44
/src/Document.php
1
<?php
2
declare(strict_types=1);
3

4
/**
5
 * This file is part of the PHP-VAST package.
6
 *
7
 * (c) Dmytro Sokil <dmytro.sokil@gmail.com>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12

13
namespace Sokil\Vast;
14

15
use Sokil\Vast\Ad\AbstractAdNode;
16
use Sokil\Vast\Ad\InLine;
17
use Sokil\Vast\Ad\Wrapper;
18
use Sokil\Vast\Document\AbstractNode;
19

20
class Document extends AbstractNode
21
{
22
    /**
23
     * @var \DOMDocument
24
     */
25
    private $domDocument;
26

27
    /**
28
     * @var ElementBuilder
29
     */
30
    private $vastElementBuilder;
31

32
    /**
33
     * Ad node list
34
     *
35
     * @var AbstractAdNode[]
36
     */
37
    private $vastAdNodeList = [];
38

39
    /**
40
     * @param \DOMDocument $DOMDocument
41
     */
42
    public function __construct(\DOMDocument $DOMDocument, ElementBuilder $vastElementBuilder)
43
    {
44
        $this->domDocument = $DOMDocument;
26✔
45
        $this->vastElementBuilder = $vastElementBuilder;
26✔
46
    }
47

48
    /**
49
     * @return \DOMElement
50
     */
51
    protected function getDomElement(): \DOMElement
52
    {
53
        return $this->domDocument->documentElement;
1✔
54
    }
55

56
    /**
57
     * "Magic" method to convert document to string
58
     *
59
     * @return string
60
     */
61
    public function __toString()
62
    {
63
        return $this->domDocument->saveXML();
17✔
64
    }
65

66
    /**
67
     * Get DomDocument object
68
     *
69
     * @return \DomDocument
70
     */
71
    public function toDomDocument(): \DOMDocument
72
    {
73
        return $this->domDocument;
2✔
74
    }
75
    
76
    /**
77
     * Create "Ad" section on "VAST" node
78
     *
79
     * @param string $type
80
     *
81
     * @throws \InvalidArgumentException
82
     *
83
     * @return AbstractAdNode|InLine|Wrapper
84
     */
85
    private function createAdSection($type): AbstractAdNode
86
    {
87
        // Check Ad type
88
        if (!in_array($type, [InLine::TAG_NAME, Wrapper::TAG_NAME])) {
18✔
89
            throw new \InvalidArgumentException(sprintf('Ad type %s not supported', $type));
×
90
        }
91

92
        // create dom node
93
        $adDomElement = $this->domDocument->createElement('Ad');
18✔
94
        $this->domDocument->documentElement->appendChild($adDomElement);
18✔
95

96
        // create type element
97
        $adTypeDomElement = $this->domDocument->createElement($type);
18✔
98
        $adDomElement->appendChild($adTypeDomElement);
18✔
99

100
        // create ad section
101
        switch ($type) {
102
            case InLine::TAG_NAME:
18✔
103
                $adSection = $this->vastElementBuilder->createInLineAdNode($adDomElement);
15✔
104
                break;
15✔
105
            case Wrapper::TAG_NAME:
4✔
106
                $adSection = $this->vastElementBuilder->createWrapperAdNode($adDomElement);
4✔
107
                break;
4✔
108
            default:
109
                throw new \InvalidArgumentException(sprintf('Ad type %s not supported', $type));
×
110
        }
111

112
        // cache
113
        $this->vastAdNodeList[] = $adSection;
18✔
114
        
115
        return $adSection;
18✔
116
    }
117
    
118
    /**
119
     * Create inline Ad section
120
     *
121
     * @return \Sokil\Vast\Ad\InLine
122
     */
123
    public function createInLineAdSection(): InLine
124
    {
125
        return $this->createAdSection(InLine::TAG_NAME);
15✔
126
    }
127
    
128
    /**
129
     * Create Wrapper Ad section
130
     *
131
     * @return \Sokil\Vast\Ad\Wrapper
132
     */
133
    public function createWrapperAdSection(): Wrapper
134
    {
135
        return $this->createAdSection(Wrapper::TAG_NAME);
4✔
136
    }
137

138
    /**
139
     * Get document ad sections
140
     *
141
     * @return AbstractAdNode[]
142
     *
143
     * @throws \Exception
144
     */
145
    public function getAdSections(): array
146
    {
147
        if (!empty($this->vastAdNodeList)) {
2✔
148
            return $this->vastAdNodeList;
1✔
149
        }
150
            
151
        foreach ($this->domDocument->documentElement->childNodes as $adDomElement) {
1✔
152
            // get Ad tag
153
            if (!$adDomElement instanceof \DOMElement) {
1✔
154
                continue;
1✔
155
            }
156

157
            if ('ad' !== strtolower($adDomElement->tagName)) {
1✔
158
                continue;
×
159
            }
160

161
            // get Ad type tag
162
            foreach ($adDomElement->childNodes as $node) {
1✔
163
                if (!($node instanceof \DOMElement)) {
1✔
164
                    continue;
1✔
165
                }
166

167
                $type = $node->tagName;
1✔
168

169
                // create ad section
170
                switch ($type) {
171
                    case InLine::TAG_NAME:
1✔
172
                        $adSection = $this->vastElementBuilder->createInLineAdNode($adDomElement);
1✔
173
                        break;
1✔
174
                    case Wrapper::TAG_NAME:
×
175
                        $adSection = $this->vastElementBuilder->createWrapperAdNode($adDomElement);
×
176
                        break;
×
177
                    default:
178
                        throw new \Exception('Ad type ' . $type . ' not supported');
×
179
                }
180

181
                $this->vastAdNodeList[] = $adSection;
1✔
182
            }
183
        }
184
        
185
        return $this->vastAdNodeList;
1✔
186
    }
187

188
    /**
189
     * Add Error tracking url.
190
     * Allowed multiple error elements.
191
     *
192
     * @param string $url
193
     *
194
     * @return Document
195
     */
196
    public function addErrors(string $url): self
197
    {
198
        $this->addCdataNode('Error', $url);
1✔
199
        return $this;
1✔
200
    }
201

202
    /**
203
     * Get previously set error tracking url value
204
     *
205
     * @return array
206
     */
207
    public function getErrors(): array
208
    {
209
        return $this->getValuesOfArrayNode('Error');
1✔
210
    }
211
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc