ldc-boutique-cnc-sable/domain/Models/ValueObjects/Slug.php
Jérémy Munsch 8b96ce90be
test(casts): add tests for EmployeeSitesCast functionality
test(casts): add tests for ShopperEstablishmentCodeCast functionality

test(casts): add tests for ShopperServiceCodeCast functionality

test(casts): add tests for ShopperSiteCodeCast functionality

test(dto): update ProductDtoTest to include additional fields

test(dto): update SettingDtoTest to remove arcadiaSites assertions

test(events): update AnOrderHasBeenPlacedTest to use value objects

test(entities): update OrderTest to use value objects for employee details

test(value-objects): update EmployeeUpsertDataTest to use value objects

test(pagination): update PaginationTest to access value properties

test(services): update AppCustomerAuthServiceTest to use CrmHashcode

test(services): update CrmServiceTest to use CrmHashcode for customer retrieval
2026-10-27 09:58:45 +01:00

38 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
use Webmozart\Assert\Assert;
final readonly class Slug implements \Stringable
{
public function __construct(public string $value)
{
Assert::stringNotEmpty(trim($value), 'Slug cannot be empty.');
Assert::maxLength($value, 255, 'Slug cannot exceed 255 characters.');
Assert::regex($value, '/^[a-z0-9]+(?:-[a-z0-9]+)*$/', 'Slug format is invalid.');
}
public static function fromString(string $string): self
{
$transliterated = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $string);
$cleanString = $transliterated !== false ? $transliterated : $string;
$slug = strtolower(trim($cleanString));
$slug = preg_replace('/[^a-z0-9]+/i', '-', $slug) ?? '';
$slug = trim($slug, '-');
$slug = preg_replace('/-+/', '-', $slug) ?? '';
Assert::stringNotEmpty($slug, 'Cannot generate a valid slug from given string.');
return new self($slug);
}
#[\Override]
public function __toString(): string
{
return $this->value;
}
}