Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:8.0-cli-alpine3.13
FROM php:8.4-cli-alpine3.22

RUN apk update && \
apk add --no-cache \
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
},
"require": {
"php": "^8.0",
"php": "^8.4",
"ext-json": "*",
"ramsey/uuid": "^4.2"
},
Expand Down
5 changes: 5 additions & 0 deletions src/Domain/Model/ValueObject/CollectionValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function walk(callable $func): void
\array_walk($this->items, $func);
}

public function findOne(callable $func)
{
return \array_find($this->items, $func);
}

public function filter(callable $func): static
{
return static::from(\array_values(\array_filter($this->items, $func)));
Expand Down
9 changes: 0 additions & 9 deletions src/Domain/Model/ValueObject/DateTimeRangeValeObject.php

This file was deleted.

12 changes: 4 additions & 8 deletions src/Domain/Model/ValueObject/DateTimeValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,26 @@ final public static function createFromFormat(
string $format,
string $datetime,
?\DateTimeZone $timezone = null
): static|false {
): static {
$datetime = parent::createFromFormat($format, $datetime, $timezone);

if (false === $datetime) {
return false;
throw new \InvalidArgumentException('Invalid date format');
}

$timeZone = new \DateTimeZone(self::TIME_ZONE);

return static::createFromInterface($datetime->setTimezone($timeZone));
}

final public static function fromFormat(string $format, string $str): static|false
final public static function fromFormat(string $format, string $str): static
{
return static::createFromFormat($format, $str, new \DateTimeZone(self::TIME_ZONE));
}

final public static function createFromTimestamp(float|int $timestamp): static
{
$dateTime = self::fromFormat('U.u', \number_format((float) $timestamp, 6, '.', ''));

\assert(false !== $dateTime, 'Unexpected error on create date time from timestamp');

return $dateTime;
return self::fromFormat('U.u', \number_format((float) $timestamp, 6, '.', ''));
}

final public static function fromTimestamp(int|float $timestamp): static
Expand Down
14 changes: 5 additions & 9 deletions src/Domain/Model/ValueObject/DateValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function from(string $str): static
{
$timeZone = new \DateTimeZone(self::TIME_ZONE);

return (new static($str, $timeZone))->setTimezone($timeZone);
return (new static($str, $timeZone))->setTimezone($timeZone)->setTime(0, 0, 0, 0);
}

final public static function now(): static
Expand All @@ -40,30 +40,26 @@ final public static function createFromFormat(
string $format,
string $datetime,
?\DateTimeZone $timezone = null
): static|false {
): static {
$datetime = parent::createFromFormat($format, $datetime, $timezone);

if (false === $datetime) {
return false;
throw new \InvalidArgumentException('Invalid date format');
}

$timeZone = new \DateTimeZone(self::TIME_ZONE);

return static::createFromInterface($datetime->setTimezone($timeZone));
}

final public static function fromFormat(string $format, string $str): static|false
final public static function fromFormat(string $format, string $str): static
{
return static::createFromFormat($format, $str, new \DateTimeZone(self::TIME_ZONE));
}

final public static function createFromTimestamp(float|int $timestamp): static
{
$dateTime = self::fromFormat('U.u', \number_format((float) $timestamp, 6, '.', ''));

\assert(false !== $dateTime, 'Unexpected error on create date time from timestamp');

return $dateTime;
return self::fromFormat('U.u', \number_format((float) $timestamp, 6, '.', ''));
}

final public static function fromTimestamp(int|float $timestamp): static
Expand Down
22 changes: 21 additions & 1 deletion src/Domain/Model/ValueObject/UniqueId.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,30 @@

class UniqueId extends StringValueObject
{
private const VALID_PATTERN = '/^[A-Z0-9]{10}$/';

public static function from(string $value): static
{
if (false === self::isValid($value)) {
throw new \InvalidArgumentException('Invalid UniqueId.');
}

return parent::from($value);
}

public static function create(): static
{
$value = \base_convert(\uniqid(), 16, 36);
$value = \str_pad($value, 10, '0', \STR_PAD_LEFT);
$value = \substr($value, -10);

return self::from(
\strtoupper(\base_convert(\uniqid(), 16, 36)),
\strtoupper($value),
);
}

public static function isValid(string $value): bool
{
return 1 === \preg_match(self::VALID_PATTERN, $value);
}
}
116 changes: 0 additions & 116 deletions tests/Domain/Model/ValueObject/DateTimeRangeValeObjectTest.php

This file was deleted.

27 changes: 26 additions & 1 deletion tests/Domain/Model/ValueObject/UniqueIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function given_uid_class_when_ask_to_generate_an_uid_then_return_uid_inst
{
$uid = UniqueId::create();
$this->assertInstanceOf(UniqueId::class, $uid);
$this->assertMatchesRegularExpression('/^[0-9A-Z]{10}$/i', $uid->value());
$this->assertTrue(UniqueId::isValid($uid->value()));
}

/**
Expand All @@ -53,4 +53,29 @@ public function given_two_different_uids_when_ask_to_check_equality_then_return_

$this->assertFalse($str->equalTo($other));
}

/**
* @test
*/
public function given_invalid_uid_value_when_construct_then_fail()
{
$this->expectException(\InvalidArgumentException::class);
UniqueId::from('12345');
}

/**
* @test
*/
public function given_invalid_uid_value_when_ask_is_valid_then_return_false()
{
self::assertFalse(UniqueId::isValid('12345'));
}

/**
* @test
*/
public function given_valid_uid_value_when_ask_is_valid_then_return_true()
{
self::assertTrue(UniqueId::isValid('H7HVQ2U72X'));
}
}