| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\CartRules; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\NonLoggedInUsers\Roles; |
| 4 |
|
| 5 |
/** |
| 6 |
* Cart-level rules per role. Pure: no WordPress calls, so it is unit-tested on its own. |
| 7 |
* |
| 8 |
* Minimums are [ role => [ 'amount' => float, 'quantity' => int ] ]; a customer with several roles |
| 9 |
* gets the strictest minimum. Method allow-lists are role arrays; an empty list allows everyone. |
| 10 |
*/ |
| 11 |
class CartRules { |
| 12 |
|
| 13 |
/** |
| 14 |
* @param mixed $raw JSON string or array, as stored or posted |
| 15 |
* |
| 16 |
* @return array<string, array{amount: float, quantity: int}> rows with at least one minimum |
| 17 |
*/ |
| 18 |
public static function normalizeMinimums( $raw ): array { |
| 19 |
if ( is_string( $raw ) ) { |
| 20 |
$raw = json_decode( $raw, true ); |
| 21 |
} |
| 22 |
|
| 23 |
$minimums = array(); |
| 24 |
|
| 25 |
foreach ( is_array( $raw ) ? $raw : array() as $role => $row ) { |
| 26 |
$role = strtolower( trim( (string) preg_replace( '/[^A-Za-z0-9_\-]/', '', (string) $role ) ) ); |
| 27 |
|
| 28 |
if ( '' === $role || ! is_array( $row ) ) { |
| 29 |
continue; |
| 30 |
} |
| 31 |
|
| 32 |
$amount = max( 0.0, (float) str_replace( ',', '.', (string) ( $row['amount'] ?? '' ) ) ); |
| 33 |
$quantity = max( 0, (int) ( $row['quantity'] ?? 0 ) ); |
| 34 |
|
| 35 |
if ( $amount > 0 || $quantity > 0 ) { |
| 36 |
$minimums[ $role ] = array( 'amount' => $amount, 'quantity' => $quantity ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return $minimums; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* The minimums that apply to a visitor: the strictest among their roles. |
| 45 |
* |
| 46 |
* @return array{amount: float, quantity: int} |
| 47 |
*/ |
| 48 |
public static function pickMinimums( array $minimums, array $roles, bool $isGuest ): array { |
| 49 |
$picked = array( 'amount' => 0.0, 'quantity' => 0 ); |
| 50 |
|
| 51 |
foreach ( $isGuest ? array( Roles::GUEST ) : $roles as $role ) { |
| 52 |
if ( ! isset( $minimums[ $role ] ) ) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
$picked['amount'] = max( $picked['amount'], (float) ( $minimums[ $role ]['amount'] ?? 0 ) ); |
| 57 |
$picked['quantity'] = max( $picked['quantity'], (int) ( $minimums[ $role ]['quantity'] ?? 0 ) ); |
| 58 |
} |
| 59 |
|
| 60 |
return $picked; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Which minimums the cart misses. |
| 65 |
* |
| 66 |
* @return array{amount?: float, quantity?: int} the missed minimum(s) |
| 67 |
*/ |
| 68 |
public static function missedMinimums( array $picked, float $subtotal, int $count ): array { |
| 69 |
$missed = array(); |
| 70 |
|
| 71 |
if ( $picked['amount'] > 0 && $subtotal + 0.00001 < $picked['amount'] ) { |
| 72 |
$missed['amount'] = (float) $picked['amount']; |
| 73 |
} |
| 74 |
|
| 75 |
if ( $picked['quantity'] > 0 && $count < $picked['quantity'] ) { |
| 76 |
$missed['quantity'] = (int) $picked['quantity']; |
| 77 |
} |
| 78 |
|
| 79 |
return $missed; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Whether a visitor may use a method: an empty allow-list allows everyone. |
| 84 |
*/ |
| 85 |
public static function isAllowed( array $allowedRoles, array $roles, bool $isGuest ): bool { |
| 86 |
$allowedRoles = array_values( array_filter( array_map( 'strval', $allowedRoles ), 'strlen' ) ); |
| 87 |
|
| 88 |
if ( ! $allowedRoles ) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
return $isGuest ? in_array( Roles::GUEST, $allowedRoles, true ) : (bool) array_intersect( $roles, $allowedRoles ); |
| 93 |
} |
| 94 |
} |
| 95 |
|