Skip to content

Commit

Permalink
move hydrate to Manager, refactor FileLoader, general tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
electricjones committed Nov 3, 2015
1 parent 9649744 commit 46b5c6a
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 217 deletions.
53 changes: 29 additions & 24 deletions src/Bags/FileBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,50 @@
*/
class FileBag
{
/**
* @var array An array of SplFileInfo objects
*/
protected $fileObjects = [];

private $fileObjects = [];

/**
* Constructs a new FileBag
* @param $arrayOfSplFileInfoObjects
*/
public function __construct($arrayOfSplFileInfoObjects)
{
$this->initialize($arrayOfSplFileInfoObjects);
}

/**
* Return an array of SplFileInfo objects
* @return array
*/
public function getAllFileInfoObjects()
{
return $this->fileObjects;
}

/**
* Reset the FileBag back to an empty array.
* @return array - an empty array
*/
public function emptyBag()
{
$this->fileObjects = [];
return $this->fileObjects;
}

/**
* Set up the bag with a proper array of SplFileInfo objects
* @param array $splFileInfoObjects
* @internal param $arrayOfSplFileInfoObjects
*/
private function initialize(array $splFileInfoObjects)
protected function initialize(array $splFileInfoObjects)
{
foreach ($splFileInfoObjects as $object) {
if ($this->isSplFileInfoObject($object)) {
array_unshift($this->fileObjects, $object);
// $this->fileObjects[] = $object;
} else {
throw new BadFileInfoObjectException('The input array does not hold proper SplFileInfo objects.');
}
Expand All @@ -44,25 +70,4 @@ protected function isSplFileInfoObject($object)
{
return ($object instanceof \SplFileInfo);
}

/**
* Return an array of SplFileInfo objects
* @return array
*/
public function getAllFileInfoObjects()
{
return $this->fileObjects;
}

/**
* Reset the FileBag back to an empty array.
* @return array - an empty array
*/
public function emptyBag()
{
$this->fileObjects = [];
return $this->fileObjects;
}

}

6 changes: 1 addition & 5 deletions src/Contracts/DecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Michaels\Manager\Contracts;

/**
*
* Contract for Decoders
* @package Michaels\Manager
*/

Expand All @@ -16,7 +16,6 @@ interface DecoderInterface
*/
public function decode($data);


/**
* The data returned is the actual file extensions supported for the files to decode.
* For instance, if you want to decode Yaml files with the extensions ".yml" and ".yaml",
Expand All @@ -25,7 +24,4 @@ public function decode($data);
* @return array
*/
public function getMimeType();


}

4 changes: 1 addition & 3 deletions src/Contracts/LoadsFilesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface LoadsFilesInterface
* @param $append boolean
* @return array
*/
public function loadFiles(array $files, $append=false);
public function loadFiles(array $files, $append = false);

/**
* Allows for the addition of a custom decoder to load custom files..
Expand All @@ -25,6 +25,4 @@ public function loadFiles(array $files, $append=false);
* @return mixed
*/
public function addDecoder(DecoderInterface $decoder);

}

