refactor: susun semula struktur folder — Laravel source ke src/

This commit is contained in:
Saufi
2026-05-19 15:58:35 +08:00
parent f052251b94
commit bf53c71b45
10806 changed files with 1385379 additions and 121 deletions

View File

@@ -0,0 +1,69 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use Ramsey\Uuid\UuidInterface;
/**
* A codec encodes and decodes a UUID according to defined rules
*
* @immutable
*/
interface CodecInterface
{
/**
* Returns a hexadecimal string representation of a UuidInterface
*
* @param UuidInterface $uuid The UUID for which to create a hexadecimal string representation
*
* @return non-empty-string Hexadecimal string representation of a UUID
*
* @pure
*/
public function encode(UuidInterface $uuid): string;
/**
* Returns a binary string representation of a UuidInterface
*
* @param UuidInterface $uuid The UUID for which to create a binary string representation
*
* @return non-empty-string Binary string representation of a UUID
*
* @pure
*/
public function encodeBinary(UuidInterface $uuid): string;
/**
* Returns a UuidInterface derived from a hexadecimal string representation
*
* @param string $encodedUuid The hexadecimal string representation to convert into a UuidInterface instance
*
* @return UuidInterface An instance of a UUID decoded from a hexadecimal string representation
*
* @pure
*/
public function decode(string $encodedUuid): UuidInterface;
/**
* Returns a UuidInterface derived from a binary string representation
*
* @param string $bytes The binary string representation to convert into a UuidInterface instance
*
* @return UuidInterface An instance of a UUID decoded from a binary string representation
*
* @pure
*/
public function decodeBytes(string $bytes): UuidInterface;
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use Ramsey\Uuid\Guid\Guid;
use Ramsey\Uuid\UuidInterface;
use function bin2hex;
use function sprintf;
use function substr;
/**
* GuidStringCodec encodes and decodes globally unique identifiers (GUID)
*
* @see Guid
*
* @immutable
*/
class GuidStringCodec extends StringCodec
{
public function encode(UuidInterface $uuid): string
{
/** @phpstan-ignore possiblyImpure.methodCall */
$hex = bin2hex($uuid->getFields()->getBytes());
/** @var non-empty-string */
return sprintf(
'%02s%02s%02s%02s-%02s%02s-%02s%02s-%04s-%012s',
substr($hex, 6, 2),
substr($hex, 4, 2),
substr($hex, 2, 2),
substr($hex, 0, 2),
substr($hex, 10, 2),
substr($hex, 8, 2),
substr($hex, 14, 2),
substr($hex, 12, 2),
substr($hex, 16, 4),
substr($hex, 20),
);
}
public function decode(string $encodedUuid): UuidInterface
{
/** @phpstan-ignore possiblyImpure.methodCall */
$bytes = $this->getBytes($encodedUuid);
/** @phpstan-ignore possiblyImpure.methodCall, possiblyImpure.methodCall */
return $this->getBuilder()->build($this, $this->swapBytes($bytes));
}
public function decodeBytes(string $bytes): UuidInterface
{
// Call parent::decode() to preserve the correct byte order.
return parent::decode(bin2hex($bytes));
}
/**
* Swaps bytes according to the GUID rules
*/
private function swapBytes(string $bytes): string
{
return $bytes[3] . $bytes[2] . $bytes[1] . $bytes[0]
. $bytes[5] . $bytes[4] . $bytes[7] . $bytes[6]
. substr($bytes, 8);
}
}

View File

@@ -0,0 +1,101 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use function strlen;
use function substr;
/**
* OrderedTimeCodec encodes and decodes a UUID, optimizing the byte order for more efficient storage
*
* For binary representations of version 1 UUID, this codec may be used to reorganize the time fields, making the UUID
* closer to sequential when storing the bytes. According to Percona, this optimization can improve database INSERT and
* SELECT statements using the UUID column as a key.
*
* The string representation of the UUID will remain unchanged. Only the binary representation is reordered.
*
* PLEASE NOTE: Binary representations of UUIDs encoded with this codec must be decoded with this codec. Decoding using
* another codec can result in malformed UUIDs.
*
* @deprecated Please migrate to {@link https://uuid.ramsey.dev/en/stable/rfc4122/version6.html Version 6, reordered time-based UUIDs}.
*
* @link https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/ Storing UUID Values in MySQL
*
* @immutable
*/
class OrderedTimeCodec extends StringCodec
{
/**
* Returns a binary string representation of a UUID, with the timestamp fields rearranged for optimized storage
*
* @return non-empty-string
*/
public function encodeBinary(UuidInterface $uuid): string
{
if (
/** @phpstan-ignore possiblyImpure.methodCall */
!($uuid->getFields() instanceof Rfc4122FieldsInterface)
/** @phpstan-ignore possiblyImpure.methodCall */
|| $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME
) {
throw new InvalidArgumentException('Expected version 1 (time-based) UUID');
}
/** @phpstan-ignore possiblyImpure.methodCall */
$bytes = $uuid->getFields()->getBytes();
return $bytes[6] . $bytes[7] . $bytes[4] . $bytes[5]
. $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3]
. substr($bytes, 8);
}
/**
* Returns a UuidInterface derived from an ordered-time binary string representation
*
* @throws InvalidArgumentException if $bytes is an invalid length
*
* @inheritDoc
*/
public function decodeBytes(string $bytes): UuidInterface
{
if (strlen($bytes) !== 16) {
throw new InvalidArgumentException('$bytes string should contain 16 characters.');
}
// Rearrange the bytes to their original order.
$rearrangedBytes = $bytes[4] . $bytes[5] . $bytes[6] . $bytes[7]
. $bytes[2] . $bytes[3] . $bytes[0] . $bytes[1]
. substr($bytes, 8);
$uuid = parent::decodeBytes($rearrangedBytes);
/** @phpstan-ignore possiblyImpure.methodCall */
$fields = $uuid->getFields();
if (!$fields instanceof Rfc4122FieldsInterface || $fields->getVersion() !== Uuid::UUID_TYPE_TIME) {
throw new UnsupportedOperationException(
'Attempting to decode a non-time-based UUID using OrderedTimeCodec',
);
}
return $uuid;
}
}

