Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * ILLI
- *
- * @category ILLI_System_Net
- * @package ILLI
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- */
- NAMESPACE ILLI\System\Net;
- /**
- * ILLI Scheme
- *
- * @category ILLI_System_Net
- * @package ILLI
- * @subpackage System_Net
- * @namespace ILLI\System\Net
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- * @since 2.0.1
- * @version 3.0.1
- */
- CLASS Scheme EXTENDS \ILLI\System\Table IMPLEMENTS \ILLI\Core\iArrayList
- {
- public function __construct(array $config = array())
- {
- $default = [
- 'scheme' => 'tcp',
- 'username' => '',
- 'password' => '',
- 'host' => 'localhost',
- 'port' => 0,
- 'path' => '',
- 'query' => [],
- 'fragment' => '',
- 'body' => []
- ];
- $config += $default;
- parent::__construct($config);
- $self = $this;
- $this
- ->filterRegisterEventClosure(\ILLI\Core\iFilter::ARGUMENTS, 'ILLI\System\Table::__set', function($data) use ($self)
- {
- $index = $data['index'];
- $value = $data['value'];
- switch($index):
- case 'scheme':
- $value = strtolower($value);
- break;
- case 'host':
- if(strpos($value, '/') !== false)
- {
- list($host, $self->path) = explode('/', $value, 2);
- $value = $host;
- }
- $value = strtolower($value);
- break;
- case 'path':
- $value = ($value[0] === '/') ?: '/'.$value;
- $value = str_replace('\\', '/', $value);
- $value = str_replace('//', '/', $value);
- break;
- case 'query':
- $value = (array) $value;
- break;
- endswitch;
- return ['index' => $index, 'value' => $value];
- });
- }
- public function addBody($data = NULL)
- {
- $orig = $this->body;
- $new = array_merge((array) $orig, (array) $data);
- $this->body = $new;
- return $this;
- }
- public function joinBody()
- {
- return join("\r\n", $this->body);
- }
- public function toUser()
- {
- $user = '';
- if($this->username !== '')
- {
- $user = $this->username;
- if($this->password !== '')
- $user = $user.':'.$this->password;
- $user = $user.'@';
- }
- return $user;
- }
- public function toQuery()
- {
- $query = [];
- foreach($this->query as $key => $value)
- $query[] = is_integer($key) ? $value : $key.'='.$value;
- $query = '?'.join('&', $query);
- return $query;
- }
- public function toFragement()
- {
- $fragment = '';
- if($this->fragment !== '')
- $fragment = '#'.$this->fragment;
- return $fragment;
- }
- public function toHost()
- {
- $port = '';
- if($this->port !== 0)
- $port = ':'.$this->port;
- return $this->host.$port;
- }
- public function toScheme()
- {
- return $this->scheme.'://';
- }
- public function toDomain()
- {
- return $this->toScheme().$this->toUser().$this->toHost();
- }
- public function toUri()
- {
- return $this->toDomain().$this->path.$this->toQuery().$this->toFragement();
- }
- public function toContext()
- {
- return [$this->scheme => $this->joinBody()];
- }
- }
- $foo = new \ILLI\System\Net\Http;
- $foo->host = 'pastebin.com';
- $foo->baz = 'bar' // has no effect
- $foo->version = []; // throws exception
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement