Completed
Push — master ( 0d861e...0d54f8 )
by Stefano
03:27
created

URL::__get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 5
rs 9.4285
1
<?php
2
3
/**
4
 * URL
5
 *
6
 * Helper object for handling URLs
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2016 - http://caffeina.com
11
 */
12
13
class URL {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
The property $_origin is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $_parsed is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
14
15
  private   $_origin   = '',
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
16
            $_parsed   = false,
17
            $scheme    = false,
18
            $user      = false,
19
            $pass      = false,
20
            $host      = false,
21
            $port      = false,
22
            $path      = false,
23
            $query     = [],
24
            $fragment  = false;
25
26
  public function __construct($url=''){
27
    if (empty($url) || !is_string($url)) return;
28
    $this->_origin = $url;
29
  }
30
31
  private function parse(){
32
    $url          = $this->_origin;
33
    $tmp_url      = (strpos($url, '://') === false) ? "..N..://$url" : $url;
34
    if (mb_detect_encoding($tmp_url, 'UTF-8', true) || ($parsed = parse_url($tmp_url)) === false) {
35
      preg_match('(^((?P<scheme>[^:/?#]+):(//))?((\\3|//)?(?:(?P<user>[^:]+):(?P<pass>[^@]+)@)?(?P<host>[^/?:#]*))(:(?P<port>\\d+))?(?P<path>[^?#]*)(\\?(?P<query>[^#]*))?(#(?P<fragment>.*))?)u', $tmp_url, $parsed);
36
    }
37
    foreach($parsed as $k => $v) if(isset($this->$k)) $this->$k = $v;
38
    if ($this->scheme == '..N..') $this->scheme = null;
39
    if (!empty($this->query)) {
40
      parse_str($this->query, $this->query);
41
    }
42
    $this->_parsed = true;
43
  }
44
45
  public function & __get($name){
46
    $this->_parsed || $this->parse();
47
    if (isset($this->$name)) return $this->$name;
48
    else throw new Exception('Trying to read an unknown URL property');
49
  }
50
51
  public function __set($name, $value){
52
    $this->_parsed || $this->parse();
53
    return $this->$name = $value;
54
  }
55
56
  public function __toString(){
57
    if ($this->_parsed) {
58
      $d = [];
59
      if ($this->scheme)         $d[] = "{$this->scheme}://";
60
      if ($this->user)           $d[] = "{$this->user}" . (empty($this->pass)?'':":{$this->pass}") . "@";
61
      if ($this->host)           $d[] = "{$this->host}";
62
      if ($this->port)           $d[] = ":{$this->port}";
63
      if ($this->path)           $d[] = "/" . ltrim($this->path,"/");
64
      if (!empty($this->query))  $d[] = "?" . http_build_query($this->query);
65
      if ($this->fragment)       $d[] = "#{$this->fragment}";
66
      return implode('', $d);
67
    } else {
68
      return $this->_origin;
69
    }
70
  }
71
72
} /* End of class */
73