View File

@@ -0,0 +1,121 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use function bin2hex;
use function hex2bin;
use function implode;
use function sprintf;
use function str_replace;
use function strlen;
use function substr;
/**
* StringCodec encodes and decodes RFC 9562 (formerly RFC 4122) UUIDs
*
* @immutable
*/
class StringCodec implements CodecInterface
{
/**
* Constructs a StringCodec
*
* @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
*/
public function __construct(private UuidBuilderInterface $builder)
{
}
public function encode(UuidInterface $uuid): string
{
/** @phpstan-ignore possiblyImpure.methodCall */
$hex = bin2hex($uuid->getFields()->getBytes());
/** @var non-empty-string */
return sprintf(
'%08s-%04s-%04s-%04s-%012s',
substr($hex, 0, 8),
substr($hex, 8, 4),
substr($hex, 12, 4),
substr($hex, 16, 4),
substr($hex, 20),
);
}
/**
* @return non-empty-string
*/
public function encodeBinary(UuidInterface $uuid): string
{
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
return $uuid->getFields()->getBytes();
}
/**
* @throws InvalidUuidStringException
*
* @inheritDoc
*/
public function decode(string $encodedUuid): UuidInterface
{
/** @phpstan-ignore possiblyImpure.methodCall */
return $this->builder->build($this, $this->getBytes($encodedUuid));
}
public function decodeBytes(string $bytes): UuidInterface
{
if (strlen($bytes) !== 16) {
throw new InvalidArgumentException('$bytes string should contain 16 characters.');
}
return $this->builder->build($this, $bytes);
}
/**
* Returns the UUID builder
*/
protected function getBuilder(): UuidBuilderInterface
{
return $this->builder;
}
/**
* Returns a byte string of the UUID
*/
protected function getBytes(string $encodedUuid): string
{
$parsedUuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'], '', $encodedUuid);
$components = [
substr($parsedUuid, 0, 8),
substr($parsedUuid, 8, 4),
substr($parsedUuid, 12, 4),
substr($parsedUuid, 16, 4),
substr($parsedUuid, 20),
];
if (!Uuid::isValid(implode('-', $components))) {
throw new InvalidUuidStringException('Invalid UUID string: ' . $encodedUuid);
}
return (string) hex2bin($parsedUuid);
}
}

