Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public static function deserialize(array $data) : QueryMessage
 {
     $keys = ['id', 'type', 'timestamp', 'meta_data', 'payload_type', 'payload'];
     foreach ($keys as $key) {
         if (!isset($data[$key])) {
             $message = sprintf('Invalid serialization data: %s', VarPrinter::toString($data));
             throw new DomainException($message);
         }
     }
     if ($data['type'] !== MessageType::QUERY) {
         $message = sprintf('Invalid message type: %s', $data['type']);
         throw new DomainException($message);
     }
     /** @var MessageId $id */
     $id = MessageId::fromString($data['id']);
     /** @var DateTime $timestamp */
     $timestamp = DateTime::fromString($data['timestamp']);
     /** @var MetaData $metaData */
     $metaData = MetaData::create($data['meta_data']);
     /** @var Type $payloadType */
     $payloadType = Type::create($data['payload_type']);
     /** @var string $payloadClass */
     $payloadClass = $payloadType->toClassName();
     assert(Validate::implementsInterface($payloadClass, Query::class), sprintf('Unable to deserialize: %s', $payloadClass));
     /** @var Query $payload */
     $payload = $payloadClass::fromArray($data['payload']);
     return new static($id, $timestamp, $payload, $metaData);
 }
Esempio n. 2
0
 /**
  * Registers a command handler
  *
  * @param string         $commandClass The full command class name
  * @param CommandHandler $handler      The command handler
  *
  * @return void
  *
  * @throws DomainException When the command class is not valid
  */
 public function registerHandler(string $commandClass, CommandHandler $handler)
 {
     if (!Validate::implementsInterface($commandClass, Command::class)) {
         $message = sprintf('Invalid command class: %s', $commandClass);
         throw new DomainException($message);
     }
     $type = Type::create($commandClass)->toString();
     $this->handlers[$type] = $handler;
 }
