Completed
Push — develop ( f7cbfd...3fc737 )
by Lars
08:51
created

PunycodeWrapper::encode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Pdp;
4
5
use TrueBV\Punycode;
6
7
class PunycodeWrapper
8
{
9
  /**
10
   * @var Punycode
11
   */
12
  private static $punycode;
13
14
  /**
15
   * @var bool
16
   */
17
  private $idnSupport = false;
18
19
  public function __construct()
20
  {
21
    if (function_exists('idn_to_ascii') && function_exists('idn_to_utf8')) {
22
      $this->idnSupport = true;
23
      return;
24
    }
25
26
    if (self::$punycode === null) {
27
      self::$punycode = new Punycode();
28
    }
29
  }
30
31
  /**
32
   * Encode a domain to its Punycode version
33
   *
34
   * @param string $input Domain name in Unicode to be encoded
35
   *
36
   * @return string Punycode representation in ASCII
37
   */
38
  public function encode($input)
39
  {
40
    if ($this->idnSupport === true) {
41
      return idn_to_ascii($input);
42
    }
43
44
    return self::$punycode->encode($input);
45
  }
46
47
  /**
48
   * Decode a Punycode domain name to its Unicode counterpart
49
   *
50
   * @param string $input Domain name in Punycode
51
   *
52
   * @return string Unicode domain name
53
   */
54
  public function decode($input)
55
  {
56
    if ($this->idnSupport === true) {
57
      return idn_to_utf8($input);
58
    }
59
60
    return self::$punycode->decode($input);
61
  }
62
}