Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- interface Guid_Interface
- {
- }
- class Guid
- extends Object
- implements Guid_Interface
- {
- public function set($value) {
- try
- {
- $l = strlen($value);
- if(NULL === $value)
- throw new ArgumentNullException(ArgumentNullException::ERROR_IS_NULL);
- if(!is_string($value))
- throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_STRING);
- if(empty($value))
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_EMPTY_STRING);
- if($l < 32)
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_TO_SHORT);
- if($l > 32 && $l < 36)
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_LENGTH);
- if($l > 36)
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_TO_LONG);
- if($l === 36 && FALSE === (bool) preg_match("#^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$#",
- $value))
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_NO_PREGMATCH);
- if(FALSE === (bool) preg_match("#^[a-f0-9\-]+$#",
- $value))
- throw new ArgumentExpectedException(ArgumentExpectedException::ERROR_INVALID_STRING_ILLEGAL_CHAR);
- if($l === 36)
- return $this->format36(new String($value));
- return $this->format32(new String($value));
- }
- catch(ArgumentNullException $e)
- {
- return $this->format32(new String(md5($this->getHashCode()->get())));
- }
- catch(SystemException $e)
- {
- throw new DatatypeExpectedException(DatatypeExpectedException::ERROR_NOT_GUID, $e);
- }
- }
- private function format32(String $value) {
- $int = new Integer; $cat = new String;
- return $this->format36($value
- ->insert($int->set(8),
- $cat->set('-'))
- ->insert($int->set(13),
- $cat)
- ->insert($int->set(18),
- $cat)
- ->insert($int->set(23),
- $cat));
- }
- private function format36(String $value) {
- return parent::set($value->get())->finalize()->lock();
- }
- public function __construct($value = NULL) {
- try
- {
- try
- {
- parent::__construct($value);
- }
- //catch(DatatypeExpectedException $e){throw new DatatypeExpectedException($e->getCode(), $e);}
- catch(ArgumentNullException $e)
- {
- throw new ArgumentNullException($e->getCode());
- }
- }
- catch(SystemException $e)
- {
- throw new ObjectHandlerException(ObjectHandlerException::ERROR_CAN_NOT_CREATE, $e);
- }
- }
- }
- /*
- object(Guid)#1 (5) {
- ["__copy":"Object":private]=>
- string(36) "779bad1c-5e22-3bf1-f5d6-02372d6e09a0"
- ["__buffer":"Object":private]=>
- string(36) "779bad1c-5e22-3bf1-f5d6-02372d6e09a0"
- ["__usedBy":"Object":private]=>
- array(0) {
- }
- ["__type":"SystemObject":private]=>
- object(Type)#3 (8) {
- ["__type":"Type":private]=>
- string(4) "Guid"
- ["__isArray":"Type":private]=>
- bool(false)
- ["__isPrimitive":"Type":private]=>
- bool(false)
- ["__isCountable":"Type":private]=>
- bool(false)
- ["__isIterator":"Type":private]=>
- bool(false)
- ["__isLocked":"Type":private]=>
- bool(true)
- ["__locktrace":"Type":private]=>
- NULL
- ["__hash":"System":private]=>
- object(Hash)#4 (1) {
- ["__hashCode":"Hash":private]=>
- int(2029072002)
- }
- }
- ["__hash":"System":private]=>
- object(Hash)#2 (1) {
- ["__hashCode":"Hash":private]=>
- int(1159004465)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement