/** * mutator method for trailUuid * @param string $newTrailUuid value given for trail submissions * @throws InvalidArgumentException if $newTrailUuid is not a string or insecure * @throws RangeException If $newTrailUuid is not equal to 22 characters **/ public function setTrailUuid($newTrailUuid) { // base case if trail uuid is null, this is a new trail submission if ($newTrailUuid === null) { $this->trailUuid = null; return; } // base case if a new UUID is requested, make one if ($newTrailUuid === "CREATE-NEW-UUID") { $this->trailUuid = ShortUuid::uuid4(); return; } // verify the trail UUID is valid $newTrailUuid = trim($newTrailUuid); $newTrailUuid = trim($newTrailUuid); $newTrailUuid = filter_var($newTrailUuid, FILTER_SANITIZE_STRING); if ($newTrailUuid === false) { throw new InvalidArgumentException("uuid is empty of insecure"); } if (strlen($newTrailUuid) === 36) { // encode trail uuid to short form $uuid = Uuid::fromString($newTrailUuid); $shortUuid = new ShortUuid(); $this->trailUuid = $shortUuid->encode($uuid); } else { if (strlen($newTrailUuid) === 22) { $this->trailUuid = $newTrailUuid; } else { // throw an Range Exception throw new RangeException("trail uuid is not the correct length for the data base"); } } }
/** * @test * @dataProvider uuid_provider */ public function it_should_encode_a_given_uuid($uuid, $expectedShortUuid) { $shortUuid = $this->shortUuid->encode($uuid); $this->assertSame($expectedShortUuid, $shortUuid); }
protected function longUuidToShortUuid($longUuid) { $uuid = Uuid::fromString($longUuid); $shortUuid = new ShortUuid(); return (string) $shortUuid->encode($uuid); }