dry interval.php

This commit is contained in:
Paul Sealock 2019-01-29 10:23:44 +13:00
parent 3d0890a704
commit 2f022ffdba
4 changed files with 15 additions and 50 deletions

View File

@ -52,8 +52,6 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$args['country'] = $request['country'];
$args['last_active_before'] = $request['last_active_before'];
$args['last_active_after'] = $request['last_active_after'];
$args['last_active_min'] = $request['last_active_min'];
$args['last_active_max'] = $request['last_active_max'];
$args['orders_count_min'] = $request['orders_count_min'];
$args['orders_count_max'] = $request['orders_count_max'];
$args['total_spend_min'] = $request['total_spend_min'];
@ -64,10 +62,10 @@ class WC_Admin_REST_Reports_Customers_Controller extends WC_REST_Reports_Control
$args['last_order_after'] = $request['last_order_after'];
$numeric_between_params = array( 'orders_count', 'total_spend', 'avg_order_value' );
$date_between_params = array( 'last_active' );
$normalized = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $numeric_between_params );
$normalized_dates = WC_Admin_Reports_Interval::normalize_date_between_params( $request, $date_between_params );
$args = array_merge( $args, $normalized, $normalized_dates );
$date_between_params = array( 'last_active' );
$normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $numeric_between_params, false );
$normalized_dates = WC_Admin_Reports_Interval::normalize_between_params( $request, $date_between_params, true );
$args = array_merge( $args, $normalized, $normalized_dates );
return $args;
}

View File

@ -57,7 +57,7 @@ class WC_Admin_REST_Reports_Customers_Stats_Controller extends WC_REST_Reports_C
$args['last_order_after'] = $request['last_order_after'];
$between_params = array( 'orders_count', 'total_spend', 'avg_order_value' );
$normalized = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $between_params );
$normalized = WC_Admin_Reports_Interval::normalize_between_params( $request, $between_params );
$args = array_merge( $args, $normalized );
return $args;

View File

@ -498,9 +498,10 @@ class WC_Admin_Reports_Interval {
*
* @param array $request Query params from REST API request.
* @param string|array $param_names One or more param names to handle. Should not include "_between" suffix.
* @param bool $is_date Boolean if the param is date is related.
* @return array Normalized query values.
*/
public static function normalize_numeric_between_params( $request, $param_names ) {
public static function normalize_between_params( $request, $param_names, $is_date ) {
if ( ! is_array( $param_names ) ) {
$param_names = array( $param_names );
}
@ -518,49 +519,15 @@ class WC_Admin_Reports_Interval {
continue;
}
if ( $range[0] < $range[1] ) {
$normalized[ $param_name . '_min' ] = $range[0];
$normalized[ $param_name . '_max' ] = $range[1];
} else {
$normalized[ $param_name . '_min' ] = $range[1];
$normalized[ $param_name . '_max' ] = $range[0];
}
}
return $normalized;
}
/**
* Normalize "*_between" parameters to "*_after" and "*_before".
*
* @param array $request Query params from REST API request.
* @param string|array $param_names One or more param names to handle. Should not include "_between" suffix.
* @return array Normalized query values.
*/
public static function normalize_date_between_params( $request, $param_names ) {
if ( ! is_array( $param_names ) ) {
$param_names = array( $param_names );
}
$normalized = array();
foreach ( $param_names as $param_name ) {
if ( ! is_array( $request[ $param_name . '_between' ] ) ) {
continue;
}
$range = $request[ $param_name . '_between' ];
if ( 2 !== count( $range ) ) {
continue;
}
$min = $is_date ? '_after' : '_min';
$max = $is_date ? '_before' : '_max';
if ( $range[0] < $range[1] ) {
$normalized[ $param_name . '_after' ] = $range[0];
$normalized[ $param_name . '_before' ] = $range[1];
$normalized[ $param_name . $min ] = $range[0];
$normalized[ $param_name . $max ] = $range[1];
} else {
$normalized[ $param_name . '_after' ] = $range[1];
$normalized[ $param_name . '_before' ] = $range[0];
$normalized[ $param_name . $min ] = $range[1];
$normalized[ $param_name . $max ] = $range[0];
}
}

View File

@ -813,7 +813,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case {
/**
* Test function that normalizes *_between query parameters to *_min & *_max.
*/
public function test_normalize_numeric_between_params() {
public function test_normalize_between_params() {
$request = array(
'a_between' => 'malformed', // won't be normalized (not an array).
'b_between' => array( 1, 5 ), // results in min=1, max=5.
@ -822,7 +822,7 @@ class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case {
'f_between' => array( 10, 12 ), // not in params, skipped.
);
$params = array( 'a', 'b', 'c', 'd' );
$result = WC_Admin_Reports_Interval::normalize_numeric_between_params( $request, $params );
$result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params );
$expected = array(
'b_min' => 1,
'b_max' => 5,