34 lines
1.3 KiB
PHP
34 lines
1.3 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Core;
|
||
|
|
|
||
|
|
/** Pure rules for "end at target" goals (no WordPress / database access). */
|
||
|
|
final class GoalTargetPolicy {
|
||
|
|
|
||
|
|
/** Donation column summed towards the goal: total (fees included) or base. Whitelisted values only. */
|
||
|
|
public static function amountColumn( array $goal ): string {
|
||
|
|
return ! empty( $goal['counts_fees'] ) ? 'total_minor' : 'base_minor';
|
||
|
|
}
|
||
|
|
|
||
|
|
/** True when the goal should now be auto-closed: flag on, currently active, target set and reached or exceeded. */
|
||
|
|
public static function shouldClose( array $goal, int $raisedMinor ): bool {
|
||
|
|
return ! empty( $goal['close_at_target'] )
|
||
|
|
&& 'active' === ( $goal['status'] ?? '' )
|
||
|
|
&& (int) ( $goal['target_minor'] ?? 0 ) > 0
|
||
|
|
&& $raisedMinor >= (int) $goal['target_minor'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/** True for a goal that is closed to donors because it reached its target (flag on + complete). */
|
||
|
|
public static function isClosedAtTarget( array $goal ): bool {
|
||
|
|
return ! empty( $goal['close_at_target'] ) && 'complete' === ( $goal['status'] ?? '' );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Checkbox value from a raw form field: 1 when ticked, else 0. */
|
||
|
|
public static function flagFromPost( mixed $raw ): int {
|
||
|
|
if ( is_array( $raw ) || null === $raw ) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
return in_array( strtolower( trim( (string) $raw ) ), array( '1', 'on', 'true', 'yes' ), true ) ? 1 : 0;
|
||
|
|
}
|
||
|
|
}
|