0 ? 'INF' : '-INF'; } if ( is_string( $value ) ) { return self::cut( $value, $max_chars ); } if ( is_array( $value ) ) { return self::walk( $value, $depth, $max_chars, $max_items ); } if ( is_object( $value ) ) { return self::object_label( $value ); } if ( is_resource( $value ) || 'resource (closed)' === gettype( $value ) ) { return '[resource: ' . ( is_resource( $value ) ? get_resource_type( $value ) : 'closed' ) . ']'; } return '[' . gettype( $value ) . ']'; } /** * Walk an array. * * @param array $value Array. * @param int $depth Levels left, including this one. * @param int $max_chars Longest string kept whole. * @param int $max_items Most items kept (0: all). * @return array|string */ private static function walk( array $value, int $depth, int $max_chars, int $max_items ) { if ( $depth < 1 ) { return '[array: ' . count( $value ) . ' items]'; } $out = array(); $kept = 0; foreach ( $value as $key => $item ) { if ( $max_items && $kept >= $max_items ) { break; } ++$kept; $key = is_string( $key ) ? self::cut( $key, $max_chars ) : $key; if ( is_array( $item ) ) { $out[ $key ] = self::walk( $item, $depth - 1, $max_chars, $max_items ); } else { $out[ $key ] = self::to_array( $item, 1, $max_chars, $max_items ); } } $more = count( $value ) - $kept; if ( $more > 0 ) { $note = '[+' . $more . ' more]'; if ( array_is_list( $out ) ) { $out[] = $note; } else { $out['…'] = $note; } } return $out; } /** * "[object ClassName]", read from the class name only (never from the object). * * @param object $value Object. * @return string */ private static function object_label( $value ): string { if ( $value instanceof \__PHP_Incomplete_Class ) { return '[object (incomplete)]'; } return '[object ' . get_class( $value ) . ']'; } /** * Cut a long string. * * @param string $text Text. * @param int $max_chars Longest kept whole. * @return string */ private static function cut( string $text, int $max_chars ): string { if ( ! mb_check_encoding( $text, 'UTF-8' ) ) { $text = mb_scrub( $text, 'UTF-8' ); // Invalid bytes become the substitute character. if ( ! mb_check_encoding( $text, 'UTF-8' ) ) { $text = (string) wp_check_invalid_utf8( $text, true ); } } $length = mb_strlen( $text, 'UTF-8' ); if ( $length <= $max_chars ) { return $text; } return mb_substr( $text, 0, $max_chars, 'UTF-8' ) . '…[truncated: ' . $length . ' chars]'; } }