Merge pull request #22216 from demeritcowboy/th-br
[civicrm-core.git] / Civi / WorkflowMessage / Traits / FinalHelperTrait.php
CommitLineData
1972897e
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13namespace Civi\WorkflowMessage\Traits;
14
15/**
16 * Define a series of high-level, non-extensible helpers for WorkflowMessages,
17 * such as `renderTemplate()` or `sendTemplate()`.
18 *
19 * These helpers are convenient because it should be common to take a WorkflowMessage
20 * instance and pass it to a template. However, WorkflowMessage is the data-model
21 * for content of a message -- templating is outside the purview of WorkflowMessage.
22 * Consequently, there should not be any substantial templating logic here. Instead,
23 * these helpers MUST ONLY delegate out to a canonical renderer.
24 */
25trait FinalHelperTrait {
26
27 /**
28 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::export()
29 * @see \Civi\WorkflowMessage\Traits\ReflectiveWorkflowTrait::export()
30 */
31 abstract public function export(string $format = NULL): ?array;
32
33 /**
34 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::validate()
35 * @see \Civi\WorkflowMessage\Traits\ReflectiveWorkflowTrait::validate()
36 */
37 abstract public function validate(): array;
38
39 /**
40 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::assertValid()
41 */
42 final public function assertValid($strict = FALSE) {
43 $validations = $this->validate();
44 if (!$strict) {
45 $validations = array_filter($validations, function ($validation) {
46 return $validation['severity'] === 'error';
47 });
48 }
49 if (!empty($validations)) {
50 throw new \CRM_Core_Exception(sprintf("Found %d validation error(s) in %s.", count($validations), get_class($this)));
51 }
52 return $this;
53 }
54
55 /**
56 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::renderTemplate()
57 */
58 final public function renderTemplate(array $params = []): array {
59 $params['model'] = $this;
60 return \CRM_Core_BAO_MessageTemplate::renderTemplate($params);
61 }
62
63 /**
64 * @see \Civi\WorkflowMessage\WorkflowMessageInterface::sendTemplate()
65 */
66 final public function sendTemplate(array $params = []): array {
67 return \CRM_Core_BAO_MessageTemplate::sendTemplate($params + ['model' => $this]);
68 }
69
70 ///**
71 // * Get the list of available tokens.
72 // *
73 // * @return array
74 // * Ex: ['contact.first_name' => ['entity' => 'contact', 'field' => 'first_name', 'label' => ts('Last Name')]]
75 // * Array(string $dottedName => array('entity'=>string, 'field'=>string, 'label'=>string)).
76 // */
77 //final public function getTokens(): array {
78 // $tp = new TokenProcessor(\Civi::dispatcher(), [
79 // 'controller' => static::CLASS,
80 // ]);
81 // $tp->addRow($this->export('tokenContext'));
82 // return $tp->getTokens();
83 //}
84
85}