Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
IsPortNumber
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 validate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 createRandom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTransportInfo
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\IanaValueObjects\PortNumber;
3
4use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
5use Apie\Core\ValueObjects\IsStringValueObject;
6use Apie\Core\ValueObjects\Utils;
7use Faker\Generator;
8use ReflectionClass;
9
10trait IsPortNumber
11{
12    use IsStringValueObject;
13
14    public static function validate(string $input): void
15    {
16        $input = Utils::toInt($input);
17        if ($input < 0 || $input > 65535) {
18            throw new InvalidStringForValueObjectException((string) $input, new ReflectionClass(static::class));
19        }
20    }
21
22    public static function createRandom(Generator $factory): self
23    {
24        return new static((string) $factory->numberBetween(0, 65535));
25    }
26
27    public function getTransportInfo(?TransportType $transport): PortNumberAndTransport|ActivePortNumberAndTransport
28    {
29        $className = static::class . 'AndTransport';
30        return new $className($this->internal . ($transport->value ?? ''));
31    }
32}