24use InvalidArgumentException;
29use UnexpectedValueException;
164 $this->maxSize = $maxSize;
173 $this->errorFormatter = $formatter;
194 self::META_TYPE =>
'assoc',
258 $last = array_pop(
$path );
259 $ret = &$this->path(
$path,
'dummy' );
260 if ( !isset( $ret[$last] ) ) {
262 } elseif ( is_array( $ret[$last] ) ) {
289 public static function setValue( array &$arr, $name, $value, $flags = 0 ) {
290 if ( ( $flags & self::NO_VALIDATE ) !== self::NO_VALIDATE ) {
291 $value = self::validateValue( $value );
294 if ( $name ===
null ) {
295 if ( $flags & self::ADD_ON_TOP ) {
296 array_unshift( $arr, $value );
303 $exists = isset( $arr[$name] );
304 if ( !$exists || ( $flags & self::OVERRIDE ) ) {
305 if ( !$exists && ( $flags & self::ADD_ON_TOP ) ) {
306 $arr = [ $name => $value ] + $arr;
308 $arr[$name] = $value;
310 } elseif ( is_array( $arr[$name] ) && is_array( $value ) ) {
311 $conflicts = array_intersect_key( $arr[$name], $value );
313 $arr[$name] += $value;
315 $keys = implode(
', ', array_keys( $conflicts ) );
316 throw new RuntimeException(
317 "Conflicting keys ($keys) when attempting to merge element $name"
320 } elseif ( $value !== $arr[$name] ) {
321 throw new RuntimeException(
322 "Attempting to add element $name=$value, existing value is {$arr[$name]}"
332 private static function validateValue( $value ) {
333 if ( is_object( $value ) ) {
336 if ( is_callable( [ $value,
'serializeForApiResult' ] ) ) {
338 $value = $value->serializeForApiResult();
339 if ( is_object( $value ) ) {
340 throw new UnexpectedValueException(
341 get_class( $oldValue ) .
'::serializeForApiResult() returned an object of class ' .
349 return self::validateValue( $value );
350 }
catch ( Exception $ex ) {
351 throw new UnexpectedValueException(
352 get_class( $oldValue ) .
'::serializeForApiResult() returned an invalid value: ' .
358 } elseif ( is_callable( [ $value,
'__toString' ] ) ) {
359 $value = (string)$value;
361 $value = (array)$value + [ self::META_TYPE =>
'assoc' ];
365 if ( is_string( $value ) ) {
367 static $contentLanguage =
null;
368 if ( !$contentLanguage ) {
371 $value = $contentLanguage->normalize( $value );
372 } elseif ( is_array( $value ) ) {
373 foreach ( $value as $k => $v ) {
374 $value[$k] = self::validateValue( $v );
376 } elseif ( $value !==
null && !is_scalar( $value ) ) {
377 $type = get_debug_type( $value );
378 throw new InvalidArgumentException(
"Cannot add $type to ApiResult" );
379 } elseif ( is_float( $value ) && !is_finite( $value ) ) {
380 throw new InvalidArgumentException(
'Cannot add non-finite floats to ApiResult' );
403 $arr = &$this->path(
$path, ( $flags & self::ADD_ON_TOP ) ?
'prepend' :
'append' );
405 if ( !( $flags & self::NO_SIZE_CHECK ) ) {
408 $value = self::validateValue( $value );
411 $newsize = $this->size + self::size( $value );
412 if ( $this->maxSize !==
false && $newsize > $this->maxSize ) {
413 $this->errorFormatter->addWarning(
418 $this->size = $newsize;
433 if ( isset( $arr[$name] ) ) {
435 unset( $arr[$name] );
452 if ( $name ===
null ) {
454 throw new InvalidArgumentException(
'Cannot remove the data root' );
456 $name = array_pop(
$path );
459 if ( !( $flags & self::NO_SIZE_CHECK ) ) {
460 $newsize = $this->size - self::size( $ret );
461 $this->size = max( $newsize, 0 );
476 if ( $name ===
null ) {
477 throw new InvalidArgumentException(
'Content value must be named' );
494 if ( $name ===
null ) {
495 throw new InvalidArgumentException(
'Content value must be named' );
510 $this->
addValue(
'limits', $moduleName, $limit,
511 self::OVERRIDE | self::NO_SIZE_CHECK );
529 if ( isset( $arr[self::META_CONTENT] ) &&
530 isset( $arr[$arr[self::META_CONTENT]] ) &&
531 !( $flags & self::OVERRIDE )
533 throw new RuntimeException(
534 "Attempting to set content element as $name when " . $arr[self::META_CONTENT] .
535 ' is already set as the content element'
550 $arr = &$this->path(
$path, ( $flags & self::ADD_ON_TOP ) ?
'prepend' :
'append' );
562 if ( !isset( $arr[self::META_SUBELEMENTS] ) ) {
577 $arr = &$this->path(
$path );
589 if ( isset( $arr[self::META_SUBELEMENTS] ) ) {
602 $arr = &$this->path(
$path );
613 if ( !is_string( $tag ) ) {
614 throw new InvalidArgumentException(
'Bad tag name' );
626 $arr = &$this->path(
$path );
638 if ( !is_string( $tag ) ) {
639 throw new InvalidArgumentException(
'Bad tag name' );
642 foreach ( $arr as $k => &$v ) {
643 if ( is_array( $v ) && !self::isMetadataKey( $k ) ) {
657 $arr = &$this->path(
$path );
672 if ( !isset( $arr[self::META_PRESERVE_KEYS] ) ) {
687 $arr = &$this->path(
$path );
699 if ( isset( $arr[self::META_PRESERVE_KEYS] ) ) {
712 $arr = &$this->path(
$path );
724 public static function setArrayType( array &$arr, $type, $kvpKeyName =
null ) {
725 if ( !in_array( $type, [
726 'default',
'array',
'assoc',
'kvp',
'BCarray',
'BCassoc',
'BCkvp'
728 throw new InvalidArgumentException(
'Bad type' );
731 if ( is_string( $kvpKeyName ) ) {
744 $arr = &$this->path(
$path );
757 foreach ( $arr as $k => &$v ) {
758 if ( is_array( $v ) && !self::isMetadataKey( $k ) ) {
772 $arr = &$this->path(
$path );
792 return ord( $key ) === 95;
805 $strip = $transforms[
'Strip'] ??
'none';
806 if ( $strip ===
'base' ) {
807 $transforms[
'Strip'] =
'none';
809 $transformTypes = $transforms[
'Types'] ??
null;
810 if ( $transformTypes !==
null && !is_array( $transformTypes ) ) {
811 throw new InvalidArgumentException( __METHOD__ .
':Value for "Types" must be an array' );
817 if ( isset( $transforms[
'Custom'] ) ) {
818 if ( !is_callable( $transforms[
'Custom'] ) ) {
819 throw new InvalidArgumentException( __METHOD__ .
': Value for "Custom" must be callable' );
821 call_user_func_array( $transforms[
'Custom'], [ &$data, &$metadata ] );
824 if ( ( isset( $transforms[
'BC'] ) || $transformTypes !==
null ) &&
825 isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] ===
'BCkvp' &&
826 !isset( $metadata[self::META_KVP_KEY_NAME] )
828 throw new UnexpectedValueException(
'Type "BCkvp" used without setting ' .
829 'ApiResult::META_KVP_KEY_NAME metadata item' );
834 if ( isset( $transforms[
'BC'] ) ) {
835 if ( !is_array( $transforms[
'BC'] ) ) {
836 throw new InvalidArgumentException( __METHOD__ .
':Value for "BC" must be an array' );
838 if ( !in_array(
'nobool', $transforms[
'BC'],
true ) ) {
839 $boolKeys = isset( $metadata[self::META_BC_BOOLS] )
840 ? array_fill_keys( $metadata[self::META_BC_BOOLS],
true )
844 if ( !in_array(
'no*', $transforms[
'BC'],
true ) &&
845 isset( $metadata[self::META_CONTENT] ) && $metadata[self::META_CONTENT] !==
'*'
848 $data[
'*'] = $data[$k];
853 if ( !in_array(
'nosub', $transforms[
'BC'],
true ) &&
854 isset( $metadata[self::META_BC_SUBELEMENTS] )
856 foreach ( $metadata[self::META_BC_SUBELEMENTS] as $k ) {
857 if ( isset( $data[$k] ) ) {
860 self::META_CONTENT =>
'*',
861 self::META_TYPE =>
'assoc',
867 if ( isset( $metadata[self::META_TYPE] ) ) {
868 switch ( $metadata[self::META_TYPE] ) {
881 $defaultType =
'array';
883 foreach ( $data as $k => &$v ) {
885 if ( $boolKeys !==
null && is_bool( $v ) && !isset( $boolKeys[$k] ) ) {
892 if ( is_string( $k ) ) {
893 $defaultType =
'assoc';
894 } elseif ( $k > $maxKey ) {
907 $keepMetadata = &$metadata;
911 $keepMetadata = array_intersect_key( $metadata, [
912 self::META_INDEXED_TAG_NAME => 1,
913 self::META_SUBELEMENTS => 1,
917 throw new InvalidArgumentException( __METHOD__ .
': Unknown value for "Strip"' );
921 if ( $transformTypes ===
null ) {
922 return $data + $keepMetadata;
925 if ( $defaultType ===
'array' && $maxKey !== count( $data ) - 1 ) {
926 $defaultType =
'assoc';
930 $type = $defaultType;
931 if ( isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] !==
'default' ) {
934 if ( ( $type ===
'kvp' || $type ===
'BCkvp' ) &&
935 empty( $transformTypes[
'ArmorKVP'] )
938 } elseif ( $type ===
'BCarray' ) {
940 } elseif ( $type ===
'BCassoc' ) {
948 $data += $keepMetadata;
949 return empty( $transformTypes[
'AssocAsObject'] ) ? $data : (object)$data;
961 uksort( $data,
static function ( $a, $b ):
int {
967 if ( is_numeric( trim( $a ) ) xor is_numeric( trim( $b ) ) ) {
968 return (
string)$a <=> (
string)$b;
974 $data = array_values( $data );
977 return $data + $keepMetadata;
982 $valKey = isset( $transforms[
'BC'] ) ?
'*' :
'value';
983 $assocAsObject = !empty( $transformTypes[
'AssocAsObject'] );
984 $merge = !empty( $metadata[self::META_KVP_MERGE] );
987 foreach ( $data as $k => $v ) {
988 if ( $merge && ( is_array( $v ) || is_object( $v ) ) ) {
990 if ( isset( $vArr[self::META_TYPE] ) ) {
992 } elseif ( is_object( $v ) ) {
993 $mergeType =
'assoc';
995 $keys = array_keys( $vArr );
996 sort( $keys, SORT_NUMERIC );
997 $mergeType = ( $keys === array_keys( $keys ) ) ?
'array' :
'assoc';
1002 if ( $mergeType ===
'assoc' ) {
1007 if ( $strip ===
'none' ) {
1015 if ( $strip ===
'none' ) {
1017 self::META_PRESERVE_KEYS => [ $key ],
1018 self::META_CONTENT => $valKey,
1019 self::META_TYPE =>
'assoc',
1023 $ret[] = $assocAsObject ? (object)$item : $item;
1028 return $ret + $keepMetadata;
1031 throw new UnexpectedValueException(
"Unknown type '$type'" );
1046 if ( is_array( $data ) || is_object( $data ) ) {
1047 $isObj = is_object( $data );
1049 $data = (array)$data;
1051 $preserveKeys = isset( $data[self::META_PRESERVE_KEYS] )
1052 ? (array)$data[self::META_PRESERVE_KEYS]
1054 foreach ( $data as $k => $v ) {
1055 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1057 } elseif ( is_array( $v ) || is_object( $v ) ) {
1062 $data = (object)$data;
1080 if ( !is_array( $metadata ) ) {
1083 if ( is_array( $data ) || is_object( $data ) ) {
1084 $isObj = is_object( $data );
1086 $data = (array)$data;
1088 $preserveKeys = isset( $data[self::META_PRESERVE_KEYS] )
1089 ? (array)$data[self::META_PRESERVE_KEYS]
1091 foreach ( $data as $k => $v ) {
1092 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1098 $data = (object)$data;
1110 private static function size( $value ) {
1112 if ( is_array( $value ) ) {
1113 foreach ( $value as $k => $v ) {
1114 if ( !self::isMetadataKey( $k ) ) {
1115 $s += self::size( $v );
1118 } elseif ( is_scalar( $value ) ) {
1119 $s = strlen( $value );
1136 private function &path(
$path, $create =
'append' ) {
1138 $ret = &$this->data;
1139 foreach (
$path as $i => $k ) {
1140 if ( !isset( $ret[$k] ) ) {
1141 switch ( $create ) {
1146 $ret = [ $k => [] ] + $ret;
1152 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1153 throw new InvalidArgumentException(
"Path $fail does not exist" );
1156 if ( !is_array( $ret[$k] ) ) {
1157 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1158 throw new InvalidArgumentException(
"Path $fail is not an array" );
1178 foreach ( $vars as $k => $v ) {
1179 if ( is_array( $v ) || is_object( $v ) ) {
1181 } elseif ( is_bool( $v ) ) {
1185 if ( is_string( $k ) ) {
1187 } elseif ( $k > $maxKey ) {
1191 if ( !$hash && $maxKey !== count( $vars ) - 1 ) {
1199 $keys = array_diff( array_keys( $vars ), [
1200 self::META_TYPE, self::META_PRESERVE_KEYS, self::META_KVP_KEY_NAME,
1201 self::META_INDEXED_TAG_NAME, self::META_BC_BOOLS
1205 self::META_TYPE =>
'kvp',
1206 self::META_KVP_KEY_NAME =>
'key',
1207 self::META_PRESERVE_KEYS => $keys,
1208 self::META_BC_BOOLS => $bools,
1209 self::META_INDEXED_TAG_NAME =>
'var',
1213 self::META_TYPE =>
'array',
1214 self::META_BC_BOOLS => $bools,
1215 self::META_INDEXED_TAG_NAME =>
'value',
1231 ->getReplicaDatabase()
1234 if ( $expiry ===
'' || $expiry ===
null || $expiry ===
false ||
1257class_alias( ApiResult::class,
'ApiResult' );
wfIsInfinity( $str)
Determine input string is represents as infinity.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.