Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
3 / 4
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hostname
75.00% covered (warning)
75.00%
3 / 4
66.67% covered (warning)
66.67%
2 / 3
5.39
0.00% covered (danger)
0.00%
0 / 1
 createRandom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 convert
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
4.12
1<?php
2namespace Apie\CommonValueObjects;
3
4use Apie\Core\Attributes\CmsSingleInput;
5use Apie\Core\Attributes\Description;
6use Apie\Core\Attributes\ExampleValue;
7use Apie\Core\Attributes\FakeMethod;
8use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
9use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
10use Apie\Core\ValueObjects\IsStringValueObject;
11use Faker\Generator;
12use 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')]
18class 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}