Commit | Line | Data |
---|---|---|
19b53e5b | 1 | <?php |
380f3545 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
41498ac5 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
41498ac5 TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
19b53e5b C |
21 | |
22 | namespace Civi\Api4\Generic; | |
23 | ||
24 | /** | |
25 | * Base class for all "Create" api actions. | |
26 | * | |
27 | * @method $this setValues(array $values) Set all field values from an array of key => value pairs. | |
19b53e5b C |
28 | * @method array getValues() Get field values. |
29 | * | |
30 | * @package Civi\Api4\Generic | |
31 | */ | |
32 | abstract class AbstractCreateAction extends AbstractAction { | |
33 | ||
34 | /** | |
35 | * Field values to set | |
36 | * | |
37 | * @var array | |
38 | */ | |
39 | protected $values = []; | |
40 | ||
41 | /** | |
121ec912 | 42 | * @param string $fieldName |
19b53e5b C |
43 | * @return mixed|null |
44 | */ | |
121ec912 CW |
45 | public function getValue(string $fieldName) { |
46 | return isset($this->values[$fieldName]) ? $this->values[$fieldName] : NULL; | |
47 | } | |
48 | ||
49 | /** | |
50 | * Add a field value. | |
51 | * @param string $fieldName | |
52 | * @param mixed $value | |
53 | * @return $this | |
54 | */ | |
55 | public function addValue(string $fieldName, $value) { | |
56 | $this->values[$fieldName] = $value; | |
57 | return $this; | |
19b53e5b C |
58 | } |
59 | ||
60 | /** | |
61 | * @throws \API_Exception | |
62 | */ | |
63 | protected function validateValues() { | |
64 | $unmatched = $this->checkRequiredFields($this->getValues()); | |
65 | if ($unmatched) { | |
66 | throw new \API_Exception("Mandatory values missing from Api4 {$this->getEntityName()}::{$this->getActionName()}: " . implode(", ", $unmatched), "mandatory_missing", ["fields" => $unmatched]); | |
67 | } | |
68 | } | |
69 | ||
70 | } |