@@ -92,7 +92,7 @@ public static function parseValue($value)
92
92
{
93
93
$ value = (string )$ value ;
94
94
95
- if ('' === $ value ) {
95
+ if (in_array ( $ value , array ( '' , ' nil ' ), true ) ) {
96
96
return null ;
97
97
} elseif (in_array ($ value , array ('true ' , 'false ' , 'yes ' , 'no ' ), true )) {
98
98
return $ value === 'true ' || $ value === 'yes ' ;
@@ -207,7 +207,7 @@ public static function escapeValue($value)
207
207
if ('000000 ' === $ usec ) {
208
208
unset($ usec );
209
209
}
210
- $ unixEpoch = new DateTime ('1970-01-01 00:00:00.000000 ' );
210
+ $ unixEpoch = new DateTime ('@0 ' );
211
211
$ value = $ unixEpoch ->diff ($ value );
212
212
}
213
213
if ($ value instanceof DateInterval) {
@@ -480,7 +480,7 @@ public function find()
480
480
*
481
481
* @return string|null|bool The value of the specified property. If the
482
482
* property is not set, NULL will be returned. If no such item exists,
483
- * FALSE will be returned.
483
+ * or the property name is not valid, FALSE will be returned.
484
484
*/
485
485
public function get ($ number , $ value_name )
486
486
{
@@ -493,16 +493,28 @@ public function get($number, $value_name)
493
493
}
494
494
}
495
495
496
- $ request = new Request ($ this ->menu . '/print ' );
496
+ //For new RouterOS versions
497
+ $ request = new Request ($ this ->menu . '/get ' );
498
+ $ request ->setArgument ('number ' , $ number );
499
+ $ request ->setArgument ('value-name ' , $ value_name );
500
+ $ responses = $ this ->client ->sendSync ($ request );
501
+ if (Response::TYPE_ERROR === $ responses ->getType ()) {
502
+ return false ;
503
+ }
504
+ $ result = $ responses ->getArgument ('ret ' );
505
+ if (null !== $ result ) {
506
+ return $ result ;
507
+ }
508
+
509
+ // The "get" of old RouterOS versions return an empty !done response.
510
+ // This is a backup for them, although this should also execute on new
511
+ // versions when a valid property is not set.
512
+ $ query = null ;
497
513
if (null !== $ number ) {
498
514
$ number = (string )$ number ;
499
- $ request ->setQuery (
500
- Query::where ('.id ' , $ number )->orWhere ('name ' , $ number )
501
- );
515
+ $ query = Query::where ('.id ' , $ number )->orWhere ('name ' , $ number );
502
516
}
503
- $ request ->setArgument ('.proplist ' , $ value_name );
504
- $ responses = $ this ->client ->sendSync ($ request )
505
- ->getAllOfType (Response::TYPE_DATA );
517
+ $ responses = $ this ->getall (array ('.proplist ' => $ value_name ), $ query );
506
518
507
519
if (0 === count ($ responses )) {
508
520
return false ;
@@ -691,6 +703,68 @@ public function move($numbers, $destination)
691
703
return $ this ->client ->sendSync ($ moveRequest );
692
704
}
693
705
706
+ /**
707
+ * Counts items at the current menu.
708
+ *
709
+ * Counts items at the current menu. This executes a dedicated command
710
+ * ("print" with a "count-only" argument) on RouterOS, which is why only
711
+ * queries are allowed as a criteria, in contrast with {@link find()},
712
+ * where numbers and callbacks are allowed also.
713
+ *
714
+ * @param Query $query A query to filter items by. Without it, all items
715
+ * are included in the count.
716
+ *
717
+ * @return int The number of items, or -1 on failure (e.g. if the
718
+ * current menu does not have a "print" command or items to be counted).
719
+ */
720
+ public function count (Query $ query = null )
721
+ {
722
+ $ result = self ::parseValue (
723
+ $ this ->client ->sendSync (
724
+ new Request ($ this ->menu . '/print count-only="" ' , $ query )
725
+ )->getLast ()->getArgument ('ret ' )
726
+ );
727
+
728
+ if (null === $ result ) {
729
+ return -1 ;
730
+ }
731
+ return $ result ;
732
+ }
733
+
734
+ /**
735
+ * Gets all items in the current menu.
736
+ *
737
+ * Gets all items in the current menu, using a print request.
738
+ *
739
+ * @param string[] $args Additional arguments to pass to the request.
740
+ * Each array key is the name of the argument, and each array value is
741
+ * the value of the argument to be passed. Empty arguments can also be
742
+ * specified using a numeric key, and the name of the argument as the
743
+ * array value.
744
+ * @param Query $query A query to filter items by.
745
+ *
746
+ * @return ResponseCollection|bool A response collection with all
747
+ * {@link Response::TYPE_DATA} responses. The collection will be empty
748
+ * when there are no matching items. FALSE on failure.
749
+ */
750
+ public function getall (array $ args = array (), Query $ query = null )
751
+ {
752
+ $ printRequest = new Request ($ this ->menu . '/print ' , $ query );
753
+ foreach ($ args as $ name => $ value ) {
754
+ if (is_int ($ name )) {
755
+ $ printRequest ->setArgument ($ value );
756
+ } else {
757
+ $ printRequest ->setArgument ($ name , $ value );
758
+ }
759
+ }
760
+ $ responses = $ this ->client ->sendSync ($ printRequest );
761
+
762
+ if (count ($ responses ->getAllOfType (Response::TYPE_ERROR )) > 0 ) {
763
+ return false ;
764
+ }
765
+ return $ responses ->getAllOfType (Response::TYPE_DATA );
766
+ }
767
+
694
768
/**
695
769
* Puts a file on RouterOS's file system.
696
770
*
@@ -866,66 +940,4 @@ private function _exec(
866
940
867
941
return $ result ;
868
942
}
869
-
870
- /**
871
- * Counts items at the current menu.
872
- *
873
- * Counts items at the current menu. This executes a dedicated command
874
- * ("print" with a "count-only" argument) on RouterOS, which is why only
875
- * queries are allowed as a criteria, in contrast with {@link find()},
876
- * where numbers and callbacks are allowed also.
877
- *
878
- * @param Query $query A query to filter items by. Without it, all items
879
- * are included in the count.
880
- *
881
- * @return int The number of items, or -1 on failure (e.g. if the
882
- * current menu does not have a "print" command or items to be counted).
883
- */
884
- public function count (Query $ query = null )
885
- {
886
- $ result = self ::parseValue (
887
- $ this ->client ->sendSync (
888
- new Request ($ this ->menu . '/print count-only="" ' , $ query )
889
- )->getLast ()->getArgument ('ret ' )
890
- );
891
-
892
- if (null === $ result ) {
893
- return -1 ;
894
- }
895
- return $ result ;
896
- }
897
-
898
- /**
899
- * Gets all items in the current menu.
900
- *
901
- * Gets all items in the current menu, using a print request.
902
- *
903
- * @param string[] $args Additional arguments to pass to the request.
904
- * Each array key is the name of the argument, and each array value is
905
- * the value of the argument to be passed. Empty arguments can also be
906
- * specified using a numeric key, and the name of the argument as the
907
- * array value.
908
- * @param Query $query A query to filter items by.
909
- *
910
- * @return ResponseCollection|bool A response collection with all
911
- * {@link Response::TYPE_DATA} responses. The collection will be empty
912
- * when there are no matching items. FALSE on failure.
913
- */
914
- public function getall (array $ args = array (), Query $ query = null )
915
- {
916
- $ printRequest = new Request ($ this ->menu . '/print ' , $ query );
917
- foreach ($ args as $ name => $ value ) {
918
- if (is_int ($ name )) {
919
- $ printRequest ->setArgument ($ value );
920
- } else {
921
- $ printRequest ->setArgument ($ name , $ value );
922
- }
923
- }
924
- $ responses = $ this ->client ->sendSync ($ printRequest );
925
-
926
- if (!empty ($ responses ->getAllOfType (Response::TYPE_ERROR ))) {
927
- return false ;
928
- }
929
- return $ responses ->getAllOfType (Response::TYPE_DATA );
930
- }
931
943
}
0 commit comments