9 changes: 3 additions & 6 deletions src/Decoders/CustomXmlDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
*/
class CustomXmlDecoder implements DecoderInterface
{
private $arrayData = [];
/** @var array Decoded data */
protected $arrayData = [];

/**
* This is the method, which does the decoding work.
Expand All @@ -23,11 +24,10 @@ public function decode($data)
{
$xml = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$this->arrayData = json_decode($json,TRUE);
$this->arrayData = json_decode($json, true);
return $this->arrayData;
}


/**
* The data returned is the actual file extensions supported for the files to decode.
* For instance, if you want to decode Yaml files with the extensions ".yml" and ".yaml",
Expand All @@ -39,7 +39,4 @@ public function getMimeType()
{
return ['xml'];
}

}


18 changes: 5 additions & 13 deletions src/Decoders/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

use Michaels\Manager\Contracts\DecoderInterface;
use Michaels\Manager\Exceptions\JsonDecodingFailedException;

/**
* A standard Json Decoder Module for the data manager file loader.
*
* @package Michaels\Manager
*/
class JsonDecoder implements DecoderInterface
{

private $arrayData;
/** @var array Decoded data */
protected $arrayData = [];

/**
* Decodes JSON data to array
Expand All @@ -23,40 +24,31 @@ class JsonDecoder implements DecoderInterface
public function decode($data)
{
if (is_string($data)) {

$this->arrayData = json_decode($data, true); // true gives us associative arrays

if($this->isValidJson()) {

if ($this->isValidJson()) {
return $this->arrayData;
}
}

throw new JsonDecodingFailedException('The data provided was not proper JSON');
}


/**
* Returns MimeType
* @return string
*/
public function getMimeType()
{
return ['json'];
}


/**
* Checks if the input is really a json string and if the PHP Json decoding was successful.
*
* @return boolean
*/
protected function isValidJson()
{
return (json_last_error() === JSON_ERROR_NONE);
}




}

9 changes: 3 additions & 6 deletions src/Decoders/PhpDecoder.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
namespace Michaels\Manager\Decoders;

use Michaels\Manager\Contracts\DecoderInterface;

/**
* A wrapper for the PHP Decoder Module for the data manager file loader.
*
* @package Michaels\Manager
*/

use Michaels\Manager\Contracts\DecoderInterface;

class PhpDecoder implements DecoderInterface
{

Expand All @@ -22,7 +22,6 @@ public function decode($data)
return $data;
}


/**
* The data returned is the actual file extensions supported for the files to decode.
* For instance, if you want to decode Yaml files with the extensions ".yml" and ".yaml",
Expand All @@ -34,6 +33,4 @@ public function getMimeType()
{
return ['php'];
}

}

8 changes: 3 additions & 5 deletions src/Decoders/YamlDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/
class YamlDecoder implements DecoderInterface
{
private $arrayData = [];
/** @var array Decoded data */
protected $arrayData = [];

/**
* This is the method, which does the decoding work.
Expand All @@ -25,7 +26,6 @@ public function decode($data)
return $this->arrayData;
}


/**
* The data returned is the actual file extensions supported for the files to decode.
* For instance, if you want to decode Yaml files with the extensions ".yml" and ".yaml",
Expand All @@ -35,8 +35,6 @@ public function decode($data)
*/
public function getMimeType()
{
return ['yaml','yml'];
return ['yaml', 'yml'];
}

}

3 changes: 0 additions & 3 deletions src/Decoders/YmlDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

class YmlDecoder extends YamlDecoder implements DecoderInterface
{

/* Only needed to be able to build a decoder object with any Yaml file extension type.
* For instance, with Yaml files, the extension could be ".yaml" or ".yml". Thus, this
* class is needed for ".yml".
*/

}

6 changes: 2 additions & 4 deletions src/Exceptions/BadFileDataException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php
namespace Michaels\Manager\Exceptions;

/**
* BadFileDataException - used, when file data being added to the loaded is not compatible.
* @package Michaels\Manager
*/

namespace Michaels\Manager\Exceptions;


class BadFileDataException extends \InvalidArgumentException
{

Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/IncorrectDataException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
namespace Michaels\Manager\Exceptions;

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

}


1 change: 0 additions & 1 deletion src/Exceptions/JsonDecodingFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ class JsonDecodingFailedException extends \InvalidArgumentException
{

}

1 change: 0 additions & 1 deletion src/Exceptions/SerializationTypeNotSupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ class SerializationTypeNotSupportedException extends \InvalidArgumentException
{

}

6 changes: 4 additions & 2 deletions src/Exceptions/UnsupportedFilesException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace Michaels\Manager\Exceptions;


/**
* Class UnsupportedFilesException
* @package Michaels\Manager\Exceptions
*/
class UnsupportedFilesException extends \InvalidArgumentException
{

}

Loading

0 comments on commit 46b5c6a

Please sign in to comment.