1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/object-registry-bundle package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2018 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Sauls\Bundle\ObjectRegistryBundle\Factory; |
14
|
|
|
|
15
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Exception\CannotCreateEventNameForCollectionException; |
16
|
|
|
use function Sauls\Component\Helper\array_get_value; |
17
|
|
|
use function Sauls\Component\Helper\class_ucnp; |
18
|
|
|
use function Sauls\Component\Helper\object_ucnp; |
19
|
|
|
|
20
|
|
|
class EventNameFactory implements EventNameFactoryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $eventNamePattern = '%s.%s'; |
26
|
|
|
|
27
|
5 |
|
public function create(string $eventName, object $object): string |
28
|
|
|
{ |
29
|
5 |
|
if (\method_exists($object, 'getSubject')) { |
30
|
1 |
|
return $this->createEventNameForObject($eventName, $object->getSubject()); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
if (\method_exists($object, 'getObject')) { |
34
|
1 |
|
return $this->createEventNameForObject($eventName, $object->getObject()); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
if (\method_exists($object, 'getObjectClass')) { |
38
|
1 |
|
return $this->createEventNameForClass($eventName, $object->getObjectClass()); |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
if (\method_exists($object, 'getCollection')) { |
42
|
1 |
|
return $this->createEventNameForCollection($eventName, $object->getCollection()); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
return $this->createEventNameForObject($eventName, $object); |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
public function createEventNameForObject(string $eventName, object $object): string |
49
|
|
|
{ |
50
|
6 |
|
return sprintf($this->eventNamePattern, $eventName, object_ucnp($object)); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function createEventNameForClass(string $eventName, string $class): string |
54
|
|
|
{ |
55
|
2 |
|
return sprintf($this->eventNamePattern, $eventName, class_ucnp($class)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $eventName |
60
|
|
|
* @param array $collection |
61
|
|
|
* @return string |
62
|
|
|
* @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
63
|
|
|
*/ |
64
|
3 |
|
public function createEventNameForCollection(string $eventName, array $collection): string |
65
|
|
|
{ |
66
|
3 |
|
$object = array_get_value($collection, 0); |
67
|
3 |
|
if (null !== $object) { |
68
|
2 |
|
return $this->createEventNameForObject($eventName, $object); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
throw new CannotCreateEventNameForCollectionException( |
72
|
1 |
|
sprintf('Cannot create event name for collection: `%s`', print_r($collection, true)) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|