1000, 'customers' => 5000, 'orders' => 10000, 'coupons' => 200, 'reviews' => 5000, ); /** * What the form shows first. */ const DEFAULTS = array( 'products' => 20, 'customers' => 50, 'orders' => 100, 'coupons' => 5, 'reviews' => 40, ); /** * Other inputs: [ min, max, default ]. */ const OPTIONS = array( 'variable_share' => array( 0, 100, 40 ), 'guest_share' => array( 0, 100, 30 ), 'days' => array( 1, 730, 90 ), ); /** * Longest label. */ const LABEL_MAX = 60; /** * Per-run maxima: a filtered list can only lower the ceilings (never raise them, never below 0). * * @since 1.0.0 * @param mixed $filtered Output of the `apwc_test_data_limits` filter. * @return array */ public static function maxima( $filtered ): array { $filtered = is_array( $filtered ) ? $filtered : array(); $out = array(); foreach ( self::CEILINGS as $type => $ceiling ) { $value = $filtered[ $type ] ?? $ceiling; $value = self::whole( $value, $ceiling ); $out[ $type ] = max( 0, min( $ceiling, $value ) ); } return $out; } /** * Clamp requested counts to 0 … max. Missing, negative, non-numeric and non-finite values * become 0; fractions are cut. * * @since 1.0.0 * @param array $input Requested counts. * @param array $max Maxima per type (keys decide what comes back). * @return array */ public static function clamp( array $input, array $max ): array { $out = array(); foreach ( $max as $type => $limit ) { $value = self::whole( $input[ $type ] ?? 0, 0 ); $out[ $type ] = max( 0, min( (int) $limit, $value ) ); } return $out; } /** * Clamp one of the other inputs (shares, days) to its range; unusable values get the default. * * @since 1.0.0 * @param string $key Key of OPTIONS. * @param mixed $value Value. * @return int */ public static function option( string $key, $value ): int { if ( ! isset( self::OPTIONS[ $key ] ) ) { return 0; } list( $min, $max, $default ) = self::OPTIONS[ $key ]; if ( null === $value || '' === $value ) { return $default; } return max( $min, min( $max, self::whole( $value, $default ) ) ); } /** * A whole number from user input, or the fallback. * * @param mixed $value Value. * @param int $fallback Fallback. * @return int */ private static function whole( $value, int $fallback ): int { if ( is_int( $value ) ) { return $value; } if ( is_bool( $value ) || ! is_numeric( $value ) ) { return $fallback; } $number = (float) $value; if ( ! is_finite( $number ) ) { return $fallback; } if ( $number >= PHP_INT_MAX ) { return PHP_INT_MAX; } if ( $number <= PHP_INT_MIN ) { return PHP_INT_MIN; } return (int) $number; } }