Skip to content

Commit

Permalink
Merge pull request #9 from byjg/4.0.1
Browse files Browse the repository at this point in the history
Release 4.0.1
  • Loading branch information
byjg authored Apr 11, 2020
2 parents 6801abc + ccb0b68 commit 73be854
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php
php:
- "7.3"
- "7.2"
- "7.1"
- "7.0"
Expand Down
50 changes: 29 additions & 21 deletions src/AnyDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,46 @@ class AnyDataset
* Path to anydataset file
* @var string
*/
private $path;
private $filename;

/**
* @param string $file
* @param string $filename
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function __construct($file = null)
public function __construct($filename = null)
{
$this->collection = array();
$this->currentRow = -1;

$this->path = null;
$this->filename = null;
$this->defineSavePath($filename, function () {
if (!is_null($this->filename)) {
$this->createFrom($this->filename);
}
});
}

public function getFilename()
{
return $this->filename;
}

private function defineSavePath($file, $closure)
{
if (!is_null($file)) {
if (!is_string($file)) {
throw new \InvalidArgumentException('I expected a string as a file name');
}

$ext = pathinfo($file, PATHINFO_EXTENSION);
if (empty($ext)) {
if (empty($ext) && substr($file, 0, 6) !== "php://") {
$file .= '.anydata.xml';
}
$this->path = $file;

$this->createFrom($this->path);
$this->filename = $file;
}

$closure();
}

/**
Expand Down Expand Up @@ -151,25 +165,19 @@ public function getAsDom()
}

/**
* @param string $file
* @param string $filename
* @throws DatabaseException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function save($file = null)
public function save($filename = null)
{
if (!is_null($file)) {
if (!is_string($file)) {
throw new InvalidArgumentException('Invalid file name');
$this->defineSavePath($filename, function () {
if (is_null($this->filename)) {
throw new DatabaseException("No such file path to save anydataset");
}

$this->path = $file;
}

if (is_null($this->path)) {
throw new DatabaseException("No such file path to save anydataset");
}

XmlUtil::saveXmlDocument($this->getAsDom(), $this->path);
XmlUtil::saveXmlDocument($this->getAsDom(), $this->filename);
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/AnyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AnyIterator extends GenericIterator
/**
* Iterator constructor
*
*@param Row[] $list
* @param Row[] $list
*/

public function __construct($list)
Expand Down Expand Up @@ -53,7 +53,7 @@ public function hasNext()
/**
* Return the next row.
*
*@return Row
* @return Row
*/
public function moveNext()
{
Expand Down
15 changes: 15 additions & 0 deletions tests/AnyDatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public function testConstructorString()
"field2" => "othervalue2",
],
], $anydata->getIterator()->toArray());

$anydataMem = new AnyDataset("php://memory");
$anydataMem->import($anydata->getIterator());
$this->assertEquals(2, count($anydataMem->getIterator()->toArray()));
$this->assertEquals([
[
"field1" => "value1",
"field2" => "value2",
],
[
"field1" => "othervalue1",
"field2" => "othervalue2",
],
], $anydata->getIterator()->toArray());
$anydataMem->save();
}

public function testXML()
Expand Down

0 comments on commit 73be854

Please sign in to comment.