Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hydrateFrom() and appendFrom() methods #26

Merged
merged 6 commits into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ $manager->toJson(); // returns json of all items
echo $manager; // returns json string of all items
$manager->reset($array); // rebuild with new items
$manager->clear(); // empty the manager
$manager->hydrateFrom($type, $data); // imports from serialized data into new data set - currently only JSON
$manager->appendFrom($type, $data); // same as hydrate, but adds data to current data set

/* You can also use $manager as an array or in loops */
$manager['some']['starting']['data']; // 'here (optional)'
Expand Down
11 changes: 11 additions & 0 deletions src/Exceptions/IncorrectDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Michaels\Manager\Exceptions;

/**
* SerializationTypeNotSupportedException
* @package Michaels\Manager
*/
class IncorrectDataException extends \InvalidArgumentException
{

}
12 changes: 12 additions & 0 deletions src/Exceptions/SerializationTypeNotSupportedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Michaels\Manager\Exceptions;

/**
* SerializationTypeNotSupportedException
* @package Michaels\Manager
*/
class SerializationTypeNotSupportedException extends \InvalidArgumentException
{

}

95 changes: 95 additions & 0 deletions src/Traits/ManagesItemsTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Michaels\Manager\Traits;

use Michaels\Manager\Exceptions\IncorrectDataException;
use Michaels\Manager\Exceptions\SerializationTypeNotSupportedException;
use Michaels\Manager\Exceptions\ItemNotFoundException;
use Michaels\Manager\Exceptions\ModifyingProtectedValueException;
use Michaels\Manager\Exceptions\NestingUnderNonArrayException;
Expand Down Expand Up @@ -393,4 +395,97 @@ protected function performProtectedCheck($item)
throw new ModifyingProtectedValueException("Cannot access $item because it is protected");
}
}

/**
* Hydrate with external data
*
* @param $type string The type of data to be hydrated into the manager
* @param $data string The data to be hydrated into the manager
* @return $this
* @throws \Michaels\Manager\Exceptions\SerializationTypeNotSupportedException
*/
public function hydrateFrom($type, $data)
{
// we can possibly do some polymorphism for any other serialization types later
if( ! $this->checkType($type)){
throw new SerializationTypeNotSupportedException("$type serialization is not supported.");
}

$decodedData = $this->decodeFromJson($data);

if ($this->validateJson($decodedData)){

$this->reset($decodedData);
return $this;
}
throw new IncorrectDataException("The data is not proper JSON");
}

/**
* Hydrate with external data, appending to current data
*
* @param $type string The type of data to be hydrated into the manager
* @param $data string The data to be hydrated into the manager
* @return $this
* @throws \Michaels\Manager\Exceptions\SerializationTypeNotSupportedException
*
*/
public function appendFrom($type, $data)
{
if( ! $this->checkType($type)){
throw new SerializationTypeNotSupportedException("$type serialization is not supported.");
}

$decodedData = $this->decodeFromJson($data);

if ($this->validateJson($decodedData)){

$this->add($decodedData);
return $this;
}
throw new IncorrectDataException("The data is not proper JSON");
}

/**
* Checks if the input is really a json string
* @param $data mixed|null
* @return bool
*/
private function validateJson($data)
{

if ($data !== "") {
return (json_last_error() === JSON_ERROR_NONE);
}
}

/**
* Decodes JSON data to array
* @param $data string
* @return mixed|null
*/
private function decodeFromJson($data)
{
if (is_string($data)){

return json_decode($data, true); // true gives us associative arrays
}

return "";
}

/**
* Check to make sure the type input is ok. Currently only for JSON.
* @param $type
* @return bool
*/
private function checkType($type)
{
$type = strtolower(trim($type));

if ($type !== 'json') {
return false;
}
return true;
}
}
131 changes: 130 additions & 1 deletion tests/Traits/ManagesItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function testGetIfHasReturnsMessageIfNoExists()
$actual = $this->manager->getIfHas('nope');

$this->assertInstanceOf('Michaels\Manager\Messages\NoItemFoundMessage', $actual, 'failed to return an instance of NoItemFoundMessage');
$this->assertEquals("`nope` was not found", $actual->getMessage(), 'failed to return the correct mesage');
$this->assertEquals("`nope` was not found", $actual->getMessage(), 'failed to return the correct message');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching the typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No Problem.

}

public function testUpdateSingleItem()
Expand Down Expand Up @@ -498,4 +498,133 @@ public function testLoadDefaultsIntoNonEmptyManager()

$this->assertEquals($expected, $manager->getAll(), "failed to load defaults");
}

public function testHydrateFromJson()
{
$json = json_encode($this->testData);
$this->manager->clear();
$this->manager->hydrateFrom('json', $json);

$this->assertEquals($this->testData, $this->manager->getAll(), "failed to hydrate from JSON");

}

public function testAppendFromJson()
{
$startData = [
'one' => [
'two' => [
'three' => [
'true' => true,
]
],
'four' => [
'six' => false,
]
],
'five' => 5,
'six' => [
'a' => 'A',
'b' => 'B',
'c' => 'C',
]
];

$appendData = json_encode([
'seven' => [
'two' => [],
'four' => 'michael'
],
'eight' => [
'foo' => 'bar',
],
'nine' => 10
]);

$expected = [
'one' => [
'two' => [
'three' => [
'true' => true,
]
],
'four' => [
'six' => false,
]
],
'five' => 5,
'six' => [
'a' => 'A',
'b' => 'B',
'c' => 'C',
],
'seven' => [
'two' => [],
'four' => 'michael'
],
'eight' => [
'foo' => 'bar'
],
'nine' => 10
];

$this->manager->reset($startData);
$this->manager->appendFrom('json', $appendData);

$this->assertEquals($expected, $this->manager->getAll(), "failed to append from JSON");

}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding to the comment on line 411, a quick test to for passing ' jSOn ' or something denormalized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

/**
* @expectedException \Michaels\Manager\Exceptions\SerializationTypeNotSupportedException
*/

public function testSerializationTypeUnsupportedExceptionForHydrate()
{
$data = "just a string";
$this->manager->hydrateFrom('someType', $data);

}

/**
* @expectedException \Michaels\Manager\Exceptions\SerializationTypeNotSupportedException
*/

public function testSerializationTypeUnsupportedExceptionForAppend()
{
$data = "just a string";
$this->manager->appendFrom('someType', $data);

}

/**
* @expectedException \Michaels\Manager\Exceptions\IncorrectDataException
*/

public function testIncorrectDataExceptionForHydrate()
{
$data = array();
$this->manager->hydrateFrom('json', $data);

}

/**
* @expectedException \Michaels\Manager\Exceptions\IncorrectDataException
*/

public function testIncorrectDataExceptionForAppend()
{
$data = array();
$this->manager->appendFrom('json', $data);

}

public function testDenormalizedTypeInput()
{
$json = json_encode($this->testData);
$this->manager->clear();
$this->manager->hydrateFrom('jSOn ', $json);

$this->assertEquals($this->testData, $this->manager->getAll(), "failed to hydrate from JSON, with type `jSOn `.");
}
}