-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 5 commits
cccfe54
dcc1998
8150bc6
9bd99c6
38b0561
805e6db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
{ | ||
|
||
} |
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 | ||
{ | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
<?php | ||
namespace Michaels\Manager\Traits; | ||
|
||
use Guzzle\Service\Exception\InconsistentClientTransferException; | ||
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; | ||
|
@@ -393,4 +396,82 @@ 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($type !== 'json'){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. |
||
throw new SerializationTypeNotSupportedException("$type serialization is not supported."); | ||
} | ||
|
||
$decodedData = $this->decodeToJson($data); | ||
|
||
if ($this->isJson($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($type !== 'json'){ | ||
throw new SerializationTypeNotSupportedException("$type serialization is not supported."); | ||
} | ||
|
||
$decodedData = $this->decodeToJson($data); | ||
|
||
if ($this->isJson($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 isJson($data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know about calling this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll change. |
||
{ | ||
|
||
if ($data !== "") { | ||
return (json_last_error() === JSON_ERROR_NONE); | ||
} | ||
} | ||
|
||
/** | ||
* Decodes JSON data to array | ||
* @param $data string | ||
* @return mixed|null | ||
*/ | ||
private function decodeToJson($data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, silly me. |
||
{ | ||
if (is_string($data)){ | ||
|
||
return json_decode($data, true); // true gives us associative arrays | ||
} | ||
|
||
return ""; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching the typo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No Problem. |
||
} | ||
|
||
public function testUpdateSingleItem() | ||
|
@@ -498,4 +498,125 @@ 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"); | ||
|
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see where this is used, but if it is, let's remove it. I'd rather not require any more dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea where that came from. LOL!