vendor/php-flasher/flasher/Stamp/UuidStamp.php line 12

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the PHPFlasher package.
  4.  * (c) Younes KHOUBZA <younes.khoubza@gmail.com>
  5.  */
  6. namespace Flasher\Prime\Stamp;
  7. use Flasher\Prime\Notification\Envelope;
  8. final class UuidStamp implements StampInterfacePresentableStampInterface
  9. {
  10.     /**
  11.      * @var string
  12.      */
  13.     private $uuid;
  14.     /**
  15.      * @param string|null $uuid
  16.      */
  17.     public function __construct($uuid null)
  18.     {
  19.         $this->uuid $uuid ?: sprintf(
  20.             '%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
  21.             mt_rand(065535),
  22.             mt_rand(065535),
  23.             mt_rand(065535),
  24.             mt_rand(1638420479),
  25.             mt_rand(3276849151),
  26.             mt_rand(065535),
  27.             mt_rand(065535),
  28.             mt_rand(065535)
  29.         );
  30.     }
  31.     /**
  32.      * @param Envelope[]|Envelope... $envelopes
  33.      *
  34.      * @return array<string, Envelope>
  35.      */
  36.     public static function indexByUuid($envelopes)
  37.     {
  38.         $envelopes = \is_array($envelopes) ? $envelopes : \func_get_args();
  39.         $map = array();
  40.         foreach ($envelopes as $envelope) {
  41.             $uuidStamp $envelope->get('Flasher\Prime\Stamp\UuidStamp');
  42.             if (!$uuidStamp instanceof UuidStamp) {
  43.                 $uuidStamp = new UuidStamp(spl_object_hash($envelope));
  44.                 $envelope->withStamp($uuidStamp);
  45.             }
  46.             $uuid $uuidStamp->getUuid();
  47.             $map[$uuid] = $envelope;
  48.         }
  49.         return $map;
  50.     }
  51.     /**
  52.      * @return string
  53.      */
  54.     public function getUuid()
  55.     {
  56.         return $this->uuid;
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function toArray()
  62.     {
  63.         return array('uuid' => $this->getUuid());
  64.     }
  65. }