From 2850eba99bf7a95da080c1370bf3a93c1b42b91b Mon Sep 17 00:00:00 2001 From: Chauncey McAskill Date: Thu, 7 Nov 2019 16:26:43 -0500 Subject: [PATCH] Fix property getter methods on revisionable --- src/Charcoal/Object/ObjectRevision.php | 111 ++++++------------ .../Object/ObjectRevisionInterface.php | 16 +-- src/Charcoal/Object/ObjectRoute.php | 38 +----- src/Charcoal/Object/RevisionableTrait.php | 48 +++++--- 4 files changed, 76 insertions(+), 137 deletions(-) diff --git a/src/Charcoal/Object/ObjectRevision.php b/src/Charcoal/Object/ObjectRevision.php index b86db70..3e86cfb 100644 --- a/src/Charcoal/Object/ObjectRevision.php +++ b/src/Charcoal/Object/ObjectRevision.php @@ -14,6 +14,7 @@ // From 'charcoal-core' use Charcoal\Model\AbstractModel; +use Charcoal\Model\ModelFactoryTrait; // From 'charcoal-object' use Charcoal\Object\ObjectRevisionInterface; @@ -32,6 +33,8 @@ */ class ObjectRevision extends AbstractModel implements ObjectRevisionInterface { + use ModelFactoryTrait; + /** * Object type of this revision (required) * @var string $targetType @@ -78,13 +81,7 @@ class ObjectRevision extends AbstractModel implements ObjectRevisionInterface private $dataDiff; /** - * @var FactoryInterface $modelFactory - */ - private $modelFactory; - - /** - * Dependencies - * @param Container $container DI Container. + * @param Container $container DI Container. * @return void */ protected function setDependencies(Container $container) @@ -95,24 +92,7 @@ protected function setDependencies(Container $container) } /** - * @param FactoryInterface $factory The factory used to create models. - * @return void - */ - protected function setModelFactory(FactoryInterface $factory) - { - $this->modelFactory = $factory; - } - - /** - * @return FactoryInterface The model factory. - */ - protected function modelFactory() - { - return $this->modelFactory; - } - - /** - * @param string $targetType The object type (type-ident). + * @param string $targetType The object type (type-ident). * @throws InvalidArgumentException If the obj type parameter is not a string. * @return ObjectRevision Chainable */ @@ -130,13 +110,13 @@ public function setTargetType($targetType) /** * @return string */ - public function targetType() + public function getTargetType() { return $this->targetType; } /** - * @param mixed $targetId The object ID. + * @param mixed $targetId The object ID. * @return ObjectRevision Chainable */ public function setTargetId($targetId) @@ -148,13 +128,13 @@ public function setTargetId($targetId) /** * @return mixed */ - public function targetId() + public function getTargetId() { return $this->targetId; } /** - * @param integer $revNum The revision number. + * @param integer $revNum The revision number. * @throws InvalidArgumentException If the revision number argument is not numerical. * @return ObjectRevision Chainable */ @@ -172,13 +152,13 @@ public function setRevNum($revNum) /** * @return integer */ - public function revNum() + public function getRevNum() { return $this->revNum; } /** - * @param mixed $revTs The revision's timestamp. + * @param mixed $revTs The revision's timestamp. * @throws InvalidArgumentException If the timestamp is invalid. * @return ObjectRevision Chainable */ @@ -203,13 +183,13 @@ public function setRevTs($revTs) /** * @return DateTimeInterface|null */ - public function revTs() + public function getRevTs() { return $this->revTs; } /** - * @param string $revUser The revision user ident. + * @param string $revUser The revision user ident. * @throws InvalidArgumentException If the revision user parameter is not a string. * @return ObjectRevision Chainable */ @@ -231,13 +211,13 @@ public function setRevUser($revUser) /** * @return string */ - public function revUser() + public function getRevUser() { return $this->revUser; } /** - * @param string|array|null $data The previous revision data. + * @param string|array|null $data The previous revision data. * @return ObjectRevision Chainable */ public function setDataPrev($data) @@ -255,13 +235,13 @@ public function setDataPrev($data) /** * @return array */ - public function dataPrev() + public function getDataPrev() { return $this->dataPrev; } /** - * @param array|string|null $data The current revision (object) data. + * @param array|string|null $data The current revision (object) data. * @return ObjectRevision Chainable */ public function setDataObj($data) @@ -279,13 +259,13 @@ public function setDataObj($data) /** * @return array */ - public function dataObj() + public function getDataObj() { return $this->dataObj; } /** - * @param array|string $data The data diff. + * @param array|string $data The data diff. * @return ObjectRevision */ public function setDataDiff($data) @@ -303,7 +283,7 @@ public function setDataDiff($data) /** * @return array */ - public function dataDiff() + public function getDataDiff() { return $this->dataDiff; } @@ -315,7 +295,7 @@ public function dataDiff() * 2. Load the current item from DB * 3. Create diff from (1) and (2). * - * @param RevisionableInterface $obj The object to create the revision from. + * @param RevisionableInterface $obj The object to create the revision from. * @return ObjectRevision Chainable */ public function createFromObject(RevisionableInterface $obj) @@ -324,15 +304,15 @@ public function createFromObject(RevisionableInterface $obj) $this->setTargetType($obj->objType()); $this->setTargetId($obj->id()); - $this->setRevNum($prevRev->revNum() + 1); + $this->setRevNum($prevRev->getRevNum() + 1); $this->setRevTs('now'); - if (is_callable([$obj, 'lastModifiedBy'])) { - $this->setRevUser($obj->lastModifiedBy()); + if (isset($obj['lastModifiedBy'])) { + $this->setRevUser($obj['lastModifiedBy']); } $this->setDataObj($obj->data()); - $this->setDataPrev($prevRev->dataObj()); + $this->setDataPrev($prevRev->getDataObj()); $diff = $this->createDiff(); $this->setDataDiff($diff); @@ -348,10 +328,10 @@ public function createFromObject(RevisionableInterface $obj) public function createDiff(array $dataPrev = null, array $dataObj = null) { if ($dataPrev === null) { - $dataPrev = $this->dataPrev(); + $dataPrev = $this->getDataPrev(); } if ($dataObj === null) { - $dataObj = $this->dataObj(); + $dataObj = $this->getDataObj(); } $dataDiff = $this->recursiveDiff($dataPrev, $dataObj); return $dataDiff; @@ -415,21 +395,13 @@ public function lastObjectRevision(RevisionableInterface $obj) $rev = $this->modelFactory()->create(self::class); - $sql = sprintf(' - SELECT - * - FROM - `%s` - WHERE - `target_type` = :target_type - AND - `target_id` = :target_id - ORDER BY - `rev_ts` desc - LIMIT 1', $this->source()->table()); + $sql = sprintf( + 'SELECT * FROM `%s` WHERE `target_type` = :target_type AND `target_id` = :target_id ORDER BY `rev_ts` DESC LIMIT 1', + $this->source()->table() + ); $rev->loadFromQuery($sql, [ 'target_type' => $obj->objType(), - 'target_id' => $obj->id() + 'target_id' => $obj->id(), ]); return $rev; @@ -448,25 +420,16 @@ public function objectRevisionNum(RevisionableInterface $obj, $revNum) $this->source()->createTable(); } - $rev = $this->modelFactory()->create(self::class); - $sql = sprintf(' - SELECT - * - FROM - `%s` - WHERE - `target_type` = :target_type - AND - `target_id` = :target_id - AND - `rev_num` = :rev_num - LIMIT 1', $this->source()->table()); + $sql = sprintf( + 'SELECT * FROM `%s` WHERE `target_type` = :target_type AND `target_id` = :target_id AND `rev_num` = :rev_num LIMIT 1', + $this->source()->table() + ); $rev->loadFromQuery($sql, [ 'target_type' => $obj->objType(), 'target_id' => $obj->id(), - 'rev_num' => intval($revNum) + 'rev_num' => intval($revNum), ]); return $rev; diff --git a/src/Charcoal/Object/ObjectRevisionInterface.php b/src/Charcoal/Object/ObjectRevisionInterface.php index f52adb1..9206ee6 100644 --- a/src/Charcoal/Object/ObjectRevisionInterface.php +++ b/src/Charcoal/Object/ObjectRevisionInterface.php @@ -18,7 +18,7 @@ public function setTargetType($targetType); /** * @return string */ - public function targetType(); + public function getTargetType(); /** * @param mixed $targetId The object ID. @@ -29,7 +29,7 @@ public function setTargetId($targetId); /** * @return mixed */ - public function targetId(); + public function getTargetId(); /** * @param integer $revNum The revision number. @@ -40,7 +40,7 @@ public function setRevNum($revNum); /** * @return integer */ - public function revNum(); + public function getRevNum(); /** * @param mixed $revTs The revision's timestamp. @@ -51,7 +51,7 @@ public function setRevTs($revTs); /** * @return \DateTimeInterface|null */ - public function revTs(); + public function getRevTs(); /** * @param string $revUser The revision user ident. @@ -62,7 +62,7 @@ public function setRevUser($revUser); /** * @return string */ - public function revUser(); + public function getRevUser(); /** * @param array|string $data The previous revision data. @@ -73,7 +73,7 @@ public function setDataPrev($data); /** * @return array */ - public function dataPrev(); + public function getDataPrev(); /** * @param array|string $data The current revision (object) data. @@ -84,7 +84,7 @@ public function setDataObj($data); /** * @return array */ - public function dataObj(); + public function getDataObj(); /** * @param array|string $data The data diff. @@ -95,7 +95,7 @@ public function setDataDiff($data); /** * @return array */ - public function dataDiff(); + public function getDataDiff(); /** * Create a new revision from an object diff --git a/src/Charcoal/Object/ObjectRoute.php b/src/Charcoal/Object/ObjectRoute.php index ff1bd5c..78e0960 100644 --- a/src/Charcoal/Object/ObjectRoute.php +++ b/src/Charcoal/Object/ObjectRoute.php @@ -178,7 +178,7 @@ protected function preUpdate(array $properties = null) */ public function isSlugUnique() { - $proto = $this->modelFactory()->get(self::class); + $proto = $this->modelFactory()->get(static::class); $loader = $this->collectionLoader(); $loader ->reset() @@ -411,42 +411,6 @@ public function setRouteOptionsIdent($routeOptionsIdent) return $this; } - /** - * Retrieve the object model factory. - * - * @throws RuntimeException If the model factory was not previously set. - * @return FactoryInterface - */ - public function modelFactory() - { - if (!isset($this->modelFactory)) { - throw new RuntimeException(sprintf( - 'Model Factory is not defined for "%s"', - get_class($this) - )); - } - - return $this->modelFactory; - } - - /** - * Retrieve the model collection loader. - * - * @throws RuntimeException If the collection loader was not previously set. - * @return CollectionLoader - */ - public function collectionLoader() - { - if (!isset($this->collectionLoader)) { - throw new RuntimeException(sprintf( - 'Collection Loader is not defined for "%s"', - get_class($this) - )); - } - - return $this->collectionLoader; - } - /** * Retrieve the object route. * diff --git a/src/Charcoal/Object/RevisionableTrait.php b/src/Charcoal/Object/RevisionableTrait.php index 67e8701..a1d9599 100644 --- a/src/Charcoal/Object/RevisionableTrait.php +++ b/src/Charcoal/Object/RevisionableTrait.php @@ -32,7 +32,7 @@ trait RevisionableTrait private $objectRevisionClass = ObjectRevision::class; /** - * @param boolean $enabled The (revision) enabled flag. + * @param boolean $enabled The (revision) enabled flag. * @return RevisionableInterface Chainable */ public function setRevisionEnabled($enabled) @@ -56,7 +56,7 @@ public function getRevisionEnabled() */ public function createRevisionObject() { - $rev = $this->modelFactory()->create($this->objectRevisionClass()); + $rev = $this->modelFactory()->create($this->getObjectRevisionClass()); return $rev; } @@ -85,21 +85,31 @@ protected function setObjectRevisionClass($className) * * @return string */ - public function objectRevisionClass() + public function getObjectRevisionClass() { return $this->objectRevisionClass; } /** - * @return ObjectRevision + * Alias of {@see self::getObjectRevisionClass()}. + * + * @return string + */ + public function objectRevisionClass() + { + return $this->getObjectRevisionClass(); + } + + /** * @see \Charcoal\Object\ObjectRevision::create_fromObject() + * @return ObjectRevision */ public function generateRevision() { $rev = $this->createRevisionObject(); $rev->createFromObject($this); - if (!empty($rev->dataDiff())) { + if (!empty($rev->getDataDiff())) { $rev->save(); } @@ -107,8 +117,8 @@ public function generateRevision() } /** - * @return ObjectRevision * @see \Charcoal\Object\ObejctRevision::lastObjectRevision + * @return ObjectRevision */ public function latestRevision() { @@ -119,9 +129,9 @@ public function latestRevision() } /** - * @param integer $revNum The revision number. - * @return ObjectRevision * @see \Charcoal\Object\ObejctRevision::objectRevisionNum + * @param integer $revNum The revision number. + * @return ObjectRevision */ public function revisionNum($revNum) { @@ -134,19 +144,19 @@ public function revisionNum($revNum) /** * Retrieves all revisions for the current objet * - * @param callable $callback Optional object callback. + * @param callable $callback Optional object callback. * @return array */ public function allRevisions(callable $callback = null) { $loader = new CollectionLoader([ - 'logger' => $this->logger, - 'factory' => $this->modelFactory() + 'logger' => $this->logger, + 'factory' => $this->modelFactory(), ]); $loader->setModel($this->createRevisionObject()); - $loader->addFilter('target_type', $this->objType()); - $loader->addFilter('target_id', $this->id()); - $loader->addOrder('rev_ts', 'desc'); + $loader->addFilter('targetType', $this->objType()); + $loader->addFilter('targetId', $this->id()); + $loader->addOrder('revTs', 'desc'); if ($callback !== null) { $loader->setCallback($callback); } @@ -156,7 +166,7 @@ public function allRevisions(callable $callback = null) } /** - * @param integer $revNum The revision number to revert to. + * @param integer $revNum The revision number to revert to. * @throws InvalidArgumentException If revision number is invalid. * @return boolean Success / Failure. */ @@ -173,10 +183,12 @@ public function revertToRevision($revNum) if (!$rev->id()) { return false; } - if (is_callable([$this, 'setLastModifiedBy'])) { - $this->setLastModifiedBy($rev->revUser()); + + if (isset($obj['lastModifiedBy'])) { + $obj['lastModifiedBy'] = $rev->getRevUser(); } - $this->setData($rev->dataObj()); + + $this->setData($rev->getDataObj()); $this->update(); return true;