| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | namespace Civi\WorkflowMessage; |
| 13 | |
| 14 | use Civi\Test\ExampleDataInterface; |
| 15 | |
| 16 | /** |
| 17 | * Helper class for defining WorkflowMessage example-data. |
| 18 | * |
| 19 | * By convention, you should name this class relative to the target workflow, as in: |
| 20 | * - Workflow Name: case_activity |
| 21 | * - Workflow Class: CRM_Case_WorkflowMessage_CaseActivity |
| 22 | * - Example Data: CRM_Case_WorkflowMessage_CaseActivity_Foo |
| 23 | * - Example Name: workflow/case_activity/foo |
| 24 | */ |
| 25 | abstract class WorkflowMessageExample implements ExampleDataInterface { |
| 26 | |
| 27 | /** |
| 28 | * Name of the workflow for which we are providing example data. |
| 29 | * |
| 30 | * @var string |
| 31 | * Ex: 'CRM_Case_WorkflowMessage_CaseActivity' |
| 32 | */ |
| 33 | protected $wfClass; |
| 34 | |
| 35 | /** |
| 36 | * Name of the workflow for which we are providing example data. |
| 37 | * |
| 38 | * @var string|null |
| 39 | * Ex: 'case_activity' |
| 40 | */ |
| 41 | protected $wfName; |
| 42 | |
| 43 | /** |
| 44 | * Set the workflow name. |
| 45 | * |
| 46 | * The workflow name is the value in civicrm_message_template.workflow. |
| 47 | * |
| 48 | * @param string $workflowName |
| 49 | */ |
| 50 | public function setWorkflowName(string $workflowName): void { |
| 51 | $this->wfName = $workflowName; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Name for this example specifically. |
| 56 | * |
| 57 | * @var string |
| 58 | */ |
| 59 | protected $exName; |
| 60 | |
| 61 | /** |
| 62 | * Get the example name. |
| 63 | * |
| 64 | * @return string |
| 65 | */ |
| 66 | public function getExampleName(): string { |
| 67 | return $this->exName; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * WorkflowMessageExample constructor. |
| 72 | */ |
| 73 | public function __construct() { |
| 74 | if (!preg_match(';^(.*)[_\\\]([a-zA-Z0-9]+)$;', static::class, $m)) { |
| 75 | throw new \RuntimeException("Failed to parse class: " . static::class); |
| 76 | } |
| 77 | $this->wfClass = $m[1]; |
| 78 | $this->wfName = array_search($m[1], \Civi\WorkflowMessage\WorkflowMessage::getWorkflowNameClassMap()); |
| 79 | $this->exName = $m[2]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get an example, merge/extend it with more data, and return the extended |
| 84 | * variant. |
| 85 | * |
| 86 | * @param array $base |
| 87 | * Baseline data to build upon. |
| 88 | * @param array $overrides |
| 89 | * Additional data to recursively add. |
| 90 | * |
| 91 | * @return array |
| 92 | * The result of merging the original example with the $overrides. |
| 93 | */ |
| 94 | public function extend($base, $overrides = []) { |
| 95 | \CRM_Utils_Array::extend($base, $overrides); |
| 96 | return $base; |
| 97 | } |
| 98 | |
| 99 | protected function toArray(\Civi\WorkflowMessage\WorkflowMessageInterface $wfMsg) { |
| 100 | return [ |
| 101 | 'workflow' => $this->wfName, |
| 102 | 'modelProps' => $wfMsg->export('modelProps'), |
| 103 | ]; |
| 104 | } |
| 105 | |
| 106 | } |