| 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 | * Name for this example specifically. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $exName; |
| 49 | |
| 50 | /** |
| 51 | * WorkflowMessageExample constructor. |
| 52 | */ |
| 53 | public function __construct() { |
| 54 | if (!preg_match(';^(.*)[_\\\]([a-zA-Z0-9]+)$;', static::class, $m)) { |
| 55 | throw new \RuntimeException("Failed to parse class: " . static::class); |
| 56 | } |
| 57 | $this->wfClass = $m[1]; |
| 58 | $this->wfName = array_search($m[1], \Civi\WorkflowMessage\WorkflowMessage::getWorkflowNameClassMap()); |
| 59 | $this->exName = $m[2]; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get an example, merge/extend it with more data, and return the extended |
| 64 | * variant. |
| 65 | * |
| 66 | * @param array $base |
| 67 | * Baseline data to build upon. |
| 68 | * @param array $overrides |
| 69 | * Additional data to recursively add. |
| 70 | * |
| 71 | * @return array |
| 72 | * The result of merging the original example with the $overrides. |
| 73 | */ |
| 74 | public function extend($base, $overrides = []) { |
| 75 | \CRM_Utils_Array::extend($base, $overrides); |
| 76 | return $base; |
| 77 | } |
| 78 | |
| 79 | protected function toArray(\Civi\WorkflowMessage\WorkflowMessageInterface $wfMsg) { |
| 80 | return [ |
| 81 | 'workflow' => $this->wfName, |
| 82 | 'modelProps' => $wfMsg->export('modelProps'), |
| 83 | ]; |
| 84 | } |
| 85 | |
| 86 | } |