Completed
Push — master ( e53be3...abc481 )
by Lars
03:12
created

Parser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 90
ccs 22
cts 23
cp 0.9565
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
B parseUrl() 0 27 5
A doParseUrl() 0 17 2
1
<?php
2
3
/*
4
 * This file is part of the Purl package, a project by Jonathan H. Wage.
5
 *
6
 * (c) 2013 Jonathan H. Wage
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Purl;
13
14
use Pdp\Parser as PslParser;
15
16
/**
17
 * Parser class.
18
 *
19
 * @author      Jonathan H. Wage <[email protected]>
20
 */
21
class Parser implements ParserInterface
22
{
23
24
  /**
25
   * @var PslParser Public Suffix List parser
26
   */
27
  private static $pslParser = null;
28
29
  private static $defaultParts = [
30
      'scheme'             => null,
31
      'host'               => null,
32
      'port'               => null,
33
      'user'               => null,
34
      'pass'               => null,
35
      'path'               => null,
36
      'query'              => null,
37
      'fragment'           => null,
38
      'publicSuffix'       => null,
39
      'registerableDomain' => null,
40
      'subdomain'          => null,
41
      'canonical'          => null,
42
      'resource'           => null,
43
  ];
44
45
  /**
46
   * Public constructor
47
   *
48
   * @param PslParser $pslParser Public Suffix List parser
49
   */
50 61
  public function __construct(PslParser $pslParser)
51
  {
52 61
    if (self::$pslParser === null) {
53 1
      self::$pslParser = $pslParser;
54
    }
55 61
  }
56
57
  /**
58
   * @inheritDoc
59
   */
60 61
  public function parseUrl($url)
61
  {
62 61
    $url = (string)$url;
63
64 61
    $parsedUrl = $this->doParseUrl($url);
65
66 60
    if ($parsedUrl === false) {
67
      throw new \InvalidArgumentException(sprintf('Invalid url %s', $url));
68
    }
69
70 60
    $parsedUrl = array_merge(self::$defaultParts, $parsedUrl);
71
72 60
    if (isset($parsedUrl['host'])) {
73 59
      $parsedUrl['publicSuffix'] = self::$pslParser->getPublicSuffix($parsedUrl['host']);
74 59
      $parsedUrl['registerableDomain'] = self::$pslParser->getRegistrableDomain($parsedUrl['host']);
75 59
      $parsedUrl['subdomain'] = self::$pslParser->getSubdomain($parsedUrl['host']);
76 59
      $parsedUrl['canonical'] = implode('.', array_reverse(explode('.', $parsedUrl['host']))) . ($parsedUrl['path'] ?? '') . (isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '');
77
78 59
      $parsedUrl['resource'] = $parsedUrl['path'] ?? '';
79
80 59
      if (isset($parsedUrl['query'])) {
81 6
        $parsedUrl['resource'] .= '?' . $parsedUrl['query'];
82
      }
83
    }
84
85 60
    return $parsedUrl;
86
  }
87
88
  /**
89
   * @param string $url
90
   *
91
   * @return array $parsedUrl
92
   */
93 61
  protected function doParseUrl(string $url): array
94
  {
95
    // If there's a single leading forward slash, use parse_url()
96
    // Expected matches:
97
    //
98
    // "/one/two"   YES
99
    // "/"          YES PLEASE
100
    // "//"         NO
101
    // "//one/two"  NO
102
    // ""           HELL NO
103 61
    if (preg_match('#^[\/]([^\/]|$)#', $url) === 1) {
104 2
      return parse_url($url);
105
    }
106
107
    // Otherwise use the PSL parser
108 60
    return self::$pslParser->parseUrl($url)->toArray();
109
  }
110
}
111