46 lines
1.3 KiB
PHP
46 lines
1.3 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Actions;
|
||
|
|
|
||
|
|
/** Pure: which actions does a donation of $baseMinor trigger, and how many times each. */
|
||
|
|
final class Matcher {
|
||
|
|
|
||
|
|
public const EXACT = 'exact';
|
||
|
|
public const EVERY = 'every';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array[] $actions Rows with keys id, title, amount_minor, mode. Malformed rows are skipped.
|
||
|
|
* @return array<int,array{action_id:int,title:string,times:int}> In the order of $actions.
|
||
|
|
*/
|
||
|
|
public static function match( int $baseMinor, array $actions ): array {
|
||
|
|
$out = array();
|
||
|
|
if ( $baseMinor <= 0 ) {
|
||
|
|
return $out;
|
||
|
|
}
|
||
|
|
foreach ( $actions as $a ) {
|
||
|
|
if ( ! is_array( $a ) || ! isset( $a['id'], $a['amount_minor'], $a['mode'], $a['title'] ) ) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if ( ! is_numeric( $a['id'] ) || ! is_numeric( $a['amount_minor'] ) || ! is_string( $a['mode'] ) || ! is_scalar( $a['title'] ) ) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$id = (int) $a['id'];
|
||
|
|
$amount = (int) $a['amount_minor'];
|
||
|
|
if ( $id <= 0 || $amount <= 0 ) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if ( self::EXACT === $a['mode'] ) {
|
||
|
|
$times = $baseMinor === $amount ? 1 : 0;
|
||
|
|
} elseif ( self::EVERY === $a['mode'] ) {
|
||
|
|
$times = intdiv( $baseMinor, $amount );
|
||
|
|
} else {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if ( $times >= 1 ) {
|
||
|
|
$out[] = array( 'action_id' => $id, 'title' => (string) $a['title'], 'times' => $times );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $out;
|
||
|
|
}
|
||
|
|
}
|