如何从Ramsey\Uuid\Codec\StringCodec中提取uuid?

How to extract uuid from Ramsey\Uuid\Codec\StringCodec?

到目前为止,从以下几行开始

$records = $this->entityManager->getRepository(Translation::class)->findBy([], ['id' => 'DESC'], 1, 0);
$uuid = $records[0]->getId();

我可以得到以下几行。

Ramsey\Uuid\Uuid {#131
  #codec: Ramsey\Uuid\Codec\StringCodec {#86
    -builder: Ramsey\Uuid\Builder\DefaultUuidBuilder {#383
      -converter: Ramsey\Uuid\Converter\Number\DegradedNumberConverter {#390}
    }
  }
  #fields: array:6 [
    "time_low" => "1483fc71"
    "time_mid" => "66e9"
    "time_hi_and_version" => "4e21"
    "clock_seq_hi_and_reserved" => "b9"
    "clock_seq_low" => "50"
    "node" => "c6e9e6405428"
  ]
  #converter: Ramsey\Uuid\Converter\Number\DegradedNumberConverter {#390}
  uuid: "1483fc71-66e9"
}

如何提取 uuid

class 实现了 PHP 的 __toString 方法,因此您可以将对象转换为字符串:

$uuid = (string) $records[0]->getId();

使用 Ramsey\Uuid\Uuid 您可以通过多种方式将 UUID 提取为字符串。基本方法是 Ramsey\Uuid\Uuid::toString(),因此您的代码示例如下:

$uuid = $records[0]->getId()->toString();

对于命名约定,存在一些所有调用 Ramsey\Uuid\Uuid::toString():

的包装器方法
Ramsey\Uuid\Uuid::serialize()

Ramsey\Uuid\Uuid::jsonSerialize()

但是你当然可以将整个对象转换为字符串,就像之前提到的@iainn。