Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
3 / 4 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Hostname | |
75.00% |
3 / 4 |
|
66.67% |
2 / 3 |
5.39 | |
0.00% |
0 / 1 |
| createRandom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| convert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
4.12 | |||
| 1 | <?php |
| 2 | namespace Apie\CommonValueObjects; |
| 3 | |
| 4 | use Apie\Core\Attributes\CmsSingleInput; |
| 5 | use Apie\Core\Attributes\Description; |
| 6 | use Apie\Core\Attributes\ExampleValue; |
| 7 | use Apie\Core\Attributes\FakeMethod; |
| 8 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 9 | use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface; |
| 10 | use Apie\Core\ValueObjects\IsStringValueObject; |
| 11 | use Faker\Generator; |
| 12 | use ReflectionClass; |
| 13 | |
| 14 | #[FakeMethod('createRandom')] |
| 15 | #[CmsSingleInput(['hostname', 'text'])] |
| 16 | #[Description('Represents a hostname, such as a domain name or subdomain')] |
| 17 | #[ExampleValue('example.com')] |
| 18 | class Hostname implements StringValueObjectInterface |
| 19 | { |
| 20 | use IsStringValueObject; |
| 21 | |
| 22 | public static function createRandom(Generator $generator): self |
| 23 | { |
| 24 | return new static($generator->domainName()); |
| 25 | } |
| 26 | |
| 27 | protected function convert(string $input): string |
| 28 | { |
| 29 | return strtolower(trim($input)); |
| 30 | } |
| 31 | |
| 32 | public static function validate(string $input): void |
| 33 | { |
| 34 | if ($input === '' || filter_var($input, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) { |
| 35 | throw new InvalidStringForValueObjectException($input, new ReflectionClass(__CLASS__)); |
| 36 | } |
| 37 | } |
| 38 | } |