View File

@@ -0,0 +1,112 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\UuidInterface;
use function bin2hex;
use function sprintf;
use function substr;
use function substr_replace;
/**
* TimestampFirstCombCodec encodes and decodes COMBs, with the timestamp as the first 48 bits
*
* In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec adds the timestamp to the first 48 bits of
* the COMB. To generate a timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along with the
* CombGenerator as the random generator.
*
* ```
* $factory = new UuidFactory();
*
* $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder()));
*
* $factory->setRandomGenerator(new CombGenerator(
* $factory->getRandomGenerator(),
* $factory->getNumberConverter(),
* ));
*
* $timestampFirstComb = $factory->uuid4();
* ```
*
* @deprecated Please migrate to {@link https://uuid.ramsey.dev/en/stable/rfc4122/version7.html Version 7, Unix Epoch Time UUIDs}.
*
* @link https://web.archive.org/web/20240118030355/https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
*
* @immutable
*/
class TimestampFirstCombCodec extends StringCodec
{
/**
* @return non-empty-string
*/
public function encode(UuidInterface $uuid): string
{
/** @phpstan-ignore possiblyImpure.methodCall */
$bytes = $this->swapBytes($uuid->getFields()->getBytes());
return sprintf(
'%08s-%04s-%04s-%04s-%012s',
bin2hex(substr($bytes, 0, 4)),
bin2hex(substr($bytes, 4, 2)),
bin2hex(substr($bytes, 6, 2)),
bin2hex(substr($bytes, 8, 2)),
bin2hex(substr($bytes, 10))
);
}
/**
* @return non-empty-string
*/
public function encodeBinary(UuidInterface $uuid): string
{
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
return $this->swapBytes($uuid->getFields()->getBytes());
}
/**
* @throws InvalidUuidStringException
*
* @inheritDoc
*/
public function decode(string $encodedUuid): UuidInterface
{
/** @phpstan-ignore possiblyImpure.methodCall */
$bytes = $this->getBytes($encodedUuid);
/** @phpstan-ignore possiblyImpure.methodCall */
return $this->getBuilder()->build($this, $this->swapBytes($bytes));
}
public function decodeBytes(string $bytes): UuidInterface
{
/** @phpstan-ignore possiblyImpure.methodCall */
return $this->getBuilder()->build($this, $this->swapBytes($bytes));
}
/**
* Swaps bytes according to the timestamp-first COMB rules
*
* @pure
*/
private function swapBytes(string $bytes): string
{
$first48Bits = substr($bytes, 0, 6);
$last48Bits = substr($bytes, -6);
return substr_replace(substr_replace($bytes, $last48Bits, 0, 6), $first48Bits, -6);
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* This file is part of the ramsey/uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Uuid\Codec;
/**
* TimestampLastCombCodec encodes and decodes COMBs, with the timestamp as the last 48 bits
*
* The CombGenerator when used with the StringCodec (and, by proxy, the TimestampLastCombCodec) adds the timestamp to
* the last 48 bits of the COMB. The TimestampLastCombCodec is provided for the sake of consistency. In practice, it is
* identical to the standard StringCodec, but it may be used with the CombGenerator for additional context when reading
* code.
*
* Consider the following code. By default, the codec used by UuidFactory is the StringCodec, but here, we explicitly
* set the TimestampLastCombCodec. It is redundant, but it is clear that we intend this COMB to be generated with the
* timestamp appearing at the end.
*
* ```
* $factory = new UuidFactory();
*
* $factory->setCodec(new TimestampLastCombCodec($factory->getUuidBuilder()));
*
* $factory->setRandomGenerator(new CombGenerator(
* $factory->getRandomGenerator(),
* $factory->getNumberConverter(),
* ));
*
* $timestampLastComb = $factory->uuid4();
* ```
*
* @deprecated Please use {@see StringCodec} instead.
*
* @immutable
*/
class TimestampLastCombCodec extends StringCodec
{
}