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 ( $value instanceof \
Wikimedia\Message\MessageParam ) {
362 $value = $value->getType() ===
'text' ? $value->getValue() : $codec->serialize( $value );
363 } elseif ( is_callable( [ $value,
'__toString' ] ) ) {
364 $value = (string)$value;
366 $value = (array)$value + [ self::META_TYPE =>
'assoc' ];
370 if ( is_string( $value ) ) {
372 static $contentLanguage =
null;
373 if ( !$contentLanguage ) {
376 $value = $contentLanguage->normalize( $value );
377 } elseif ( is_array( $value ) ) {
378 foreach ( $value as $k => $v ) {
379 $value[$k] = self::validateValue( $v );
381 } elseif ( $value !==
null && !is_scalar( $value ) ) {
382 $type = get_debug_type( $value );
383 throw new InvalidArgumentException(
"Cannot add $type to ApiResult" );
384 } elseif ( is_float( $value ) && !is_finite( $value ) ) {
385 throw new InvalidArgumentException(
'Cannot add non-finite floats to ApiResult' );
408 $arr = &$this->path(
$path, ( $flags & self::ADD_ON_TOP ) ?
'prepend' :
'append' );
410 if ( !( $flags & self::NO_SIZE_CHECK ) ) {
413 $value = self::validateValue( $value );
416 $newsize = $this->size + self::size( $value );
417 if ( $this->maxSize !==
false && $newsize > $this->maxSize ) {
418 $this->errorFormatter->addWarning(
423 $this->size = $newsize;
438 if ( isset( $arr[$name] ) ) {
440 unset( $arr[$name] );
457 if ( $name ===
null ) {
459 throw new InvalidArgumentException(
'Cannot remove the data root' );
461 $name = array_pop(
$path );
464 if ( !( $flags & self::NO_SIZE_CHECK ) ) {
465 $newsize = $this->size - self::size( $ret );
466 $this->size = max( $newsize, 0 );
481 if ( $name ===
null ) {
482 throw new InvalidArgumentException(
'Content value must be named' );
499 if ( $name ===
null ) {
500 throw new InvalidArgumentException(
'Content value must be named' );
515 $this->
addValue(
'limits', $moduleName, $limit,
516 self::OVERRIDE | self::NO_SIZE_CHECK );
534 if ( isset( $arr[self::META_CONTENT] ) &&
535 isset( $arr[$arr[self::META_CONTENT]] ) &&
536 !( $flags & self::OVERRIDE )
538 throw new RuntimeException(
539 "Attempting to set content element as $name when " . $arr[self::META_CONTENT] .
540 ' is already set as the content element'
555 $arr = &$this->path(
$path, ( $flags & self::ADD_ON_TOP ) ?
'prepend' :
'append' );
567 if ( !isset( $arr[self::META_SUBELEMENTS] ) ) {
582 $arr = &$this->path(
$path );
594 if ( isset( $arr[self::META_SUBELEMENTS] ) ) {
607 $arr = &$this->path(
$path );
618 if ( !is_string( $tag ) ) {
619 throw new InvalidArgumentException(
'Bad tag name' );
631 $arr = &$this->path(
$path );
643 if ( !is_string( $tag ) ) {
644 throw new InvalidArgumentException(
'Bad tag name' );
647 foreach ( $arr as $k => &$v ) {
648 if ( is_array( $v ) && !self::isMetadataKey( $k ) ) {
662 $arr = &$this->path(
$path );
677 if ( !isset( $arr[self::META_PRESERVE_KEYS] ) ) {
692 $arr = &$this->path(
$path );
704 if ( isset( $arr[self::META_PRESERVE_KEYS] ) ) {
717 $arr = &$this->path(
$path );
729 public static function setArrayType( array &$arr, $type, $kvpKeyName =
null ) {
730 if ( !in_array( $type, [
731 'default',
'array',
'assoc',
'kvp',
'BCarray',
'BCassoc',
'BCkvp'
733 throw new InvalidArgumentException(
'Bad type' );
736 if ( is_string( $kvpKeyName ) ) {
749 $arr = &$this->path(
$path );
762 foreach ( $arr as $k => &$v ) {
763 if ( is_array( $v ) && !self::isMetadataKey( $k ) ) {
777 $arr = &$this->path(
$path );
797 return ord( $key ) === 95;
810 $strip = $transforms[
'Strip'] ??
'none';
811 if ( $strip ===
'base' ) {
812 $transforms[
'Strip'] =
'none';
814 $transformTypes = $transforms[
'Types'] ??
null;
815 if ( $transformTypes !==
null && !is_array( $transformTypes ) ) {
816 throw new InvalidArgumentException( __METHOD__ .
':Value for "Types" must be an array' );
822 if ( isset( $transforms[
'Custom'] ) ) {
823 if ( !is_callable( $transforms[
'Custom'] ) ) {
824 throw new InvalidArgumentException( __METHOD__ .
': Value for "Custom" must be callable' );
826 call_user_func_array( $transforms[
'Custom'], [ &$data, &$metadata ] );
829 if ( ( isset( $transforms[
'BC'] ) || $transformTypes !==
null ) &&
830 isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] ===
'BCkvp' &&
831 !isset( $metadata[self::META_KVP_KEY_NAME] )
833 throw new UnexpectedValueException(
'Type "BCkvp" used without setting ' .
834 'ApiResult::META_KVP_KEY_NAME metadata item' );
839 if ( isset( $transforms[
'BC'] ) ) {
840 if ( !is_array( $transforms[
'BC'] ) ) {
841 throw new InvalidArgumentException( __METHOD__ .
':Value for "BC" must be an array' );
843 if ( !in_array(
'nobool', $transforms[
'BC'],
true ) ) {
844 $boolKeys = isset( $metadata[self::META_BC_BOOLS] )
845 ? array_fill_keys( $metadata[self::META_BC_BOOLS],
true )
849 if ( !in_array(
'no*', $transforms[
'BC'],
true ) &&
850 isset( $metadata[self::META_CONTENT] ) && $metadata[self::META_CONTENT] !==
'*'
853 $data[
'*'] = $data[$k];
858 if ( !in_array(
'nosub', $transforms[
'BC'],
true ) &&
859 isset( $metadata[self::META_BC_SUBELEMENTS] )
861 foreach ( $metadata[self::META_BC_SUBELEMENTS] as $k ) {
862 if ( isset( $data[$k] ) ) {
865 self::META_CONTENT =>
'*',
866 self::META_TYPE =>
'assoc',
872 if ( isset( $metadata[self::META_TYPE] ) ) {
873 switch ( $metadata[self::META_TYPE] ) {
886 $defaultType =
'array';
888 foreach ( $data as $k => &$v ) {
890 if ( $boolKeys !==
null && is_bool( $v ) && !isset( $boolKeys[$k] ) ) {
897 if ( is_string( $k ) ) {
898 $defaultType =
'assoc';
899 } elseif ( $k > $maxKey ) {
912 $keepMetadata = &$metadata;
916 $keepMetadata = array_intersect_key( $metadata, [
917 self::META_INDEXED_TAG_NAME => 1,
918 self::META_SUBELEMENTS => 1,
922 throw new InvalidArgumentException( __METHOD__ .
': Unknown value for "Strip"' );
926 if ( $transformTypes ===
null ) {
927 return $data + $keepMetadata;
930 if ( $defaultType ===
'array' && $maxKey !== count( $data ) - 1 ) {
931 $defaultType =
'assoc';
935 $type = $defaultType;
936 if ( isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] !==
'default' ) {
939 if ( ( $type ===
'kvp' || $type ===
'BCkvp' ) &&
940 empty( $transformTypes[
'ArmorKVP'] )
943 } elseif ( $type ===
'BCarray' ) {
945 } elseif ( $type ===
'BCassoc' ) {
953 $data += $keepMetadata;
954 return empty( $transformTypes[
'AssocAsObject'] ) ? $data : (object)$data;
966 uksort( $data,
static function ( $a, $b ):
int {
972 if ( is_numeric( trim( $a ) ) xor is_numeric( trim( $b ) ) ) {
973 return (
string)$a <=> (
string)$b;
979 $data = array_values( $data );
982 return $data + $keepMetadata;
987 $valKey = isset( $transforms[
'BC'] ) ?
'*' :
'value';
988 $assocAsObject = !empty( $transformTypes[
'AssocAsObject'] );
989 $merge = !empty( $metadata[self::META_KVP_MERGE] );
992 foreach ( $data as $k => $v ) {
993 if ( $merge && ( is_array( $v ) || is_object( $v ) ) ) {
995 if ( isset( $vArr[self::META_TYPE] ) ) {
997 } elseif ( is_object( $v ) ) {
998 $mergeType =
'assoc';
1000 $keys = array_keys( $vArr );
1001 sort( $keys, SORT_NUMERIC );
1002 $mergeType = ( $keys === array_keys( $keys ) ) ?
'array' :
'assoc';
1007 if ( $mergeType ===
'assoc' ) {
1012 if ( $strip ===
'none' ) {
1020 if ( $strip ===
'none' ) {
1022 self::META_PRESERVE_KEYS => [ $key ],
1023 self::META_CONTENT => $valKey,
1024 self::META_TYPE =>
'assoc',
1028 $ret[] = $assocAsObject ? (object)$item : $item;
1033 return $ret + $keepMetadata;
1036 throw new UnexpectedValueException(
"Unknown type '$type'" );
1051 if ( is_array( $data ) || is_object( $data ) ) {
1052 $isObj = is_object( $data );
1054 $data = (array)$data;
1056 $preserveKeys = isset( $data[self::META_PRESERVE_KEYS] )
1057 ? (array)$data[self::META_PRESERVE_KEYS]
1059 foreach ( $data as $k => $v ) {
1060 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1062 } elseif ( is_array( $v ) || is_object( $v ) ) {
1067 $data = (object)$data;
1085 if ( !is_array( $metadata ) ) {
1088 if ( is_array( $data ) || is_object( $data ) ) {
1089 $isObj = is_object( $data );
1091 $data = (array)$data;
1093 $preserveKeys = isset( $data[self::META_PRESERVE_KEYS] )
1094 ? (array)$data[self::META_PRESERVE_KEYS]
1096 foreach ( $data as $k => $v ) {
1097 if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys,
true ) ) {
1103 $data = (object)$data;
1115 private static function size( $value ) {
1117 if ( is_array( $value ) ) {
1118 foreach ( $value as $k => $v ) {
1119 if ( !self::isMetadataKey( $k ) ) {
1120 $s += self::size( $v );
1123 } elseif ( is_scalar( $value ) ) {
1124 $s = strlen( $value );
1141 private function &path(
$path, $create =
'append' ) {
1143 $ret = &$this->data;
1144 foreach (
$path as $i => $k ) {
1145 if ( !isset( $ret[$k] ) ) {
1146 switch ( $create ) {
1151 $ret = [ $k => [] ] + $ret;
1157 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1158 throw new InvalidArgumentException(
"Path $fail does not exist" );
1161 if ( !is_array( $ret[$k] ) ) {
1162 $fail = implode(
'.', array_slice(
$path, 0, $i + 1 ) );
1163 throw new InvalidArgumentException(
"Path $fail is not an array" );
1183 foreach ( $vars as $k => $v ) {
1184 if ( is_array( $v ) || is_object( $v ) ) {
1186 } elseif ( is_bool( $v ) ) {
1190 if ( is_string( $k ) ) {
1192 } elseif ( $k > $maxKey ) {
1196 if ( !$hash && $maxKey !== count( $vars ) - 1 ) {
1204 $keys = array_diff( array_keys( $vars ), [
1205 self::META_TYPE, self::META_PRESERVE_KEYS, self::META_KVP_KEY_NAME,
1206 self::META_INDEXED_TAG_NAME, self::META_BC_BOOLS
1210 self::META_TYPE =>
'kvp',
1211 self::META_KVP_KEY_NAME =>
'key',
1212 self::META_PRESERVE_KEYS => $keys,
1213 self::META_BC_BOOLS => $bools,
1214 self::META_INDEXED_TAG_NAME =>
'var',
1218 self::META_TYPE =>
'array',
1219 self::META_BC_BOOLS => $bools,
1220 self::META_INDEXED_TAG_NAME =>
'value',
1236 ->getReplicaDatabase()
1239 if ( $expiry ===
'' || $expiry ===
null || $expiry ===
false ||
1262class_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.