# Hstore.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2009 by Sergey S. Sergeev                               *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00015     final class Hstore implements Stringable
    00016     {
    00017         protected $properties = array();
    00018
    00024         public static function create($string)
    00025         {
    00026             $self = new self();
    00027
    00028             return $self->toValue($string);
    00029         }
    00030
    00036         public static function make($array)
    00037         {
    00038             $self = new self();
    00039
    00040             return $self->setList($array);
    00041         }
    00042
    00046         public function setList($array)
    00047         {
    00048             $this->properties = $array;
    00049
    00050             return $this;
    00051         }
    00052
    00053         public function getList()
    00054         {
    00055             return $this->properties;
    00056         }
    00057
    00058         public function get($key)
    00059         {
    00060             if (!$this->isExists($key))
    00061                 throw new ObjectNotFoundException("Property with name '{$key}' does not exists");
    00062
    00063             return $this->properties[$key];
    00064         }
    00065
    00069         public function set($key, $value)
    00070         {
    00071             $this->properties[$key] = $value;
    00072
    00073             return $this;
    00074         }
    00075
    00079         public function drop($key)
    00080         {
    00081             unset($this->properties[$key]);
    00082
    00083             return $this;
    00084         }
    00085
    00086         public function isExists($key)
    00087         {
    00088             return isset($this->properties[$key]);
    00089         }
    00090
    00094         public function toValue($raw)
    00095         {
    00096             if (!$raw)
    00097                 return $this;
    00098
    00099             $return = null;
    00100             eval("\$return = array({$raw});");
    00101             $this->properties = $return;
    00102
    00103             return $this;
    00104         }
    00105
    00106         public function toString()
    00107         {
    00108             if (empty($this->properties))
    00109                 return null;
    00110
    00111             $string = '';
    00112
    00113             foreach ($this->properties as $k => $v) {
    00114                 if ($v !== null)
    00115                     $string .= "\"{$this->quoteValue($k)}\"=>\"{$this->quoteValue($v)}\",";
    00116                 else
    00117                     $string .= "\"{$this->quoteValue($k)}\"=>NULL,";
    00118             }
    00119
    00120             return $string;
    00121         }
    00122
    00123         protected function quoteValue($value)
    00124         {
    00125             return addslashes($value);
    00126         }
    00127     }
    00128 ?>