44
55use ByJG \AnyDataset \Core \Exception \DatabaseException ;
66use ByJG \AnyDataset \Core \Formatter \XmlFormatter ;
7+ use ByJG \XmlUtil \Exception \FileException ;
8+ use ByJG \XmlUtil \Exception \XmlUtilException ;
79use ByJG \XmlUtil \File ;
810use ByJG \XmlUtil \XmlDocument ;
911use ByJG \XmlUtil \XmlNode ;
12+ use Closure ;
1013use DOMElement ;
1114use InvalidArgumentException ;
1215
@@ -51,22 +54,22 @@ class AnyDataset
5154 *
5255 * @var Row[]
5356 */
54- private $ collection ;
57+ private array $ collection ;
5558
5659 /**
5760 * Current node anydataset works
5861 * @var int
5962 */
60- private $ currentRow ;
63+ private int $ currentRow ;
6164
6265 private ?File $ file ;
6366
6467 /**
65- * @param null| string $filename
66- * @throws \ByJG\Serializer\Exception\InvalidArgumentException
67- * @throws \ByJG\XmlUtil\Exception\ XmlUtilException
68+ * @param string|null $filename
69+ * @throws FileException
70+ * @throws XmlUtilException
6871 */
69- public function __construct ($ filename = null )
72+ public function __construct (? string $ filename = null )
7073 {
7174 $ this ->collection = array ();
7275 $ this ->currentRow = -1 ;
@@ -82,7 +85,7 @@ public function __construct($filename = null)
8285 /**
8386 * @return string|null
8487 */
85- public function getFilename ()
88+ public function getFilename (): ? string
8689 {
8790 return $ this ->file ->getFilename ();
8891 }
@@ -92,12 +95,13 @@ public function getFilename()
9295 * @param string|null $filename
9396 * @param mixed $closure
9497 * @return void
98+ * @throws FileException
9599 */
96- private function defineSavePath ($ filename , $ closure )
100+ private function defineSavePath (? string $ filename , Closure $ closure ): void
97101 {
98102 if (!is_null ($ filename )) {
99103 $ ext = pathinfo ($ filename , PATHINFO_EXTENSION );
100- if (empty ($ ext ) && substr ($ filename , 0 , 6 ) !== "php:// " ) {
104+ if (empty ($ ext ) && ! str_starts_with ($ filename , "php:// " ) ) {
101105 $ filename .= '.anydata.xml ' ;
102106 }
103107 $ this ->file = new File ($ filename , allowNotFound: true );
@@ -109,12 +113,10 @@ private function defineSavePath($filename, $closure)
109113 /**
110114 * Private method used to read and populate anydataset class from specified file
111115 *
112- * @param string $filepath Path and Filename to be read
113116 * @return void
114- * @throws \ByJG\Serializer\Exception\InvalidArgumentException
115- * @throws \ByJG\XmlUtil\Exception\XmlUtilException
117+ * @throws XmlUtilException
116118 */
117- private function createFromFile ()
119+ private function createFromFile (): void
118120 {
119121 if (file_exists ($ this ->getFilename ())) {
120122 $ anyDataSet = new XmlDocument ($ this ->file );
@@ -152,9 +154,9 @@ public function xml(): string
152154 * @param string|null $filename
153155 * @return void
154156 * @throws DatabaseException
155- * @throws \ByJG\XmlUtil\Exception\XmlUtilException
157+ * @throws FileException
156158 */
157- public function save ($ filename = null )
159+ public function save (? string $ filename = null ): void
158160 {
159161 $ this ->defineSavePath ($ filename , function () use ($ filename ){
160162 if (is_null ($ this ->file )) {
@@ -168,11 +170,10 @@ public function save($filename = null)
168170 /**
169171 * Append one row to AnyDataset.
170172 *
171- * @param Row|array|\stdClass|object|null $singleRow
173+ * @param Row|array $singleRow
172174 * @return void
173- * @throws \ByJG\Serializer\Exception\InvalidArgumentException
174175 */
175- public function appendRow ($ singleRow = [])
176+ public function appendRow (Row | array $ singleRow = []): void
176177 {
177178 if (!empty ($ singleRow )) {
178179 if ($ singleRow instanceof Row) {
@@ -196,9 +197,8 @@ public function appendRow($singleRow = [])
196197 *
197198 * @param GenericIterator $iterator
198199 * @return void
199- * @throws \ByJG\Serializer\Exception\InvalidArgumentException
200200 */
201- public function import ($ iterator )
201+ public function import (GenericIterator $ iterator ): void
202202 {
203203 foreach ($ iterator as $ singleRow ) {
204204 $ this ->appendRow ($ singleRow );
@@ -209,11 +209,9 @@ public function import($iterator)
209209 * Insert one row before specified position.
210210 *
211211 * @param int $rowNumber
212- * @param Row|array|\stdClass|object $row
213- * @return void
214- * @throws \ByJG\Serializer\Exception\InvalidArgumentException
212+ * @param Row|array $row
215213 */
216- public function insertRowBefore ($ rowNumber , $ row )
214+ public function insertRowBefore (int $ rowNumber , Row | array $ row ): void
217215 {
218216 if ($ rowNumber > count ($ this ->collection )) {
219217 $ this ->appendRow ($ row );
@@ -236,11 +234,11 @@ public function insertRowBefore($rowNumber, $row)
236234
237235 /**
238236 *
239- * @param mixed $row
237+ * @param int|Row|null $row
240238 * @return void
241239 * @throws \ByJG\Serializer\Exception\InvalidArgumentException
242240 */
243- public function removeRow ($ row = null )
241+ public function removeRow (int | Row $ row = null ): void
244242 {
245243 if (is_null ($ row )) {
246244 $ row = $ this ->currentRow ;
@@ -270,9 +268,8 @@ public function removeRow($row = null)
270268 * @param string $name - Field name
271269 * @param string $value - Field value
272270 * @return void
273- * @throws \ByJG\Serializer\Exception\InvalidArgumentException
274271 */
275- public function addField ($ name , $ value )
272+ public function addField (string $ name , mixed $ value ): void
276273 {
277274 if ($ this ->currentRow < 0 ) {
278275 $ this ->appendRow ();
@@ -282,10 +279,10 @@ public function addField($name, $value)
282279
283280 /**
284281 * Get an Iterator filtered by an IteratorFilter
285- * @param IteratorFilter $itf
286- * @return GenericIterator
282+ * @param IteratorFilter|null $itf
283+ * @return GenericIterator|AnyIterator
287284 */
288- public function getIterator (IteratorFilter $ itf = null )
285+ public function getIterator (IteratorFilter $ itf = null ): GenericIterator | AnyIterator
289286 {
290287 if (is_null ($ itf )) {
291288 return new AnyIterator ($ this ->collection );
@@ -298,10 +295,10 @@ public function getIterator(IteratorFilter $itf = null)
298295 * Undocumented function
299296 *
300297 * @param string $fieldName
301- * @param IteratorFilter $itf
298+ * @param IteratorFilter|null $itf
302299 * @return array
303300 */
304- public function getArray ($ fieldName , $ itf = null )
301+ public function getArray (string $ fieldName , IteratorFilter $ itf = null ): array
305302 {
306303 $ iterator = $ this ->getIterator ($ itf );
307304 $ result = array ();
@@ -316,7 +313,7 @@ public function getArray($fieldName, $itf = null)
316313 * @param string $field
317314 * @return void
318315 */
319- public function sort ($ field )
316+ public function sort (string $ field ): void
320317 {
321318 if (count ($ this ->collection ) == 0 ) {
322319 return ;
@@ -330,7 +327,7 @@ public function sort($field)
330327 * @param string $field
331328 * @return array
332329 */
333- protected function quickSortExec ($ seq , $ field )
330+ protected function quickSortExec (array $ seq , string $ field ): array
334331 {
335332 if (!count ($ seq )) {
336333 return $ seq ;
0 commit comments