Skip to content

Commit

Permalink
feat(revision): add properties and propertyBlacklist options for …
Browse files Browse the repository at this point in the history
…Revisions

`properties` limits the scope of revision to only some properties while `propertyBlacklist` ensures that some properties are not processed into the revision
  • Loading branch information
JoelAlphonso committed Nov 8, 2022
1 parent e4d2429 commit baafa78
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/object/src/Charcoal/Object/ObjectRevision.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion packages/object/src/Charcoal/Object/RevisionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
15 changes: 14 additions & 1 deletion packages/object/src/Charcoal/Object/RevisionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit baafa78

Please sign in to comment.