Advertisement
fruffl

Table Example

Aug 22nd, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.37 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_System_Net
  6.      * @package    ILLI
  7.      * @link       http://illi.be
  8.      * @license    http://l.illi.be
  9.      * @copyright  ILLI Conference
  10.      */
  11.     NAMESPACE ILLI\System\Net;
  12.  
  13.     /**
  14.      * ILLI Scheme
  15.      *
  16.      * @category   ILLI_System_Net
  17.      * @package    ILLI
  18.      * @subpackage System_Net
  19.      * @namespace  ILLI\System\Net
  20.      * @link       http://illi.be
  21.      * @license    http://l.illi.be
  22.      * @copyright  ILLI Conference
  23.      * @since      2.0.1
  24.      * @version    3.0.1
  25.      */
  26.     CLASS Scheme EXTENDS \ILLI\System\Table IMPLEMENTS \ILLI\Core\iArrayList
  27.     {
  28.         public function __construct(array $config = array())
  29.         {
  30.             $default = [
  31.                 'scheme'    => 'tcp',
  32.                 'username'  => '',
  33.                 'password'  => '',
  34.                 'host'      => 'localhost',
  35.                 'port'      => 0,
  36.                 'path'      => '',
  37.                 'query'     => [],
  38.                 'fragment'  => '',
  39.                 'body'      => []
  40.             ];
  41.            
  42.             $config += $default;
  43.            
  44.             parent::__construct($config);
  45.            
  46.             $self = $this;
  47.            
  48.             $this
  49.             ->filterRegisterEventClosure(\ILLI\Core\iFilter::ARGUMENTS, 'ILLI\System\Table::__set', function($data) use ($self)
  50.             {
  51.                 $index = $data['index'];
  52.                 $value = $data['value'];
  53.                 switch($index):
  54.                     case 'scheme':
  55.                         $value = strtolower($value);
  56.                         break;
  57.                     case 'host':
  58.                         if(strpos($value, '/') !== false)
  59.                         {
  60.                             list($host, $self->path) = explode('/', $value, 2);
  61.                             $value = $host;
  62.                         }
  63.                        
  64.                         $value = strtolower($value);
  65.                         break;
  66.                     case 'path':
  67.                         $value = ($value[0] === '/') ?: '/'.$value;
  68.                         $value = str_replace('\\', '/', $value);
  69.                         $value = str_replace('//', '/', $value);
  70.                         break;
  71.                     case 'query':
  72.                         $value = (array) $value;
  73.                         break;
  74.                 endswitch;
  75.                
  76.                 return ['index' => $index, 'value' => $value];
  77.             });
  78.         }
  79.        
  80.         public function addBody($data = NULL)
  81.         {
  82.             $orig       = $this->body;
  83.             $new        = array_merge((array) $orig, (array) $data);
  84.             $this->body = $new;
  85.             return $this;
  86.         }
  87.        
  88.         public function joinBody()
  89.         {
  90.             return join("\r\n", $this->body);
  91.         }
  92.        
  93.         public function toUser()
  94.         {
  95.             $user = '';
  96.            
  97.             if($this->username !== '')
  98.             {
  99.                 $user = $this->username;
  100.                
  101.                 if($this->password !== '')
  102.                     $user = $user.':'.$this->password;
  103.                
  104.                 $user = $user.'@';
  105.             }
  106.            
  107.             return $user;
  108.         }
  109.        
  110.         public function toQuery()
  111.         {
  112.             $query = [];
  113.             foreach($this->query as $key => $value)
  114.                 $query[] = is_integer($key) ? $value : $key.'='.$value;
  115.            
  116.             $query = '?'.join('&', $query);
  117.            
  118.             return $query;
  119.         }
  120.        
  121.         public function toFragement()
  122.         {
  123.             $fragment = '';
  124.            
  125.             if($this->fragment !== '')
  126.                 $fragment = '#'.$this->fragment;
  127.            
  128.             return $fragment;
  129.         }
  130.        
  131.         public function toHost()
  132.         {
  133.             $port = '';
  134.            
  135.             if($this->port !== 0)
  136.                 $port = ':'.$this->port;
  137.            
  138.             return $this->host.$port;
  139.         }
  140.        
  141.         public function toScheme()
  142.         {
  143.             return $this->scheme.'://';
  144.         }
  145.        
  146.         public function toDomain()
  147.         {
  148.             return $this->toScheme().$this->toUser().$this->toHost();
  149.         }
  150.        
  151.         public function toUri()
  152.         {
  153.             return $this->toDomain().$this->path.$this->toQuery().$this->toFragement();
  154.         }
  155.        
  156.         public function toContext()
  157.         {
  158.             return [$this->scheme => $this->joinBody()];
  159.         }
  160.     }
  161.  
  162.  
  163.  
  164.     $foo = new \ILLI\System\Net\Http;
  165.    
  166.     $foo->host = 'pastebin.com';
  167.     $foo->baz = 'bar' // has no effect
  168.     $foo->version = []; // throws exception
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement