Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1984 from njam/encode-minimal
Browse files Browse the repository at this point in the history
Introduce ArrayConvertible::toArrayIdOnly()
  • Loading branch information
njam committed Nov 10, 2015
2 parents d0725df + 063bdb1 commit 3741219
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 7 deletions.
6 changes: 5 additions & 1 deletion library/CM/Action/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,14 @@ public function prepare() {
$this->_prepare();
}

public function toArray() {
public function toArrayIdOnly() {
return array('actor' => $this->getActor(), 'verb' => $this->getVerb(), 'type' => $this->getType());
}

public function toArray() {
return $this->toArrayIdOnly();
}

/**
* @return CM_Paging_ActionLimit_Action
*/
Expand Down
7 changes: 7 additions & 0 deletions library/CM/ArrayConvertible.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ interface CM_ArrayConvertible {
*/
public function toArray();

/**
* Object representation as array
*
* @return array
*/
public function toArrayIdOnly();

/**
* Return object from array-representation
*
Expand Down
6 changes: 5 additions & 1 deletion library/CM/Model/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,12 @@ public static function factoryGenericMultiple(array $idTypeList, $modelType = nu
return $resultList;
}

public function toArrayIdOnly() {
return array('_type' => $this->getType(), '_id' => $this->getIdRaw());
}

public function toArray() {
$array = array('_type' => $this->getType(), '_id' => $this->getIdRaw());
$array = $this->toArrayIdOnly();
if ($this->hasId()) {
$array['id'] = $this->getId();
}
Expand Down
6 changes: 5 additions & 1 deletion library/CM/Model/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,14 @@ private static function _findByIp($ip) {
return null;
}

public function toArray() {
public function toArrayIdOnly() {
return array('level' => $this->getLevel(), 'id' => $this->getId());
}

public function toArray() {
return $this->toArrayIdOnly();
}

public static function fromArray(array $data) {
return new self($data['level'], $data['id']);
}
Expand Down
6 changes: 5 additions & 1 deletion library/CM/Paging/ContentList/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ public function contains($string, $pattern = '/^\Q$item\E$/i') {
return false;
}

public function toArray() {
public function toArrayIdOnly() {
return array('type' => static::getTypeStatic());
}

public function toArray() {
return $this->toArrayIdOnly();
}

/**
* @param int $type
* @return CM_Paging_ContentList_Abstract
Expand Down
10 changes: 10 additions & 0 deletions library/CM/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,16 @@ public static function encode($value, $json = null) {
return $value;
}

/**
* @param CM_ArrayConvertible $object
* @return string JSON
*/
public static function encodeObjectId(CM_ArrayConvertible $object) {
$array = $object->toArrayIdOnly();
$value = array_merge($array, array('_class' => get_class($object)));
return self::jsonEncode($value);
}

/**
* @param string $value
* @param boolean|null $json
Expand Down
6 changes: 5 additions & 1 deletion library/CM/Site/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ public function rewrite(CM_Http_Request_Abstract $request) {
}
}

public function toArray() {
public function toArrayIdOnly() {
return array('type' => $this->getType());
}

public function toArray() {
return $this->toArrayIdOnly();
}

/**
* @param string $theme
* @return CM_Site_Abstract
Expand Down
7 changes: 7 additions & 0 deletions tests/library/CM/Model/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,11 @@ public function testFindByAttributesCache() {
public function testFindByAttributesException() {
CM_Model_Location::findByAttributes(CM_Model_Location::LEVEL_COUNTRY, ['notExistingField' => 'CH']);
}

public function testArrayConvertible() {
$location = CMTest_TH::createLocation();

$this->assertEquals($location, CM_Model_Location::fromArray($location->toArray()));
$this->assertEquals($location, CM_Model_Location::fromArray($location->toArrayIdOnly()));
}
}
24 changes: 22 additions & 2 deletions tests/library/CM/ParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ public function testEncodeArrayConvertible() {
$this->assertSame(1, $toArrayMethod->getCallCount());
}

public function testEncodeObjectId() {
/** @var CM_ArrayConvertible|\Mocka\ClassMock $arrayConvertible */
$arrayConvertible = $this->mockInterface('CM_ArrayConvertible')->newInstance();
$arrayConvertible->mockMethod('toArrayIdOnly')->set([
'id' => 3
]);
$arrayConvertible->mockMethod('toArray')->set([
'id' => 3,
'foo' => 'bar',
]);

$expectedEncoded = json_encode([
'id' => 3,
'_class' => get_class($arrayConvertible),
]);
$this->assertEquals($expectedEncoded, CM_Params::encodeObjectId($arrayConvertible));
}

public function testGetDateTime() {
$dateTimeList = array(
new DateTime('2012-12-12 13:00:00 +0300'),
Expand All @@ -231,7 +249,9 @@ public function testGetDateTime() {

public function testGetLocation() {
$location = CMTest_TH::createLocation();
$params = new CM_Params(['location' => $location, 'locationParameters' => ['id' => $location->getId(), 'level' => $location->getLevel()], 'insufficientParameters' => 1]);
$params = new CM_Params(['location' => $location,
'locationParameters' => ['id' => $location->getId(), 'level' => $location->getLevel()],
'insufficientParameters' => 1]);
$this->assertEquals($location, $params->getLocation('location'));
$this->assertEquals($location, $params->getLocation('locationParameters'));
try {
Expand Down Expand Up @@ -364,7 +384,7 @@ public function testDebugInfo() {
public function testDebugInfoWithException() {
/** @var CM_Params|\Mocka\AbstractClassTrait $params */
$params = $this->mockObject('CM_Params');
$params->mockMethod('getParamsDecoded')->set(function() {
$params->mockMethod('getParamsDecoded')->set(function () {
throw new Exception('foo');
});
$this->assertSame('[Cannot dump params: `foo`]', $params->getDebugInfo());
Expand Down

0 comments on commit 3741219

Please sign in to comment.