/
EventNameTrait.php
59 lines (51 loc) · 1.74 KB
/
EventNameTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Workflow\Event;
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
/**
* @author Nicolas Rigaud <squrious@protonmail.com>
*/
trait EventNameTrait
{
/**
* Get the event name as lowercase string.
*
* Example: EnterEvent => enter
*/
public static function getName(): string
{
return strtolower(substr(substr(static::class, strrpos(static::class, '\\') + 1), 0, -5));
}
/**
* Get event name for workflow and transition.
*
* @throws InvalidArgumentException If $transitionName is provided without $workflowName
*/
private static function getNameForTransition(?string $workflowName, ?string $transitionName): string
{
return self::computeName($workflowName, $transitionName);
}
/**
* Get event name for workflow and place.
*
* @throws InvalidArgumentException If $placeName is provided without $workflowName
*/
private static function getNameForPlace(?string $workflowName, ?string $placeName): string
{
return self::computeName($workflowName, $placeName);
}
private static function computeName(?string $workflowName, ?string $transitionOrPlaceName): string
{
if (null !== $transitionOrPlaceName && null === $workflowName) {
throw new \InvalidArgumentException('Missing workflow name.');
}
return implode('.', array_filter(['workflow', $workflowName, self::getName(), $transitionOrPlaceName], \is_string(...)));
}
}