diff --git a/.travis.yml b/.travis.yml index c44797f..2e8d81b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: php php: + - "7.3" - "7.2" - "7.1" - "7.0" diff --git a/src/AnyDataset.php b/src/AnyDataset.php index 6aa2e01..502ded7 100644 --- a/src/AnyDataset.php +++ b/src/AnyDataset.php @@ -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(); } /** @@ -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); + }); } /** diff --git a/src/AnyIterator.php b/src/AnyIterator.php index 3509522..4e70666 100644 --- a/src/AnyIterator.php +++ b/src/AnyIterator.php @@ -23,7 +23,7 @@ class AnyIterator extends GenericIterator /** * Iterator constructor * -*@param Row[] $list + * @param Row[] $list */ public function __construct($list) @@ -53,7 +53,7 @@ public function hasNext() /** * Return the next row. * -*@return Row + * @return Row */ public function moveNext() { diff --git a/tests/AnyDatasetTest.php b/tests/AnyDatasetTest.php index 2a1594e..0fefcbe 100644 --- a/tests/AnyDatasetTest.php +++ b/tests/AnyDatasetTest.php @@ -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()