193 lines
4.1 KiB
PHP
Executable File
193 lines
4.1 KiB
PHP
Executable File
<?php
|
|
|
|
class Url {
|
|
|
|
static private $properties = array(
|
|
'scheme' => '',
|
|
'host' => '',
|
|
'port' => '',
|
|
'path' => '',
|
|
'dirname' => '',
|
|
'basename' => '',
|
|
'filename' => '',
|
|
'extension' => '',
|
|
'fragment' => '',
|
|
'query' => '',
|
|
);
|
|
|
|
static public function get ( $url = '' ) {
|
|
|
|
return new self($url);
|
|
|
|
}
|
|
|
|
static public function parse ( $url ) {
|
|
|
|
$urlData = parse_url($url);
|
|
$urlData = array_merge(self::$properties, $urlData, pathinfo($urlData['path']));
|
|
|
|
if ( isset($urlData['query']) )
|
|
parse_str($urlData['query'], $urlData['query']);
|
|
|
|
return $urlData;
|
|
|
|
}
|
|
|
|
static public function build ( $urlData ) {
|
|
|
|
if ( isset($urlData['query']) )
|
|
$urlData['query'] = http_build_str($urlData['query']);
|
|
|
|
return http_build_url($urlData);
|
|
|
|
}
|
|
|
|
static public function override ( $urlData, $part, $value ) {
|
|
|
|
$urlData[$part] = $value;
|
|
$update = 0;
|
|
|
|
switch ( $part ) {
|
|
|
|
case 'path':
|
|
$update = 1;
|
|
break;
|
|
|
|
case 'dirname':
|
|
$urlData['path'] = preg_replace('!//++!', '/', $urlData['dirname'] . '/' . $urlData['basename']);
|
|
break;
|
|
|
|
case 'basename':
|
|
$urlData['path'] = preg_replace('!//++!', '/', $urlData['dirname'] . '/' . $urlData['basename']);
|
|
$update = 1;
|
|
break;
|
|
|
|
case 'filename':
|
|
$urlData['path'] = preg_replace('!//++!', '/', $urlData['dirname'] . '/' . $urlData['filename'] . '.' . $urlData['extension']);
|
|
$urlData['basename'] = $urlData['filename'] . ( $urlData['extension'] ? '.' . $urlData['extension'] : '' );
|
|
break;
|
|
|
|
case 'extension':
|
|
$urlData['path'] = preg_replace('!//++!', '/', $urlData['dirname'] . '/' . $urlData['filename'] . '.' . $urlData['extension']);
|
|
$urlData['basename'] = $urlData['filename'] . ( $urlData['extension'] ? '.' . $urlData['extension'] : '' );
|
|
break;
|
|
|
|
}
|
|
|
|
if ( $update ) {
|
|
|
|
$urlData['dirname'] = '';
|
|
$urlData['basename'] = '';
|
|
$urlData['filename'] = '';
|
|
$urlData['extension'] = '';
|
|
|
|
$urlData = array_merge($urlData, pathinfo($urlData['path']));
|
|
|
|
}
|
|
|
|
return $urlData;
|
|
|
|
}
|
|
|
|
private $data = array();
|
|
|
|
public function __construct ( $urlData = array() ) {
|
|
|
|
if ( $urlData instanceof self )
|
|
$urlData = $urlData->data;
|
|
|
|
elseif ( is_string($urlData) )
|
|
$urlData = self::parse($urlData);
|
|
|
|
elseif ( is_array($urlData) ) foreach ( $urlData as $p => $v )
|
|
$urlData = self::override($urlData, $p, $v);
|
|
|
|
else throw SenseException::sprintf(
|
|
'Argument of type "%1$s" is invalid ! Type "string","array" or "Url" expected !',
|
|
gettype($urlData)
|
|
);
|
|
|
|
$this->data = $urlData;
|
|
|
|
}
|
|
|
|
public function __invoke ( $parts ) {
|
|
|
|
$urlData = $this->data;
|
|
|
|
foreach ( $parts as $part => $value ) {
|
|
|
|
if ( !array_key_exists($part, self::$properties) )
|
|
throw new SenseException(sprintf(
|
|
'Property "%1$s" does not exist in Url instance !',
|
|
$part
|
|
));
|
|
|
|
$urlData = self::override($urlData, $part, $value);
|
|
|
|
}
|
|
|
|
return new self($urlData);
|
|
|
|
}
|
|
|
|
public function __call ( $name, $args ) {
|
|
|
|
if ( !array_key_exists($name, self::$properties) )
|
|
throw new SenseException(sprintf(
|
|
'Property "%1$s" does not exist in Url instance !',
|
|
$name
|
|
));
|
|
|
|
return new self(self::override($this->data, $name, $args[0]));
|
|
|
|
}
|
|
|
|
public function &__get ( $offset ) {
|
|
|
|
if ( !array_key_exists($offset, self::$properties) )
|
|
throw new SenseException(sprintf(
|
|
'Property "%1$s" does not exist in Url instance !',
|
|
$offset
|
|
));
|
|
|
|
return $this->data[$offset];
|
|
|
|
}
|
|
|
|
public function __set ( $offset, $value ) {
|
|
|
|
if ( !array_key_exists($offset, self::$properties) )
|
|
throw new SenseException(sprintf(
|
|
'Property "%1$s" does not exist in Url instance !',
|
|
$offset
|
|
));
|
|
|
|
$this->data = self::override($this->data, $offset, $value);
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
public function __unset ( $offset ) {
|
|
|
|
if ( !array_key_exists($offset, self::$properties) )
|
|
throw new SenseException(sprintf(
|
|
'Property "%1$s" does not exist in Url instance !',
|
|
$offset
|
|
));
|
|
|
|
$this->$offset = '';
|
|
|
|
}
|
|
|
|
public function __toString ( ) {
|
|
|
|
return self::build($this->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
?>
|