Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Email
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 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
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getLocalPart
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getHostname
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\CommonValueObjects;
3
4use Apie\Core\Attributes\CmsSingleInput;
5use Apie\Core\Attributes\CmsValidationCheck;
6use Apie\Core\Attributes\Description;
7use Apie\Core\Attributes\FakeMethod;
8use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
9use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
10use Apie\Core\ValueObjects\IsStringValueObject;
11use Egulias\EmailValidator\EmailValidator;
12use Egulias\EmailValidator\Validation\RFCValidation;
13use Faker\Generator;
14use ReflectionClass;
15
16#[FakeMethod('createRandom')]
17#[CmsSingleInput(['email', 'text'])]
18#[CmsValidationCheck(pattern: '^[^@]+@[^@]+$')]
19#[Description('Represents an e-mail address')]
20class Email implements StringValueObjectInterface
21{
22    use IsStringValueObject;
23
24    public static function createRandom(Generator $generator): self
25    {
26        return new static($generator->email());
27    }
28
29    protected function convert(string $input): string
30    {
31        return trim($input);
32    }
33
34    public static function validate(string $input): void
35    {
36        $validator = new EmailValidator();
37        if (!$validator->isValid($input, new RFCValidation())) {
38            throw new InvalidStringForValueObjectException($input, new ReflectionClass(__CLASS__));
39        }
40    }
41
42    public function getLocalPart(): EmailLocalPart
43    {
44        [$localPart] = explode('@', $this->internal, 2);
45
46        return new EmailLocalPart($localPart);
47    }
48
49    public function getHostname(): Hostname
50    {
51        $parts = explode('@', $this->internal, 2);
52
53        return new Hostname($parts[1]);
54    }
55}