Passed
Push — master ( e45b53...cf614d )
by Austin
01:43
created

SecurityTxt::removeContacts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * src/SecurityTxt.php
4
 *
5
 * @package     php-security-txt
6
 * @author      Austin Heap <[email protected]>
7
 * @version     v0.3.0
8
 */
9
10
declare(strict_types=1);
11
12
namespace AustinHeap\Security\Txt;
13
14
/**
15
 * SecurityTxt
16
 *
17
 * @link        https://github.com/austinheap/php-security-txt
18
 * @link        https://packagist.org/packages/austinheap/php-security-txt
19
 * @link        https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.SecurityTxt.html
20
 */
21
class SecurityTxt
22
{
23
24
    /**
25
     * Internal version number.
26
     *
27
     * @var string
28
     */
29
    const VERSION               = '0.3.0';
30
31
    /**
32
     * Internal parent object.
33
     *
34
     * @var \AustinHeap\Security\Txt\Writer|\AustinHeap\Security\Txt\Reader
35
     */
36
    private $parent             = null;
37
38
    /**
39
     * Internal Writer object.
40
     *
41
     * @var \AustinHeap\Security\Txt\Writer
42
     */
43
    protected $writer           = null;
44
45
    /**
46
     * Internal Reader object.
47
     *
48
     * @var \AustinHeap\Security\Txt\Reader
49
     */
50
    protected $reader           = null;
51
52
    /**
53
     * Internal text cache.
54
     *
55
     * @var string
56
     */
57
    protected $text             = null;
58
59
    /**
60
     * Enable debug output.
61
     *
62
     * @var bool
63
     */
64
    protected $debug            = false;
65
66
    /**
67
     * Enable built-in comments.
68
     *
69
     * @var bool
70
     */
71
    protected $comments         = true;
72
73
    /**
74
     * The security contact(s).
75
     *
76
     * @var array
77
     */
78
    protected $contacts         = [];
79
80
    /**
81
     * The PGP key file URL.
82
     *
83
     * @var string
84
     */
85
    protected $encryption       = null;
86
87
    /**
88
     * The disclosure policy.
89
     *
90
     * @var string
91
     */
92
    protected $disclosure       = null;
93
94
    /**
95
     * The acknowledgement URL.
96
     *
97
     * @var string
98
     */
99
    protected $acknowledgement  = null;
100
101
    /**
102
     * Create a new SecurityTxt instance.
103
     *
104
     * @return \AustinHeap\Security\Txt\SecurityTxt
105
     */
106
    public function __construct(&$parent = null)
107
    {
108
        if (!defined('PHP_SECURITY_TXT_VERSION'))
109
            define('PHP_SECURITY_TXT_VERSION', self::VERSION);
110
111
        $this->parent = $parent;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Enable the comments flag.
118
     *
119
     * @return \AustinHeap\Security\Txt\SecurityTxt
120
     */
121
    public function enableComments(): SecurityTxt
122
    {
123
        return $this->setComments(true);
124
    }
125
126
    /**
127
     * Disable the comments flag.
128
     *
129
     * @return \AustinHeap\Security\Txt\SecurityTxt
130
     */
131
    public function disableComments(): SecurityTxt
132
    {
133
        return $this->setComments(false);
134
    }
135
136
    /**
137
     * Set the comments flag.
138
     *
139
     * @param  string       $comments
140
     * @return \AustinHeap\Security\Txt\SecurityTxt
141
     */
142
    public function setComments(bool $comments): SecurityTxt
143
    {
144
        $this->comments = $comments;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Get the comments flag.
151
     *
152
     * @return bool
153
     */
154
    public function getComments(): bool
155
    {
156
        return $this->comments;
157
    }
158
159
    /**
160
     * Enable the debug flag.
161
     *
162
     * @return \AustinHeap\Security\Txt\SecurityTxt
163
     */
164
    public function enableDebug(): SecurityTxt
165
    {
166
        return $this->setDebug(true);
167
    }
168
169
    /**
170
     * Disable the debug flag.
171
     *
172
     * @return \AustinHeap\Security\Txt\SecurityTxt
173
     */
174
    public function disableDebug(): SecurityTxt
175
    {
176
        return $this->setDebug(false);
177
    }
178
179
    /**
180
     * Set the debug flag.
181
     *
182
     * @param  bool         $debug
183
     * @return \AustinHeap\Security\Txt\SecurityTxt
184
     */
185
    public function setDebug(bool $debug): SecurityTxt
186
    {
187
        $this->debug = $debug;
188
189
        return $this;
190
    }
191
192
    /**
193
     * Get the debug flag.
194
     *
195
     * @return bool
196
     */
197
    public function getDebug(): bool
198
    {
199
        return $this->debug;
200
    }
201
202
    /**
203
     * Set the text.
204
     *
205
     * @param  string       $text
206
     * @return \AustinHeap\Security\Txt\SecurityTxt
207
     */
208
    public function setText(string $text): SecurityTxt
209
    {
210
        $this->text = $text;
211
212
        return $this;
213
    }
214
215
    /**
216
     * Get the text.
217
     *
218
     * @return string
219
     */
220
    public function getText(): string
221
    {
222
        return $this->text === null ? '' : $this->text;
223
    }
224
225
    /**
226
     * Set the contacts.
227
     *
228
     * @param  array        $contacts
229
     * @return \AustinHeap\Security\Txt\SecurityTxt
230
     */
231
    public function setContacts(array $contacts): SecurityTxt
232
    {
233
        $this->contacts = $contacts;
234
235
        return $this;
236
    }
237
238
    /**
239
     * Get the contacts.
240
     *
241
     * @return array
242
     */
243
    public function getContacts(): array
244
    {
245
        return is_null($this->contacts) ? [] : $this->contacts;
246
    }
247
248
    /**
249
     * Add a contact.
250
     *
251
     * @param  string $contact
252
     * @return \AustinHeap\Security\Txt\SecurityTxt
253
     */
254
    public function addContact(string $contact): SecurityTxt
255
    {
256
        return $this->addContacts([$contact]);
257
    }
258
259
    /**
260
     * Add contacts.
261
     *
262
     * @param  array $contacts
263
     * @return \AustinHeap\Security\Txt\SecurityTxt
264
     */
265
    public function addContacts(array $contacts): SecurityTxt
266
    {
267
        foreach ($contacts as $contact)
268
            $this->contacts[$contact] = true;
269
270
        return $this;
271
    }
272
273
    /**
274
     * Remove a contact.
275
     *
276
     * @param  string $contact
277
     * @return \AustinHeap\Security\Txt\SecurityTxt
278
     */
279
    public function removeContact(string $contact): SecurityTxt
280
    {
281
        $this->removeContacts([$contact]);
282
283
        return $this;
284
    }
285
286
    /**
287
     * Remove contacts.
288
     *
289
     * @param  array $contacts
290
     * @return \AustinHeap\Security\Txt\SecurityTxt
291
     */
292
    public function removeContacts(array $contacts): SecurityTxt
293
    {
294
        foreach ($contacts as $contact)
295
            if (array_key_exists($contact, $this->contacts))
296
                unset($this->contacts[$contact]);
297
298
        return $this;
299
    }
300
301
    /**
302
     * Set the encryption.
303
     *
304
     * @param  string $encryption
305
     * @return \AustinHeap\Security\Txt\SecurityTxt
306
     */
307
    public function setEncryption(string $encryption): SecurityTxt
308
    {
309
        if (filter_var($encryption, FILTER_VALIDATE_URL) === false)
310
            throw new \Exception('Encryption must be a well-formed URL.');
311
312
        $this->encryption = $encryption;
313
314
        return $this;
315
    }
316
317
    /**
318
     * Get the encryption.
319
     *
320
     * @return string
321
     */
322
    public function getEncryption(): string
323
    {
324
        return $this->encryption;
325
    }
326
327
    /**
328
     * Set the disclosure policy.
329
     *
330
     * @param  string $disclosure
331
     * @return \AustinHeap\Security\Txt\SecurityTxt
332
     */
333
    public function setDisclosure(string $disclosure): SecurityTxt
334
    {
335
        if (!in_array(trim(strtolower($disclosure)), ['full', 'partial', 'none']))
336
            throw new \Exception('Disclosure policy must be either "full", "partial", or "none".');
337
338
        $this->disclosure = $disclosure;
339
340
        return $this;
341
    }
342
343
    /**
344
     * Get the disclosure policy.
345
     *
346
     * @return string
347
     */
348
    public function getDisclosure(): string
349
    {
350
        return $this->disclosure;
351
    }
352
353
    /**
354
     * Set the acknowledgement URL.
355
     *
356
     * @param  string $acknowledgement
357
     * @return \AustinHeap\Security\Txt\SecurityTxt
358
     */
359
    public function setAcknowledgement(string $acknowledgement): SecurityTxt
360
    {
361
        if (filter_var($acknowledgement, FILTER_VALIDATE_URL) === false)
362
            throw new \Exception('Acknowledgement must be a well-formed URL.');
363
364
        $this->acknowledgement = $acknowledgement;
365
366
        return $this;
367
    }
368
369
    /**
370
     * Get the acknowledgement URL.
371
     *
372
     * @return string
373
     */
374
    public function getAcknowledgement(): string
375
    {
376
        return $this->acknowledgement;
377
    }
378
379
}
380