Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
3 | namespace Civi\Api4\Generic; | |
4 | ||
5 | /** | |
6 | * Base class for all "Create" api actions. | |
7 | * | |
8 | * @method $this setValues(array $values) Set all field values from an array of key => value pairs. | |
9 | * @method $this addValue($field, $value) Set field value. | |
10 | * @method array getValues() Get field values. | |
11 | * | |
12 | * @package Civi\Api4\Generic | |
13 | */ | |
14 | abstract class AbstractCreateAction extends AbstractAction { | |
15 | ||
16 | /** | |
17 | * Field values to set | |
18 | * | |
19 | * @var array | |
20 | */ | |
21 | protected $values = []; | |
22 | ||
23 | /** | |
24 | * @param string $key | |
25 | * | |
26 | * @return mixed|null | |
27 | */ | |
28 | public function getValue($key) { | |
29 | return isset($this->values[$key]) ? $this->values[$key] : NULL; | |
30 | } | |
31 | ||
32 | /** | |
33 | * @throws \API_Exception | |
34 | */ | |
35 | protected function validateValues() { | |
36 | $unmatched = $this->checkRequiredFields($this->getValues()); | |
37 | if ($unmatched) { | |
38 | throw new \API_Exception("Mandatory values missing from Api4 {$this->getEntityName()}::{$this->getActionName()}: " . implode(", ", $unmatched), "mandatory_missing", ["fields" => $unmatched]); | |
39 | } | |
40 | } | |
41 | ||
42 | } |