Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EmailLocalPart
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
4.59
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
 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\FakeMethod;
7use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
8use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
9use Apie\Core\ValueObjects\IsStringValueObject;
10use Faker\Generator;
11use ReflectionClass;
12
13#[FakeMethod('createRandom')]
14#[CmsSingleInput(['local-part', 'text'])]
15#[Description('Represents the local part of an e-mail address')]
16class EmailLocalPart implements StringValueObjectInterface
17{
18    use IsStringValueObject;
19
20    public static function createRandom(Generator $generator): self
21    {
22        return new static(str_replace(' ', '', $generator->userName()));
23    }
24
25    public static function validate(string $input): void
26    {
27        if ($input === '' || preg_match('/^(?=.{1,64}$)[A-Za-z0-9.!#$%&\'*+\/?^_`{|}~-]+$/', $input) !== 1) {
28            throw new InvalidStringForValueObjectException($input, new ReflectionClass(__CLASS__));
29        }
30    }
31}