Completed
Pull Request — master (#58)
by Sam
05:22
created

Types::isValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[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 Badcow\DNS\Rdata;
15
16
class Types
17
{
18
    /**
19
     * a host address.
20
     */
21
    public const A = 1;
22
23
    /**
24
     * an authoritative name server.
25
     */
26
    public const NS = 2;
27
28
    /**
29
     * a mail destination (OBSOLETE - use MX).
30
     */
31
    public const MD = 3;
32
33
    /**
34
     * a mail forwarder (OBSOLETE - use MX).
35
     */
36
    public const MF = 4;
37
38
    /**
39
     * the canonical name for an alias.
40
     */
41
    public const CNAME = 5;
42
43
    /**
44
     * marks the start of a zone of authority.
45
     */
46
    public const SOA = 6;
47
48
    /**
49
     * a mailbox domain name (EXPERIMENTAL).
50
     */
51
    public const MB = 7;
52
53
    /**
54
     * a mail group member (EXPERIMENTAL).
55
     */
56
    public const MG = 8;
57
58
    /**
59
     * a mail rename domain name (EXPERIMENTAL).
60
     */
61
    public const MR = 9;
62
63
    /**
64
     * a null RR (EXPERIMENTAL).
65
     */
66
    public const NULL = 10;
67
68
    /**
69
     * a well known service description.
70
     */
71
    public const WKS = 11;
72
73
    /**
74
     * a domain name pointer.
75
     */
76
    public const PTR = 12;
77
78
    /**
79
     * host information.
80
     */
81
    public const HINFO = 13;
82
83
    /**
84
     * mailbox or mail list information.
85
     */
86
    public const MINFO = 14;
87
88
    /**
89
     * mail exchange.
90
     */
91
    public const MX = 15;
92
93
    /**
94
     * text strings.
95
     */
96
    public const TXT = 16;
97
98
    /**
99
     * for Responsible Person.
100
     */
101
    public const RP = 17;
102
103
    /**
104
     * for AFS Data Base location.
105
     */
106
    public const AFSDB = 18;
107
108
    /**
109
     * for X.25 PSDN address.
110
     */
111
    public const X25 = 19;
112
113
    /**
114
     * for ISDN address.
115
     */
116
    public const ISDN = 20;
117
118
    /**
119
     * for Route Through.
120
     */
121
    public const RT = 21;
122
123
    /**
124
     * for NSAP address, NSAP style A record.
125
     */
126
    public const NSAP = 22;
127
128
    /**
129
     * for domain name pointer, NSAP style.
130
     */
131
    public const NSAP_PTR = 23;
132
133
    /**
134
     * for security signature.
135
     */
136
    public const SIG = 24;
137
138
    /**
139
     * for security key.
140
     */
141
    public const KEY = 25;
142
143
    /**
144
     * X.400 mail mapping information.
145
     */
146
    public const PX = 26;
147
148
    /**
149
     * Geographical Position.
150
     */
151
    public const GPOS = 27;
152
153
    /**
154
     * IP6 Address.
155
     */
156
    public const AAAA = 28;
157
158
    /**
159
     * Location Information.
160
     */
161
    public const LOC = 29;
162
163
    /**
164
     * Next Domain (OBSOLETE).
165
     */
166
    public const NXT = 30;
167
168
    /**
169
     * Endpoint Identifier.
170
     */
171
    public const EID = 31;
172
173
    /**
174
     * Nimrod Locator.
175
     */
176
    public const NIMLOC = 32;
177
178
    /**
179
     * Server Selection.
180
     */
181
    public const SRV = 33;
182
183
    /**
184
     * ATM Address.
185
     */
186
    public const ATMA = 34;
187
188
    /**
189
     * Naming Authority Pointer.
190
     */
191
    public const NAPTR = 35;
192
193
    /**
194
     * Key Exchanger.
195
     */
196
    public const KX = 36;
197
198
    /**
199
     * CERT.
200
     */
201
    public const CERT = 37;
202
203
    /**
204
     * A6 (OBSOLETE - use AAAA).
205
     */
206
    public const A6 = 38;
207
208
    /**
209
     * DNAME.
210
     */
211
    public const DNAME = 39;
212
213
    /**
214
     * SINK.
215
     */
216
    public const SINK = 40;
217
218
    /**
219
     * OPT.
220
     */
221
    public const OPT = 41;
222
223
    /**
224
     * APL.
225
     */
226
    public const APL = 42;
227
228
    /**
229
     * Delegation Signer.
230
     */
231
    public const DS = 43;
232
233
    /**
234
     * SSH Key Fingerprint.
235
     */
236
    public const SSHFP = 44;
237
238
    /**
239
     * IPSECKEY.
240
     */
241
    public const IPSECKEY = 45;
242
243
    /**
244
     * RRSIG.
245
     */
246
    public const RRSIG = 46;
247
248
    /**
249
     * NSEC.
250
     */
251
    public const NSEC = 47;
252
253
    /**
254
     * DNSKEY.
255
     */
256
    public const DNSKEY = 48;
257
258
    /**
259
     * DHCID.
260
     */
261
    public const DHCID = 49;
262
263
    /**
264
     * NSEC3.
265
     */
266
    public const NSEC3 = 50;
267
268
    /**
269
     * NSEC3PARAM.
270
     */
271
    public const NSEC3PARAM = 51;
272
273
    /**
274
     * TLSA.
275
     */
276
    public const TLSA = 52;
277
278
    /**
279
     * S/MIME cert association.
280
     */
281
    public const SMIMEA = 53;
282
283
    /**
284
     * Host Identity Protocol.
285
     */
286
    public const HIP = 55;
287
288
    /**
289
     * NINFO.
290
     */
291
    public const NINFO = 56;
292
293
    /**
294
     * RKEY.
295
     */
296
    public const RKEY = 57;
297
298
    /**
299
     * Trust Anchor LINK.
300
     */
301
    public const TALINK = 58;
302
303
    /**
304
     * Child DS.
305
     */
306
    public const CDS = 59;
307
308
    /**
309
     * DNSKEY(s) the Child wants reflected in DS.
310
     */
311
    public const CDNSKEY = 60;
312
313
    /**
314
     * OpenPGP Key.
315
     */
316
    public const OPENPGPKEY = 61;
317
318
    /**
319
     * Child-To-Parent Synchronization.
320
     */
321
    public const CSYNC = 62;
322
323
    /**
324
     * message digest for DNS zone.
325
     */
326
    public const ZONEMD = 63;
327
328
    public const SPF = 99;
329
330
    public const UINFO = 100;
331
332
    public const UID = 101;
333
334
    public const GID = 102;
335
336
    public const UNSPEC = 103;
337
338
    public const NID = 104;
339
340
    public const L32 = 105;
341
342
    public const L64 = 106;
343
344
    public const LP = 107;
345
346
    /**
347
     * an EUI-48 address.
348
     */
349
    public const EUI48 = 108;
350
351
    /**
352
     * an EUI-64 address.
353
     */
354
    public const EUI64 = 109;
355
356
    /**
357
     * Transaction Key.
358
     */
359
    public const TKEY = 249;
360
361
    /**
362
     * Transaction Signature.
363
     */
364
    public const TSIG = 250;
365
366
    /**
367
     * incremental transfer.
368
     */
369
    public const IXFR = 251;
370
371
    /**
372
     * transfer of an entire zone.
373
     */
374
    public const AXFR = 252;
375
376
    /**
377
     * mailbox-related RRs (MB, MG or MR).
378
     */
379
    public const MAILB = 253;
380
381
    /**
382
     * mail agent RRs (OBSOLETE - see MX).
383
     */
384
    public const MAILA = 254;
385
386
    /**
387
     * A request for some or all records the server has available.
388
     */
389
    public const ANY = 255;
390
391
    /**
392
     * URI.
393
     */
394
    public const URI = 256;
395
396
    /**
397
     * Certification Authority Restriction.
398
     */
399
    public const CAA = 257;
400
401
    /**
402
     * Application Visibility and Control.
403
     */
404
    public const AVC = 258;
405
406
    /**
407
     * Digital Object Architecture.
408
     */
409
    public const DOA = 259;
410
411
    /**
412
     * Automatic Multicast Tunneling Relay.
413
     */
414
    public const AMTRELAY = 260;
415
416
    /**
417
     * DNSSEC Trust Authorities.
418
     */
419
    public const TA = 32768;
420
421
    /**
422
     * DNSSEC Lookaside Validation (OBSOLETE).
423
     */
424
    public const DLV = 32769;
425
426
    public const RESERVED = 65535;
427
428
    const TYPE_NAMES = [
429
        self::A => 'A',
430
        self::NS => 'NS',
431
        self::MD => 'MD',
432
        self::MF => 'MF',
433
        self::CNAME => 'CNAME',
434
        self::SOA => 'SOA',
435
        self::MB => 'MB',
436
        self::MG => 'MG',
437
        self::MR => 'MR',
438
        self::NULL => 'NULL',
439
        self::WKS => 'WKS',
440
        self::PTR => 'PTR',
441
        self::HINFO => 'HINFO',
442
        self::MINFO => 'MINFO',
443
        self::MX => 'MX',
444
        self::TXT => 'TXT',
445
        self::RP => 'RP',
446
        self::AFSDB => 'AFSDB',
447
        self::X25 => 'X25',
448
        self::ISDN => 'ISDN',
449
        self::RT => 'RT',
450
        self::NSAP => 'NSAP',
451
        self::NSAP_PTR => 'NSAP_PTR',
452
        self::SIG => 'SIG',
453
        self::KEY => 'KEY',
454
        self::PX => 'PX',
455
        self::GPOS => 'GPOS',
456
        self::AAAA => 'AAAA',
457
        self::LOC => 'LOC',
458
        self::NXT => 'NXT',
459
        self::EID => 'EID',
460
        self::NIMLOC => 'NIMLOC',
461
        self::SRV => 'SRV',
462
        self::ATMA => 'ATMA',
463
        self::NAPTR => 'NAPTR',
464
        self::KX => 'KX',
465
        self::CERT => 'CERT',
466
        self::A6 => 'A6',
467
        self::DNAME => 'DNAME',
468
        self::SINK => 'SINK',
469
        self::OPT => 'OPT',
470
        self::APL => 'APL',
471
        self::DS => 'DS',
472
        self::SSHFP => 'SSHFP',
473
        self::IPSECKEY => 'IPSECKEY',
474
        self::RRSIG => 'RRSIG',
475
        self::NSEC => 'NSEC',
476
        self::DNSKEY => 'DNSKEY',
477
        self::DHCID => 'DHCID',
478
        self::NSEC3 => 'NSEC3',
479
        self::NSEC3PARAM => 'NSEC3PARAM',
480
        self::TLSA => 'TLSA',
481
        self::SMIMEA => 'SMIMEA',
482
        self::HIP => 'HIP',
483
        self::NINFO => 'NINFO',
484
        self::RKEY => 'RKEY',
485
        self::TALINK => 'TALINK',
486
        self::CDS => 'CDS',
487
        self::CDNSKEY => 'CDNSKEY',
488
        self::OPENPGPKEY => 'OPENPGPKEY',
489
        self::CSYNC => 'CSYNC',
490
        self::ZONEMD => 'ZONEMD',
491
        self::SPF => 'SPF',
492
        self::UINFO => 'UINFO',
493
        self::UID => 'UID',
494
        self::GID => 'GID',
495
        self::UNSPEC => 'UNSPEC',
496
        self::NID => 'NID',
497
        self::L32 => 'L32',
498
        self::L64 => 'L64',
499
        self::LP => 'LP',
500
        self::EUI48 => 'EUI48',
501
        self::EUI64 => 'EUI64',
502
        self::TKEY => 'TKEY',
503
        self::TSIG => 'TSIG',
504
        self::IXFR => 'IXFR',
505
        self::AXFR => 'AXFR',
506
        self::MAILB => 'MAILB',
507
        self::MAILA => 'MAILA',
508
        self::ANY => 'ANY',
509
        self::URI => 'URI',
510
        self::CAA => 'CAA',
511
        self::AVC => 'AVC',
512
        self::DOA => 'DOA',
513
        self::AMTRELAY => 'AMTRELAY',
514
        self::TA => 'TA',
515
        self::DLV => 'DLV',
516
        self::RESERVED => 'RESERVED',
517
    ];
518
519
    /**
520
     * @param int|string $type
521
     *
522
     * @return bool
523
     */
524 24
    public static function isValid($type): bool
525
    {
526 24
        if (is_int($type)) {
527 5
            return array_key_exists($type, self::TYPE_NAMES);
528
        }
529
530 20
        return in_array($type, self::TYPE_NAMES) || 1 === preg_match('/^TYPE\d+$/', $type);
531
    }
532
533
    /**
534
     * Get the name of an RDATA type. E.g. RecordTypeEnum::getName(6) return 'SOA'.
535
     *
536
     * @param int $type The index of the type
537
     *
538
     * @return string
539
     *
540
     * @throws UnsupportedTypeException
541
     */
542 5
    public static function getName(int $type): string
543
    {
544 5
        if (!self::isValid($type)) {
545 1
            throw new UnsupportedTypeException(sprintf('The integer "%d" does not correspond to a supported type.', $type));
546
        }
547
548 5
        return self::TYPE_NAMES[$type];
549
    }
550
551
    /**
552
     * Return the integer value of an RDATA type. E.g. getTypeFromName('MX') returns 15.
553
     *
554
     * @param string $name The name of the record type, e.g. = 'A' or 'MX' or 'SOA'
555
     *
556
     * @return int
557
     *
558
     * @throws UnsupportedTypeException
559
     */
560 5
    public static function getTypeCode(string $name): int
561
    {
562 5
        $name = strtoupper(trim($name));
563
564 5
        if (false !== $type = array_search($name, self::TYPE_NAMES)) {
565 5
            return (int) $type;
566
        }
567
568
        if (1 === preg_match('/^TYPE(\d+)$/', $name, $matches)) {
569
            return (int) $matches[1];
570
        }
571
572
        throw new UnsupportedTypeException(sprintf('RData type "%s" is not supported.', $name));
573
    }
574
}
575