diff --git a/packages/object/src/Charcoal/Object/ObjectRevision.php b/packages/object/src/Charcoal/Object/ObjectRevision.php index 7773b74b1..93f91ab80 100644 --- a/packages/object/src/Charcoal/Object/ObjectRevision.php +++ b/packages/object/src/Charcoal/Object/ObjectRevision.php @@ -293,9 +293,10 @@ public function getDataDiff() * 3. Create diff from (1) and (2). * * @param ModelInterface $obj The object to create the revision from. + * @param array|null $properties List of properties to revision. * @return ObjectRevision Chainable */ - public function createFromObject(ModelInterface $obj) + public function createFromObject(ModelInterface $obj, ?array $properties = null) { $prevRev = $this->lastObjectRevision($obj); @@ -308,7 +309,7 @@ public function createFromObject(ModelInterface $obj) $this->setRevUser($obj['lastModifiedBy']); } - $this->setDataObj($obj->data()); + $this->setDataObj($obj->data($properties)); $this->setDataPrev($prevRev->getDataObj()); $diff = $this->createDiff(); diff --git a/packages/object/src/Charcoal/Object/ObjectRevisionInterface.php b/packages/object/src/Charcoal/Object/ObjectRevisionInterface.php index f95d75d81..de052fafe 100644 --- a/packages/object/src/Charcoal/Object/ObjectRevisionInterface.php +++ b/packages/object/src/Charcoal/Object/ObjectRevisionInterface.php @@ -107,9 +107,10 @@ public function getDataDiff(); * 3. Create diff from (1) and (2). * * @param ModelInterface $obj The object to create the revision from. + * @param array|null $properties List of properties to revision. * @return ObjectRevision Chainable */ - public function createFromObject(ModelInterface $obj); + public function createFromObject(ModelInterface $obj, ?array $properties = null); /** * @param array $dataPrev Optional. The previous revision data. diff --git a/packages/object/src/Charcoal/Object/RevisionConfig.php b/packages/object/src/Charcoal/Object/RevisionConfig.php index 7f88362b4..71a2dab1b 100644 --- a/packages/object/src/Charcoal/Object/RevisionConfig.php +++ b/packages/object/src/Charcoal/Object/RevisionConfig.php @@ -10,10 +10,17 @@ * The config loaded when creating a revision for a model. * The config is generated from the `revisions` key in the config and can be customized per model. * - * `'revisions' : {'Namespace\\Model: {...}'}` : here the `...` represents the data used to create the config. + * {'revisions' : {'Namespace\\Model: {...}'}} here the `...` represents the data used to create the config. */ class RevisionConfig extends AbstractConfig { protected bool $enabled = true; protected string $revisionClass = ObjectRevision::class; + protected array $properties = []; + protected array $propertyBlacklist = [ + 'created', + 'lastModified', + 'createdBy', + 'lastModifiedBy', + ]; } diff --git a/packages/object/src/Charcoal/Object/RevisionService.php b/packages/object/src/Charcoal/Object/RevisionService.php index f619be0f0..f075a3636 100644 --- a/packages/object/src/Charcoal/Object/RevisionService.php +++ b/packages/object/src/Charcoal/Object/RevisionService.php @@ -32,8 +32,21 @@ public function generateRevision(ModelInterface $model): ?ObjectRevisionInterfac $config = $this->findRevisionsConfig($model); + $revisionProperties = array_keys($model->data()); + + if (count($config['properties'])) { + $revisionProperties = array_intersect_key($revisionProperties, $config['properties']); + } + + if (count($config['propertyBlacklist'])) { + $revisionProperties = array_filter( + $revisionProperties, + fn($n) => !in_array($n, $config['propertyBlacklist']) + ); + } + $revisionObject = $this->createRevisionObject($config->get('revisionClass')); - $revisionObject->createFromObject($model); + $revisionObject->createFromObject($model, $revisionProperties); if (!empty($revisionObject->getDataDiff())) { $revisionObject->save();