* * * 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:----oj6gr7BzzafCBBu8m/XiHL+xcM2gaOJrNihtO5jN8/lAy68Pg5abYPquthWPgBb0qC6vcs1jP3pvSnWvhG5hxonTn1Sbx10FhJzesG3qMT0m6/3ewjRGNyoCi8JaHSqCg18i0fQnTv1JAErlqMjQCS8Aolu28rHUb6iAm3ldbshNw79U5ioD+w+wARPnwdRzxyExNRfHOW4/rHpp0+XV/gG8hb+lgXBF06L1Pbr/JvUq2O1Q2VqhbRH5O+HAy0zLwisO0pFjVGGkeDlgZdGvREzraBVtsuSi69etNuYfcKBfCyKoIjJz6tXg3MCas7mx8A0iA0OxLdBHdu6nbfAsFnzM8+RHkkZuFMPR3/UjRZE/57jV2YGQxmPfdhV1iY8DJZdAVPz5Mlzz1gI32tE9Q+7ejSpH9bvG82VPgzkG7C/cCNNxSWJ2/syrYIinJPdggxRTxqqECwBSrrPCv0zw67JZ/37FTwmSv6BZWwdaltwhfsCfxKv+hKN0ImFvQLjIC6dgsKUQzl0Otjv70dbYZsuDPCiJ3DFsXlPN5qzWtaMsvU3deh2vdtQAGIeA+VXq8NildJ0vW0DdklDUOo42oLcO4+ERR5d9FCGtySYa7g45qHw/KTJnpkM9D8MLeQcBvJbiZs5HOrdHGSul3UGQEEDoMLJyeZaaw6+RdzYCEcQ=----ATTACHMENT:----NjkxNDkxOTkyMjQ0NTAxMyAyODAwNzMzNjUxNjk5NDkgNzA5NzE0NzkyNjczOTY3Mg==