diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Generic/BasicUpdateAction.php |
first commit
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Generic/BasicUpdateAction.php')
-rw-r--r-- | www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Generic/BasicUpdateAction.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Generic/BasicUpdateAction.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Generic/BasicUpdateAction.php new file mode 100644 index 00000000..40c93624 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Generic/BasicUpdateAction.php @@ -0,0 +1,67 @@ +<?php + +namespace Civi\Api4\Generic; + +use Civi\API\Exception\NotImplementedException; + +/** + * Update one or more records with new values. + * + * Use the where clause (required) to select them. + */ +class BasicUpdateAction extends AbstractUpdateAction { + + /** + * @var callable + * + * Function(array $item, BasicUpdateAction $thisAction) => array + */ + private $setter; + + /** + * BasicUpdateAction constructor. + * + * @param string $entityName + * @param string $actionName + * @param string|array $select + * One or more fields to select from each matching item. + * @param callable $setter + * Function(array $item, BasicUpdateAction $thisAction) => array + */ + public function __construct($entityName, $actionName, $select = 'id', $setter = NULL) { + parent::__construct($entityName, $actionName, $select); + $this->setter = $setter; + } + + /** + * We pass the writeRecord function an array representing one item to update. + * We expect to get the same format back. + * + * @param \Civi\Api4\Generic\Result $result + */ + public function _run(Result $result) { + foreach ($this->getBatchRecords() as $item) { + $result[] = $this->writeRecord($this->values + $item); + } + } + + /** + * This Basic Update class can be used in one of two ways: + * + * 1. Use this class directly by passing a callable ($setter) to the constructor. + * 2. Extend this class and override this function. + * + * Either way, this function should return an array representing the one modified object. + * + * @param array $item + * @return array + * @throws \Civi\API\Exception\NotImplementedException + */ + protected function writeRecord($item) { + if (is_callable($this->setter)) { + return call_user_func($this->setter, $item, $this); + } + throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); + } + +} |