Esempio n. 3
0
 /**
  * Registers a query handler
  *
  * @param string $queryClass  The full query class name
  * @param string $serviceName The handler service name
  *
  * @return void
  *
  * @throws DomainException When the query class is not valid
  */
 public function registerHandler(string $queryClass, string $serviceName)
 {
     if (!Validate::implementsInterface($queryClass, Query::class)) {
         $message = sprintf('Invalid query class: %s', $queryClass);
         throw new DomainException($message);
     }
     $type = Type::create($queryClass)->toString();
     $this->handlers[$type] = $serviceName;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($object) : int
 {
     if ($this === $object) {
         return 0;
     }
     assert(Validate::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     /** @var int $comp */
     $comp = $this->id <=> $object->id;
     return $comp;
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function equals($object) : bool
 {
     if ($this === $object) {
         return true;
     }
     if (!Validate::areSameType($this, $object)) {
         return false;
     }
     return $this->uuid->equals($object->uuid);
 }
Esempio n. 6
0
 /**
  * Created StdString instance
  *
  * @param mixed $value The string value
  *
  * @return StdString
  *
  * @throws DomainException When the value is not valid
  */
 function stdString($value) : StdString
 {
     if (!Validate::isStringCastable($value)) {
         $message = sprintf('Invalid string value: %s', VarPrinter::toString($value));
         throw new DomainException($message);
     }
     if ($value instanceof StdString) {
         return $value;
     }
     /** @var StdString $string */
     $string = StdString::create((string) $value);
     return $string;
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function deserialize(string $state) : Serializable
 {
     $data = json_decode($state, true);
     $keys = ['@', '$'];
     foreach ($keys as $key) {
         if (!isset($data[$key])) {
             $message = sprintf('Invalid serialization format: %s', $state);
             throw new DomainException($message);
         }
     }
     $class = ClassName::full($data['@']);
     assert(Validate::implementsInterface($class, Serializable::class), sprintf('Unable to deserialize: %s', $class));
     return $class::deserialize($data['$']);
 }
Esempio n. 8
0
 public function compareTo($object) : int
 {
     if ($this === $object) {
         return 0;
     }
     assert(Validate::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     $comp = strnatcmp($this->username(), $object->username());
     if ($comp > 0) {
         return 1;
     }
     if ($comp < 0) {
         return -1;
     }
     return 0;
 }
Esempio n. 9
0
 public function compareTo($object) : int
 {
     if ($this === $object) {
         return 0;
     }
     assert(Validate::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     $thisVal = $this->value();
     $thatVal = $object->value();
     if ($thisVal > $thatVal) {
         return 1;
     }
     if ($thisVal < $thatVal) {
         return -1;
     }
     return 0;
 }
 /**
  * Registers a subscriber service to handle events
  *
  * The subscriber class must implement:
  * Novuso\Common\Domain\Messaging\Event\EventSubscriber
  *
  * @param string $className The subscriber class name
  * @param string $serviceId The subscriber service ID
  *
  * @return void
  */
 public function registerService(string $className, string $serviceId)
 {
     assert(Validate::implementsInterface($className, EventSubscriber::class), sprintf('Invalid subscriber class: %s', $className));
     /** @var EventSubscriber $className The subscriber class name */
     foreach ($className::eventRegistration() as $eventType => $params) {
         if (is_string($params)) {
             $this->addHandlerService($eventType, $serviceId, $params);
         } elseif (is_string($params[0])) {
             $priority = isset($params[1]) ? (int) $params[1] : 0;
             $this->addHandlerService($eventType, $serviceId, $params[0], $priority);
         } else {
             foreach ($params as $handler) {
                 $priority = isset($handler[1]) ? (int) $handler[1] : 0;
                 $this->addHandlerService($eventType, $serviceId, $handler[0], $priority);
             }
         }
     }
 }
Esempio n. 11
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($object) : int
 {
     if ($this === $object) {
         return 0;
     }
     assert(Validate::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     $thisVal = $this->value;
     $thatVal = $object->value;
     $thisParts = explode('/', $thisVal);
     $thatParts = explode('/', $thatVal);
     if (count($thisParts) > 1 && count($thatParts) > 1) {
         return $this->compareParts($thisParts, $thatParts);
     } elseif (count($thisParts) > 1) {
         return 1;
     } elseif (count($thatParts) > 1) {
         return -1;
     }
     $strComp = strnatcmp($thisVal, $thatVal);
     /** @var int $comp */
     $comp = $strComp <=> 0;
     return $comp;
 }
Esempio n. 12
0
 /**
  * {@inheritdoc}
  */
 public function push($item)
 {
     assert(Validate::isType($item, $this->itemType()), $this->itemTypeError('push', $item));
     $index = $this->count++;
     $this->items[$index] = $item;
 }
Esempio n. 13
0
 /**
  * {@inheritdoc}
  */
 public function addLast($item)
 {
     assert(Validate::isType($item, $this->itemType()), $this->itemTypeError('addLast', $item));
     $this->list->push($item);
 }
Esempio n. 14
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($object) : int
 {
     if ($this === $object) {
         return 0;
     }
     assert(Validate::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     $thisStamp = $this->timestamp();
     $thatStamp = $object->timestamp();
     if ($thisStamp > $thatStamp) {
         return 1;
     }
     if ($thisStamp < $thatStamp) {
         return -1;
     }
     return $this->timezone->compareTo($object->timezone);
 }
Esempio n. 15
0
 /**
  * {@inheritdoc}
  */
 public function set($key, $value)
 {
     assert(Validate::isType($key, $this->keyType()), $this->keyTypeError('set', $key));
     assert(Validate::isType($value, $this->valueType()), $this->valueTypeError('set', $value));
     $this->tree->set($key, $value);
 }
Esempio n. 16
0
 /**
  * {@inheritdoc}
  */
 public function offsetUnset($index)
 {
     assert(Validate::isInt($index), sprintf('Invalid list index: %s', VarPrinter::toString($index)));
     $this->remove($index);
 }
Esempio n. 17
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($object) : int
 {
     if ($this === $object) {
         return 0;
     }
     assert(Validate::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     if ($this->year > $object->year) {
         return 1;
     }
     if ($this->year < $object->year) {
         return -1;
     }
     if ($this->month > $object->month) {
         return 1;
     }
     if ($this->month < $object->month) {
         return -1;
     }
     if ($this->day > $object->day) {
         return 1;
     }
     if ($this->day < $object->day) {
         return -1;
     }
     return 0;
 }
Esempio n. 18
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($object) : int
 {
     if ($this === $object) {
         return 0;
     }
     assert(Validate::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     if ($this->hour > $object->hour) {
         return 1;
     }
     if ($this->hour < $object->hour) {
         return -1;
     }
     if ($this->minute > $object->minute) {
         return 1;
     }
     if ($this->minute < $object->minute) {
         return -1;
     }
     if ($this->second > $object->second) {
         return 1;
     }
     if ($this->second < $object->second) {
         return -1;
     }
     return 0;
 }
Esempio n. 19
0
 /**
  * Removes a key/value pair
  *
  * @param string $key The key
  *
  * @return void
  */
 public function offsetUnset($key)
 {
     assert(Validate::isString($key), sprintf('Invalid metadata key: (%s) %s', gettype($key), VarPrinter::toString($key)));
     $this->remove($key);
 }
Esempio n. 20
0
 /**
  * {@inheritdoc}
  */
 public function add($item)
 {
     assert(Validate::isType($item, $this->itemType()), $this->itemTypeError('add', $item));
     $hash = Hasher::hash($item);
     if (!isset($this->buckets[$hash])) {
         $this->buckets[$hash] = new SetBucketChain();
     }
     if ($this->buckets[$hash]->add($item)) {
         $this->count++;
     }
 }
Esempio n. 21
0
 /**
  * {@inheritdoc}
  */
 public function add($item)
 {
     assert(Validate::isType($item, $this->itemType()), $this->itemTypeError('add', $item));
     $this->tree->set($item, true);
 }
Esempio n. 22
0
 /**
  * @dataProvider invalidWritableProvider
  */
 public function test_that_is_writable_returns_false_for_invalid_value($value)
 {
     $this->createFilesystem();
     $this->assertFalse(Validate::isWritable($value));
 }
Esempio n. 23
0
 /**
  * {@inheritdoc}
  */
 public function set($key, $value)
 {
     assert(Validate::isType($key, $this->keyType()), $this->keyTypeError('set', $key));
     assert(Validate::isType($value, $this->valueType()), $this->valueTypeError('set', $value));
     $hash = Hasher::hash($key);
     if (!isset($this->buckets[$hash])) {
         $this->buckets[$hash] = new TableBucketChain();
     }
     if ($this->buckets[$hash]->set($key, $value)) {
         $this->count++;
     }
 }
Esempio n. 24
0
 /**
  * {@inheritdoc}
  */
 public function enqueue($item)
 {
     assert(Validate::isType($item, $this->itemType()), $this->itemTypeError('enqueue', $item));
     $this->list->push($item);
 }
Esempio n. 25
0
 /**
  * Locates a bucket by item
  *
  * Returns null if the item is not found.
  *
  * @param mixed $item The item
  *
  * @return ItemBucket|null
  */
 protected function locate($item)
 {
     for ($this->rewind(); $this->valid(); $this->next()) {
         /** @var ItemBucket $current */
         $current = $this->current;
         if (Validate::areEqual($item, $current->item())) {
             return $current;
         }
     }
     return null;
 }
Esempio n. 26
0
 /**
  * Locates a bucket by key
  *
  * Returns null if the key is not found.
  *
  * @param mixed $key The key
  *
  * @return KeyValueBucket|null
  */
 protected function locate($key)
 {
     for ($this->rewind(); $this->valid(); $this->next()) {
         /** @var KeyValueBucket $current */
         $current = $this->current;
         if (Validate::areEqual($key, $current->key())) {
             return $current;
         }
     }
     return null;
 }
Esempio n. 27
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($object) : int
 {
     if ($this === $object) {
         return 0;
     }
     assert(Validate::areSameType($this, $object), sprintf('Comparison requires instance of %s', static::class));
     $strComp = strnatcmp($this->toString(), $object->toString());
     /** @var int $comp */
     $comp = $strComp <=> 0;
     return $comp;
 }
Esempio n. 28
0
 /**
  * {@inheritdoc}
  */
 public function enqueue($item)
 {
     assert(Validate::isType($item, $this->itemType()), $this->itemTypeError('enqueue', $item));
     if ($this->count === $this->cap) {
         $this->reindex($this->cap * 2);
     }
     $index = $this->end++;
     $this->items[$index] = $item;
     if ($this->end === $this->cap) {
         $this->end = 0;
     }
     $this->count++;
 }