Advertisement
fruffl

OOP GUID

Sep 16th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.16 KB | None | 0 0
  1.  
  2. <?PHP
  3.  
  4. interface Guid_Interface
  5. {
  6.    
  7. }
  8. class Guid
  9.         extends Object
  10.         implements Guid_Interface
  11. {
  12.     public function set($value) {
  13.         try
  14.         {
  15.             $l = strlen($value);
  16.  
  17.             if(NULL === $value)
  18.                 throw new ArgumentNullException(ArgumentNullException::ERROR_IS_NULL);
  19.             if(!is_string($value))
  20.                 throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_STRING);
  21.             if(empty($value))
  22.                 throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
  23.             if($l < 32)
  24.                 throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_TO_SHORT);
  25.             if($l > 32 && $l < 36)
  26.                 throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_LENGTH);
  27.             if($l > 36)
  28.                 throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_TO_LONG);
  29.             if($l === 36 && FALSE === (bool) preg_match("#^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$#",
  30.                                                         $value))
  31.                 throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_NO_PREGMATCH);
  32.             if(FALSE === (bool) preg_match("#^[a-f0-9\-]+$#",
  33.                                            $value))
  34.                 throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_ILLEGAL_CHAR);
  35.  
  36.             if($l === 36)
  37.                 return $this->format36(new String($value));
  38.             return $this->format32(new String($value));
  39.         }
  40.         catch(ArgumentNullException $e)
  41.         {
  42.             return $this->format32(new String(md5($this->getHashCode()->get())));
  43.         }
  44.         catch(SystemException $e)
  45.         {
  46.             throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_GUID, $e);
  47.         }
  48.     }
  49.     private function format32(String $value) {
  50.         $int = new Integer; $cat = new String;
  51.         return $this->format36($value
  52.                                 ->insert($int->set(8),
  53.                                                    $cat->set('-'))
  54.                                 ->insert($int->set(13),
  55.                                                    $cat)
  56.                                 ->insert($int->set(18),
  57.                                                    $cat)
  58.                                 ->insert($int->set(23),
  59.                                                    $cat));
  60.     }
  61.     private function format36(String $value) {
  62.         return parent::set($value->get())->finalize()->lock();
  63.     }
  64.     public function __construct($value = NULL) {
  65.         try
  66.         {
  67.             try
  68.             {
  69.                 parent::__construct($value);
  70.             }
  71. //catch(DatatypeExpectedException $e){throw new DatatypeExpectedException($e->getCode(), $e);}
  72.             catch(ArgumentNullException $e)
  73.             {
  74.                 throw new ArgumentNullException($e->getCode());
  75.             }
  76.         }
  77.         catch(SystemException $e)
  78.         {
  79.             throw new ObjectHandlerException(ObjectHandlerException::ERROR_CAN_NOT_CREATE, $e);
  80.         }
  81.     }
  82. }
  83. /*
  84. object(Guid)#1 (5) {
  85.   ["__copy":"Object":private]=>
  86.   string(36) "779bad1c-5e22-3bf1-f5d6-02372d6e09a0"
  87.   ["__buffer":"Object":private]=>
  88.   string(36) "779bad1c-5e22-3bf1-f5d6-02372d6e09a0"
  89.   ["__usedBy":"Object":private]=>
  90.   array(0) {
  91.   }
  92.   ["__type":"SystemObject":private]=>
  93.   object(Type)#3 (8) {
  94.     ["__type":"Type":private]=>
  95.     string(4) "Guid"
  96.     ["__isArray":"Type":private]=>
  97.     bool(false)
  98.     ["__isPrimitive":"Type":private]=>
  99.     bool(false)
  100.     ["__isCountable":"Type":private]=>
  101.     bool(false)
  102.     ["__isIterator":"Type":private]=>
  103.     bool(false)
  104.     ["__isLocked":"Type":private]=>
  105.     bool(true)
  106.     ["__locktrace":"Type":private]=>
  107.     NULL
  108.     ["__hash":"System":private]=>
  109.     object(Hash)#4 (1) {
  110.       ["__hashCode":"Hash":private]=>
  111.       int(2029072002)
  112.     }
  113.   }
  114.   ["__hash":"System":private]=>
  115.   object(Hash)#2 (1) {
  116.     ["__hashCode":"Hash":private]=>
  117.     int(1159004465)
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement