179 lines
6.8 KiB
PHP
179 lines
6.8 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Tiers;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Pure rules for goal tiers (milestones). No WordPress / database access.
|
||
|
|
* A tier row is an array with keys: id, target_minor, title, description, reached_at (null|string), reached_donation_id.
|
||
|
|
*/
|
||
|
|
final class TierPolicy {
|
||
|
|
|
||
|
|
public const TITLE_MAX = 120;
|
||
|
|
public const DESC_MAX = 255;
|
||
|
|
|
||
|
|
/** Tiers ordered by target ascending (ties by id). Non-array entries are dropped. @return array[] */
|
||
|
|
public static function sorted( array $tiers ): array {
|
||
|
|
$tiers = array_values( array_filter( $tiers, 'is_array' ) );
|
||
|
|
usort(
|
||
|
|
$tiers,
|
||
|
|
static fn( array $a, array $b ): int => ( (int) $a['target_minor'] <=> (int) $b['target_minor'] ) ?: ( (int) ( $a['id'] ?? 0 ) <=> (int) ( $b['id'] ?? 0 ) )
|
||
|
|
);
|
||
|
|
return $tiers;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function isReached( array $tier ): bool {
|
||
|
|
return ! empty( $tier['reached_at'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** The first tier (by target) that is not reached yet, or null (no tiers, or all reached). */
|
||
|
|
public static function currentTier( array $tiers ): ?array {
|
||
|
|
foreach ( self::sorted( $tiers ) as $t ) {
|
||
|
|
if ( ! self::isReached( $t ) ) {
|
||
|
|
return $t;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** True when there is at least one tier and every tier is reached. */
|
||
|
|
public static function allReached( array $tiers ): bool {
|
||
|
|
return $tiers && null === self::currentTier( $tiers );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 1-based position of the current tier; when all are reached, the count; 0 without tiers. */
|
||
|
|
public static function currentIndex( array $tiers ): int {
|
||
|
|
$sorted = self::sorted( $tiers );
|
||
|
|
foreach ( $sorted as $i => $t ) {
|
||
|
|
if ( ! self::isReached( $t ) ) {
|
||
|
|
return $i + 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return count( $sorted );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Start of the current tier's range: the previous tier's target (0 for the first / when none is current). */
|
||
|
|
public static function floorMinor( array $tiers ): int {
|
||
|
|
$prev = 0;
|
||
|
|
foreach ( self::sorted( $tiers ) as $t ) {
|
||
|
|
if ( ! self::isReached( $t ) ) {
|
||
|
|
return $prev;
|
||
|
|
}
|
||
|
|
$prev = (int) $t['target_minor'];
|
||
|
|
}
|
||
|
|
return $prev; // all reached: the top tier's target
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Progress inside a tier, 0..100 (clamped). Below the floor = 0, at or above the target = 100. */
|
||
|
|
public static function percent( int $raised, int $floor, int $target ): int {
|
||
|
|
if ( $target <= $floor ) {
|
||
|
|
return $raised >= $target && $target > 0 ? 100 : 0;
|
||
|
|
}
|
||
|
|
$p = intdiv( ( $raised - $floor ) * 100, $target - $floor );
|
||
|
|
return max( 0, min( 100, $p ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Percent within the current tier for the given tiers and raised total (100 when all reached, 0 without tiers). */
|
||
|
|
public static function percentFor( array $tiers, int $raised ): int {
|
||
|
|
$cur = self::currentTier( $tiers );
|
||
|
|
if ( null === $cur ) {
|
||
|
|
return $tiers ? 100 : 0;
|
||
|
|
}
|
||
|
|
return self::percent( $raised, self::floorMinor( $tiers ), (int) $cur['target_minor'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Money still needed to reach the current tier (0 when none). */
|
||
|
|
public static function remainingMinor( array $tiers, int $raised ): int {
|
||
|
|
$cur = self::currentTier( $tiers );
|
||
|
|
return null === $cur ? 0 : max( 0, (int) $cur['target_minor'] - $raised );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Every unreached tier whose target is at or below the raised total, in target order. A big donation can unlock several. @return array[] */
|
||
|
|
public static function newlyReached( array $tiers, int $raised ): array {
|
||
|
|
$out = array();
|
||
|
|
foreach ( self::sorted( $tiers ) as $t ) {
|
||
|
|
if ( ! self::isReached( $t ) && (int) $t['target_minor'] <= $raised ) {
|
||
|
|
$out[] = $t;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $out;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** The goal's target when it has tiers: the highest tier target (0 when no tiers). */
|
||
|
|
public static function derivedGoalTarget( array $tiers ): int {
|
||
|
|
$max = 0;
|
||
|
|
foreach ( $tiers as $t ) {
|
||
|
|
$max = max( $max, (int) ( $t['target_minor'] ?? 0 ) );
|
||
|
|
}
|
||
|
|
return $max;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate a tier form submission.
|
||
|
|
*
|
||
|
|
* @param array $tiers All tiers of the goal (including the one being edited).
|
||
|
|
* @param int|null $editId Tier id when editing, null when adding.
|
||
|
|
* @param int|null $target Parsed target in minor units (null = not a valid amount).
|
||
|
|
* @param int $raised Goal raised total (minor units).
|
||
|
|
* @param int $minMinor Settings::effectiveMin().
|
||
|
|
* @return array{data:?array{target_minor:int,title:string,description:string},error:?string}
|
||
|
|
*/
|
||
|
|
public static function validate( array $tiers, ?int $editId, ?int $target, string $title, string $description, int $raised, int $minMinor, string $minLabel = '' ): array {
|
||
|
|
$fail = static fn( string $m ): array => array( 'data' => null, 'error' => $m );
|
||
|
|
$title = trim( $title );
|
||
|
|
$desc = trim( $description );
|
||
|
|
if ( '' === $title ) {
|
||
|
|
return $fail( 'Please name what unlocks at this tier, for example "New microphone".' );
|
||
|
|
}
|
||
|
|
if ( self::len( $title ) > self::TITLE_MAX ) {
|
||
|
|
return $fail( 'The title can be at most ' . self::TITLE_MAX . ' characters.' );
|
||
|
|
}
|
||
|
|
if ( self::len( $desc ) > self::DESC_MAX ) {
|
||
|
|
return $fail( 'The description can be at most ' . self::DESC_MAX . ' characters.' );
|
||
|
|
}
|
||
|
|
$existing = null;
|
||
|
|
if ( null !== $editId ) {
|
||
|
|
foreach ( $tiers as $t ) {
|
||
|
|
if ( (int) ( $t['id'] ?? 0 ) === $editId ) {
|
||
|
|
$existing = $t;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if ( null === $existing ) {
|
||
|
|
return $fail( 'That tier no longer exists.' );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if ( null !== $existing && self::isReached( $existing ) ) {
|
||
|
|
// Reached tiers: the target is locked, only the text can change.
|
||
|
|
return array( 'data' => array( 'target_minor' => (int) $existing['target_minor'], 'title' => $title, 'description' => $desc ), 'error' => null );
|
||
|
|
}
|
||
|
|
if ( null === $target || $target <= 0 ) {
|
||
|
|
return $fail( 'Please enter a target greater than zero, for example 250.00.' );
|
||
|
|
}
|
||
|
|
if ( $target < $minMinor ) {
|
||
|
|
return $fail( 'The target must be at least the minimum donation' . ( '' !== $minLabel ? ' (' . $minLabel . ')' : '' ) . '.' );
|
||
|
|
}
|
||
|
|
foreach ( $tiers as $t ) {
|
||
|
|
if ( (int) ( $t['id'] ?? 0 ) !== (int) $editId && (int) $t['target_minor'] === $target ) {
|
||
|
|
return $fail( 'Another tier already has that target. Each tier needs its own amount.' );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$unchanged = null !== $existing && (int) $existing['target_minor'] === $target;
|
||
|
|
if ( ! $unchanged && $target <= $raised ) {
|
||
|
|
return $fail( 'This goal has already raised more than that, so the tier would unlock instantly. Choose a target above the current total.' );
|
||
|
|
}
|
||
|
|
return array( 'data' => array( 'target_minor' => $target, 'title' => $title, 'description' => $desc ), 'error' => null );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Display status of a tier given the sorted list: 'reached' | 'current' | 'upcoming'. */
|
||
|
|
public static function status( array $tiers, array $tier ): string {
|
||
|
|
if ( self::isReached( $tier ) ) {
|
||
|
|
return 'reached';
|
||
|
|
}
|
||
|
|
$cur = self::currentTier( $tiers );
|
||
|
|
return $cur && (int) $cur['id'] === (int) $tier['id'] ? 'current' : 'upcoming';
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function len( string $s ): int {
|
||
|
|
return function_exists( 'mb_strlen' ) ? mb_strlen( $s ) : strlen( $s );
|
||
|
|
}
|
||
|
|
}
|