Lines 100.00% 6 / 6
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ActionName 100.00% 6 / 6 100.00% 2 / 2 3 100.00% 1 / 1
 __construct 100.00% 5 / 5 100.00% 1 / 1 2
 __toString 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Action;
6
7use Stewart\Contracts\Exception\ActionCallError;
8use Stewart\Contracts\Exception\ActionCallException;
9use Stringable;
10
11final readonly class ActionName implements Stringable
12{
13    private const string PATTERN = '/\A([a-z0-9_]+)\.([a-z0-9_]+)\z/';
14
15    public string $value;
16
17    public string $domain;
18
19    public string $action;
20
21    /** @throws ActionCallException */
22    public function __construct(string $value)
23    {
24        if (preg_match(self::PATTERN, $value, $parts) !== 1) {
25            throw ActionCallException::createForReason(ActionCallError::ActionNameInvalid, action: $value);
26        }
27
28        $this->value = $value;
29        $this->domain = $parts[1];
30        $this->action = $parts[2];
31    }
32
33    public function __toString(): string
34    {
35        return $this->value;
36    }
37}