Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
rename validation file
Browse files Browse the repository at this point in the history
  • Loading branch information
senadir committed Feb 16, 2023
1 parent 8083368 commit a9bd740
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/StoreApi/Routes/V1/CartUpdateCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Automattic\WooCommerce\StoreApi\Utilities\DraftOrderTrait;
use Automattic\WooCommerce\StoreApi\Utilities\CartController;
use Automattic\WooCommerce\StoreApi\Utilities\ValidationUtils;

/**
* CartUpdateCustomer class.
Expand Down Expand Up @@ -216,11 +217,11 @@ protected function get_route_post_response( \WP_REST_Request $request ) {
* @return array
*/
protected function get_customer_billing_address( \WC_Customer $customer ) {
$controller = new CustomerController();
$validation_util = new ValidationUtils();
$billing_country = $customer->get_billing_country();
$billing_state = $customer->get_billing_state();

if ( ! $controller->validate_state( $billing_state, $billing_country ) ) {
if ( ! $validation_util->validate_state( $billing_state, $billing_country ) ) {
$billing_state = '';
}
return [
Expand Down
14 changes: 7 additions & 7 deletions src/StoreApi/Schemas/V1/AbstractAddressSchema.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Schemas\V1;

use Automattic\WooCommerce\StoreApi\Utilities\CustomerController;
use Automattic\WooCommerce\StoreApi\Utilities\ValidationUtils;

/**
* AddressSchema class.
Expand Down Expand Up @@ -89,7 +89,7 @@ public function get_properties() {
* @return array
*/
public function sanitize_callback( $address, $request, $param ) {
$controller = new CustomerController();
$validation_util = new ValidationUtils();

$address = array_merge( array_fill_keys( array_keys( $this->get_properties() ), '' ), (array) $address );
$address['country'] = wc_strtoupper( wc_clean( wp_unslash( $address['country'] ) ) );
Expand All @@ -99,7 +99,7 @@ public function sanitize_callback( $address, $request, $param ) {
$address['address_1'] = wc_clean( wp_unslash( $address['address_1'] ) );
$address['address_2'] = wc_clean( wp_unslash( $address['address_2'] ) );
$address['city'] = wc_clean( wp_unslash( $address['city'] ) );
$address['state'] = $controller->format_state( wc_clean( wp_unslash( $address['state'] ) ), $address['country'] );
$address['state'] = $validation_util->format_state( wc_clean( wp_unslash( $address['state'] ) ), $address['country'] );
$address['postcode'] = $address['postcode'] ? wc_format_postcode( wc_clean( wp_unslash( $address['postcode'] ) ), $address['country'] ) : '';
$address['phone'] = wc_clean( wp_unslash( $address['phone'] ) );
return $address;
Expand All @@ -116,9 +116,9 @@ public function sanitize_callback( $address, $request, $param ) {
* @return true|\WP_Error
*/
public function validate_callback( $address, $request, $param ) {
$errors = new \WP_Error();
$address = $this->sanitize_callback( $address, $request, $param );
$controller = new CustomerController();
$errors = new \WP_Error();
$address = $this->sanitize_callback( $address, $request, $param );
$validation_util = new ValidationUtils();

if ( ! empty( $address['country'] ) && ! in_array( $address['country'], array_keys( wc()->countries->get_countries() ), true ) ) {
$errors->add(
Expand All @@ -132,7 +132,7 @@ public function validate_callback( $address, $request, $param ) {
return $errors;
}

if ( ! empty( $address['state'] ) && ! $controller->validate_state( $address['state'], $address['country'] ) ) {
if ( ! empty( $address['state'] ) && ! $validation_util->validate_state( $address['state'], $address['country'] ) ) {
$errors->add(
'invalid_state',
sprintf(
Expand Down
6 changes: 3 additions & 3 deletions src/StoreApi/Schemas/V1/BillingAddressSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Automattic\WooCommerce\StoreApi\Schemas\V1;

use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
use Automattic\WooCommerce\StoreApi\Utilities\CustomerController;
use Automattic\WooCommerce\StoreApi\Utilities\ValidationUtils;

/**
* BillingAddressSchema class.
Expand Down Expand Up @@ -90,13 +90,13 @@ public function validate_callback( $address, $request, $param ) {
* @return stdClass
*/
public function get_item_response( $address ) {
$controller = new CustomerController();
$validation_util = new ValidationUtils();

if ( ( $address instanceof \WC_Customer || $address instanceof \WC_Order ) ) {
$billing_country = $address->get_billing_country();
$billing_state = $address->get_billing_state();

if ( ! $controller->validate_state( $billing_state, $billing_country ) ) {
if ( ! $validation_util->validate_state( $billing_state, $billing_country ) ) {
$billing_state = '';
}

Expand Down
6 changes: 3 additions & 3 deletions src/StoreApi/Schemas/V1/ShippingAddressSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Automattic\WooCommerce\StoreApi\Schemas\V1;

use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
use Automattic\WooCommerce\StoreApi\Utilities\CustomerController;
use Automattic\WooCommerce\StoreApi\Utilities\ValidationUtils;

/**
* ShippingAddressSchema class.
Expand Down Expand Up @@ -33,12 +33,12 @@ class ShippingAddressSchema extends AbstractAddressSchema {
* @return stdClass
*/
public function get_item_response( $address ) {
$controller = new CustomerController();
$validation_util = new ValidationUtils();
if ( ( $address instanceof \WC_Customer || $address instanceof \WC_Order ) ) {
$shipping_country = $address->get_shipping_country();
$shipping_state = $address->get_shipping_state();

if ( ! $controller->validate_state( $shipping_state, $shipping_country ) ) {
if ( ! $validation_util->validate_state( $shipping_state, $shipping_country ) ) {
$shipping_state = '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;

/**
* CustomerController class.
* ValidationUtils class.
* Helper class which validates and update customer info.
*/
class CustomerController {
class ValidationUtils {
/**
* Get list of states for a country.
*
Expand Down

0 comments on commit a9bd740

Please sign in to comment.