Skip to content

Commit

Permalink
Fix property getter methods on revisionable
Browse files Browse the repository at this point in the history
  • Loading branch information
mcaskill committed Nov 7, 2019
1 parent d4301e2 commit 2850eba
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 137 deletions.
111 changes: 37 additions & 74 deletions src/Charcoal/Object/ObjectRevision.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

// From 'charcoal-core'
use Charcoal\Model\AbstractModel;
use Charcoal\Model\ModelFactoryTrait;

// From 'charcoal-object'
use Charcoal\Object\ObjectRevisionInterface;
Expand All @@ -32,6 +33,8 @@
*/
class ObjectRevision extends AbstractModel implements ObjectRevisionInterface
{
use ModelFactoryTrait;

/**
* Object type of this revision (required)
* @var string $targetType
Expand Down Expand Up @@ -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)
Expand All @@ -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
*/
Expand All @@ -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)
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -303,7 +283,7 @@ public function setDataDiff($data)
/**
* @return array
*/
public function dataDiff()
public function getDataDiff()
{
return $this->dataDiff;
}
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions src/Charcoal/Object/ObjectRevisionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function setTargetType($targetType);
/**
* @return string
*/
public function targetType();
public function getTargetType();

/**
* @param mixed $targetId The object ID.
Expand All @@ -29,7 +29,7 @@ public function setTargetId($targetId);
/**
* @return mixed
*/
public function targetId();
public function getTargetId();

/**
* @param integer $revNum The revision number.
Expand All @@ -40,7 +40,7 @@ public function setRevNum($revNum);
/**
* @return integer
*/
public function revNum();
public function getRevNum();

/**
* @param mixed $revTs The revision's timestamp.
Expand All @@ -51,7 +51,7 @@ public function setRevTs($revTs);
/**
* @return \DateTimeInterface|null
*/
public function revTs();
public function getRevTs();

/**
* @param string $revUser The revision user ident.
Expand All @@ -62,7 +62,7 @@ public function setRevUser($revUser);
/**
* @return string
*/
public function revUser();
public function getRevUser();

/**
* @param array|string $data The previous revision data.
Expand All @@ -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.
Expand All @@ -84,7 +84,7 @@ public function setDataObj($data);
/**
* @return array
*/
public function dataObj();
public function getDataObj();

/**
* @param array|string $data The data diff.
Expand All @@ -95,7 +95,7 @@ public function setDataDiff($data);
/**
* @return array
*/
public function dataDiff();
public function getDataDiff();

/**
* Create a new revision from an object
Expand Down
Loading

0 comments on commit 2850eba

Please sign in to comment.