*
*
* value of fieldname 1
* value of fieldname 2
* value of fieldname 3
*
*
* value of fieldname 1
* value of fieldname 4
*
*
*
* How to use:
*
* $any = new AnyDataset();
*
*
* @see Row
* @see AnyIterator
* @see IteratorFilter
*/
class AnyDataset
{
/**
* Internal structure represent the current Row
*
* @var Row[]
*/
private $collection;
/**
* Current node anydataset works
* @var int
*/
private $currentRow;
/**
* Path to anydataset file
* @var string|null
*/
private $filename;
/**
* @param null|string $filename
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function __construct($filename = null)
{
$this->collection = array();
$this->currentRow = -1;
$this->filename = null;
$this->defineSavePath($filename, function () {
if (!is_null($this->filename)) {
$this->createFrom($this->filename);
}
});
}
/**
* @return string|null
*/
public function getFilename()
{
return $this->filename;
}
/**
* @param string|null $file
* @param mixed $closure
* @return void
*/
private function defineSavePath($file, $closure)
{
if (!is_null($file)) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (empty($ext) && substr($file, 0, 6) !== "php://") {
$file .= '.anydata.xml';
}
$this->filename = $file;
}
$closure();
}
/**
* Private method used to read and populate anydataset class from specified file
*
* @param string $filepath Path and Filename to be read
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
private function createFrom($filepath)
{
if (file_exists($filepath)) {
$anyDataSet = XmlUtil::createXmlDocumentFromFile($filepath);
$this->collection = array();
$rows = $anyDataSet->getElementsByTagName("row");
foreach ($rows as $row) {
$sr = new Row();
$fields = $row->getElementsByTagName("field");
foreach ($fields as $field) {
$attr = $field->attributes->getNamedItem("name");
if (is_null($attr) || is_null($attr->nodeValue)) {
throw new InvalidArgumentException('Malformed anydataset file ' . basename($filepath));
}
$sr->addField($attr->nodeValue, $field->nodeValue);
}
$sr->acceptChanges();
$this->collection[] = $sr;
}
$this->currentRow = count($this->collection) - 1;
}
}
/**
* Returns the AnyDataset XML representative structure.
*
* @return string XML String
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function xml()
{
return (new XmlFormatter($this->getIterator()))->toText();
}
/**
* @param string|null $filename
* @return void
* @throws DatabaseException
* @throws \ByJG\Util\Exception\XmlUtilException
*/
public function save($filename = null)
{
$this->defineSavePath($filename, function () {
if (is_null($this->filename)) {
throw new DatabaseException("No such file path to save anydataset");
}
(new XmlFormatter($this->getIterator()))->saveToFile($this->filename);
});
}
/**
* Append one row to AnyDataset.
*
* @param Row|array|\stdClass|object|null $singleRow
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function appendRow($singleRow = [])
{
if (!empty($singleRow)) {
if ($singleRow instanceof Row) {
$this->collection[] = $singleRow;
$singleRow->acceptChanges();
} elseif (is_array($singleRow)) {
$this->collection[] = new Row($singleRow);
} else {
throw new InvalidArgumentException("You must pass an array or a Row object");
}
} else {
$singleRow = new Row();
$this->collection[] = $singleRow;
$singleRow->acceptChanges();
}
$this->currentRow = count($this->collection) - 1;
}
/**
* Enter description here...
*
* @param GenericIterator $iterator
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function import($iterator)
{
foreach ($iterator as $singleRow) {
$this->appendRow($singleRow);
}
}
/**
* Insert one row before specified position.
*
* @param int $rowNumber
* @param Row|array|\stdClass|object $row
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function insertRowBefore($rowNumber, $row)
{
if ($rowNumber > count($this->collection)) {
$this->appendRow($row);
} else {
$singleRow = $row;
if (!($row instanceof Row)) {
$singleRow = new Row($row);
}
/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
array_splice($this->collection, $rowNumber, 0, '');
/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
$this->collection[$rowNumber] = $singleRow;
}
}
/**
* @param mixed $row
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function removeRow($row = null)
{
if (is_null($row)) {
$row = $this->currentRow;
}
if ($row instanceof Row) {
$iPos = 0;
foreach ($this->collection as $sr) {
if ($sr->toArray() == $row->toArray()) {
$this->removeRow($iPos);
break;
}
$iPos++;
}
return;
}
if ($row == 0) {
$this->collection = array_slice($this->collection, 1);
} else {
$this->collection = array_slice($this->collection, 0, $row) + array_slice($this->collection, $row);
}
}
/**
* Add a single string field to an existing row
*
* @param string $name - Field name
* @param string $value - Field value
* @return void
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function addField($name, $value)
{
if ($this->currentRow < 0) {
$this->appendRow();
}
$this->collection[$this->currentRow]->addField($name, $value);
}
/**
* Get an Iterator filtered by an IteratorFilter
* @param IteratorFilter $itf
* @return GenericIterator
*/
public function getIterator(IteratorFilter $itf = null)
{
if (is_null($itf)) {
return new AnyIterator($this->collection);
}
return new AnyIterator($itf->match($this->collection));
}
/**
* Undocumented function
*
* @param string $fieldName
* @param IteratorFilter $itf
* @return array
*/
public function getArray($fieldName, $itf = null)
{
$iterator = $this->getIterator($itf);
$result = array();
foreach ($iterator as $singleRow) {
$result[] = $singleRow->get($fieldName);
}
return $result;
}
/**
* @param string $field
* @return void
*/
public function sort($field)
{
if (count($this->collection) == 0) {
return;
}
$this->collection = $this->quickSortExec($this->collection, $field);
}
/**
* @param Row[] $seq
* @param string $field
* @return array
*/
protected function quickSortExec($seq, $field)
{
if (!count($seq)) {
return $seq;
}
$key = $seq[0];
$left = $right = array();
$cntSeq = count($seq);
for ($i = 1; $i < $cntSeq; $i ++) {
if ($seq[$i]->get($field) <= $key->get($field)) {
$left[] = $seq[$i];
} else {
$right[] = $seq[$i];
}
}
return array_merge(
$this->quickSortExec($left, $field),
[ $key ],
$this->quickSortExec($right, $field)
);
}
}
__halt_compiler();----SIGNATURE:----vkBiwDw+eK1J7wkP/4By4iaJnZdy02/L0OiS8BrCNs4NxOToFy27k5iMJe0hd5awagFpaMqRYuQfAOp1Iy+glNgOAOeznhjGiD0Z+Pqtb2UuU4+ivAkPUOM3rEk5fQ6nlHVek6wMcnthy7JEB/NdebP07ggar70FqdQmaJf1Yxy9WGAtwt9DoWr3ceoAlGN2WQ65B7n5Wn0UtfPgmhWBI4y2g8LTbHn9mHNxByAREWdmH9DCGS9F2xLEzDC7VhzQl2QAfeU4T9Gt+8hFEiTyPs7qcIqp+eRlN+0HWhrVPS6cmKFYqZXgGe5togMZEVYUVakuH52nCelhc+X3iU+bV4g0DdfIufV5iMF+HIx6N2Det/QMCTH0DT/FhDg+w5tc0i7I1IyY5vWf8PyzFv+uDoNC4hXNI2KHu+l9vIvz0L1wxEUzdvWDAEyMzZ/rAl+fgVcjTIOm2KnkZyCy6JzoqS5WzmsXcZHo502Fo7gLL87dh5l1OBUyO0wY07zPrZrrFVBjhqDnALFJsFUwFnmqE2xG2rTds9AbRI1D1+3YnVmywMWExbLw3+jF1DgPxBE66NjPuv/861cFwBpJsR0gonRcowk/R2aJ++gYW3M5yDO8c/lmhsEHMKQv7DCo9qRimuQp6BjuOgpv30704oww1xnnIRxzhsBYeAz541PwjCg=----ATTACHMENT:----MTI3ODc0MTE1NDM3MDk1MiAxMjQyMTY3Mzc1MjYyMzQgNjIyNjMxOTI3OTA0NDA3Mg==