Commit | Line | Data |
---|---|---|
7645fa5d TO |
1 | <?php |
2 | ||
3 | namespace Civi\API\Event; | |
4 | ||
5 | /** | |
6 | * Trait RequestTrait | |
7 | * @package Civi\API\Event | |
8 | * | |
9 | * Most events emitted by the API subsystem should include information about the active API request. | |
10 | */ | |
11 | trait RequestTrait { | |
12 | ||
13 | /** | |
14 | * @var \Civi\Api4\Generic\AbstractAction|array | |
15 | * The full description of the API request. | |
16 | * | |
17 | * @see \Civi\API\Request::create | |
18 | */ | |
19 | protected $apiRequest; | |
20 | ||
21 | /** | |
22 | * @return \Civi\Api4\Generic\AbstractAction|array | |
23 | */ | |
24 | public function getApiRequest() { | |
25 | return $this->apiRequest; | |
26 | } | |
27 | ||
28 | /** | |
29 | * @param \Civi\Api4\Generic\AbstractAction|array $apiRequest | |
30 | * The full description of the API request. | |
31 | * @return static | |
32 | */ | |
33 | protected function setApiRequest($apiRequest) { | |
34 | $this->apiRequest = $apiRequest; | |
35 | return $this; | |
36 | } | |
37 | ||
38 | /** | |
39 | * Create a brief string identifying the entity/action. Useful for | |
40 | * pithy matching/switching. | |
41 | * | |
42 | * Ex: if ($e->getApiRequestSig() === '3.contact.get') { ... } | |
43 | * | |
44 | * @return string | |
45 | * Ex: '3.contact.get' | |
46 | */ | |
47 | public function getApiRequestSig(): string { | |
48 | return mb_strtolower($this->apiRequest['version'] . '.' . $this->apiRequest['entity'] . '.' . $this->apiRequest['action']); | |
49 | } | |
50 | ||
51 | /** | |
52 | * @return string | |
53 | * Ex: 'Contact', 'Activity' | |
54 | */ | |
55 | public function getEntityName(): string { | |
56 | return $this->apiRequest['entity']; | |
57 | } | |
58 | ||
59 | /** | |
60 | * @return string | |
61 | * Ex: 'create', 'update' | |
62 | */ | |
63 | public function getActionName(): string { | |
64 | return $this->apiRequest['action']; | |
65 | } | |
66 | ||
67 | } |