> $rules The preset. * @param list> $rows Ledger rows, any order. * @return array{examined: int, held: int, denied: int, by_rule: array} */ public static function run( array $rules, array $rows ): array { $rows = array_values( array_filter( array_map( array( self::class, 'normalized' ), $rows ), static fn( array $row ): bool => (int) ( $row['agent_id'] ?? 0 ) > 0 ) ); // In time, then ledger order: rows in the same second sum the same way every run. usort( $rows, static fn( array $a, array $b ): int => array( (string) ( $a['ts'] ?? '' ), (int) ( $a['id'] ?? 0 ) ) <=> array( (string) ( $b['ts'] ?? '' ), (int) ( $b['id'] ?? 0 ) ) ); $result = array( 'examined' => count( $rows ), 'held' => 0, 'denied' => 0, 'by_rule' => array(), ); $earlier = array(); foreach ( $rows as $row ) { $agent = (int) $row['agent_id']; $index = self::first_firing( $rules, $row, $earlier[ $agent ] ?? array() ); $earlier[ $agent ][] = $row; if ( null === $index ) { continue; } $result['by_rule'][ $index ] = ( $result['by_rule'][ $index ] ?? 0 ) + 1; $decision = (string) ( $rules[ $index ]['decision'] ?? '' ); if ( in_array( $decision, self::HOLDS, true ) ) { ++$result['held']; } elseif ( 'DENY' === $decision ) { ++$result['denied']; } } ksort( $result['by_rule'] ); return $result; } /** * The index of the first rule that fires for a row, or null. * * @param list> $rules The preset. * @param array $row A ledger row. * @param list> $earlier The same agent's earlier rows. */ public static function first_firing( array $rules, array $row, array $earlier ): ?int { $row = self::normalized( $row ); $facts = $row['facts']; foreach ( $rules as $index => $rule ) { if ( ! self::matches( (array) ( $rule['match'] ?? array() ), $row ) ) { continue; } $restrictive = in_array( (string) ( $rule['decision'] ?? '' ), array_merge( self::HOLDS, array( 'DENY' ) ), true ); $holds = self::conditions( (array) ( $rule['when'] ?? array() ), $facts ); if ( false === $holds || ( null === $holds && ! $restrictive ) ) { continue; } if ( isset( $rule['budget'] ) && ! self::over_budget( (array) $rule['budget'], $row, $earlier ) ) { continue; } return $index; } return null; } /** * A row with its facts and input as arrays: the REST API gives an * empty object for no facts. * * @param array $row A ledger row. * @return array */ private static function normalized( array $row ): array { foreach ( array( 'facts', 'input' ) as $key ) { $value = $row[ $key ] ?? array(); $row[ $key ] = is_object( $value ) ? get_object_vars( $value ) : ( is_array( $value ) ? $value : array() ); } return $row; } /** * Whether a rule's match covers the row. A key the dry run does not * know never matches. * * @param array $match The rule's match. * @param array $row A ledger row. */ private static function matches( array $match, array $row ): bool { $type = (string) ( $row['entity_type'] ?? '' ); $cat = (string) ( $row['action_cat'] ?? '' ); foreach ( $match as $key => $wanted ) { $actual = match ( $key ) { 'entity_type' => $type, 'action_cat' => $cat, 'destructive' => WcRouteClassifier::destructive_write( $type, $cat, $row['input'] ), default => new \stdClass(), }; if ( $actual !== $wanted ) { return false; } } return true; } /** * Conditions AND-ed in three values: false settles it, else unknown. * * @param array $when Conditions. * @param array $facts The row's facts. */ private static function conditions( array $when, array $facts ): ?bool { $all = true; foreach ( $when as $condition ) { $holds = self::condition( (array) $condition, $facts ); if ( false === $holds ) { return false; } $all = $all && true === $holds; } return $all ? true : null; } /** * One condition: true, false, or null when it cannot be evaluated. * * @param array $condition The condition. * @param array $facts The row's facts. */ private static function condition( array $condition, array $facts ): ?bool { $name = (string) ( $condition['fact'] ?? '' ); $op = (string) ( $condition['op'] ?? '' ); if ( 'exists' === $op ) { return array_key_exists( $name, $facts ); } if ( ! array_key_exists( $name, $facts ) ) { return null; } $fact = $facts[ $name ]; $value = $condition['value'] ?? null; return match ( $op ) { 'gt', 'gte', 'lt', 'lte' => self::compare( $op, $fact, $value ), 'eq' => self::same_kind( $fact, $value ) ? $fact == $value : null, // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- 5 and 5.0 are equal amounts. 'ne' => self::same_kind( $fact, $value ) ? $fact != $value : null, // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- As eq. 'in', 'not_in' => self::in_list( $op, $fact, $value ), default => null, }; } /** * An ordering, on numbers only. * * @param string $op gt, gte, lt or lte. * @param mixed $fact The fact. * @param mixed $value The operand. */ private static function compare( string $op, mixed $fact, mixed $value ): ?bool { if ( ! self::numeric( $fact ) || ! self::numeric( $value ) ) { return null; } return match ( $op ) { 'gt' => $fact > $value, 'gte' => $fact >= $value, 'lt' => $fact < $value, default => $fact <= $value, }; } /** * Membership, of a scalar in a list of its kind. * * @param string $op in or not_in. * @param mixed $fact The fact. * @param mixed $value The list. */ private static function in_list( string $op, mixed $fact, mixed $value ): ?bool { if ( ! is_array( $value ) || ! is_scalar( $fact ) ) { return null; } $found = false; foreach ( $value as $item ) { $found = $found || ( self::same_kind( $fact, $item ) && $fact == $item ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- As eq. } return 'in' === $op ? $found : ! $found; } /** * Whether two values are both numbers, both strings or both booleans. * * @param mixed $a One. * @param mixed $b The other. */ private static function same_kind( mixed $a, mixed $b ): bool { return ( self::numeric( $a ) && self::numeric( $b ) ) || ( is_string( $a ) && is_string( $b ) ) || ( is_bool( $a ) && is_bool( $b ) ); } /** * A number, not a boolean or numeric text. * * @param mixed $value Value. */ private static function numeric( mixed $value ): bool { return is_int( $value ) || is_float( $value ); } /** * Whether the row takes its agent over a budget: the fact summed over * the agent's spending rows in the window, plus this one. A value that * cannot be known is over. * * @param array $budget The rule's budget. * @param array $row A ledger row. * @param list> $earlier The same agent's earlier rows. */ private static function over_budget( array $budget, array $row, array $earlier ): bool { $fact = (string) ( $budget['fact'] ?? '' ); $value = $row['facts'][ $fact ] ?? null; if ( ! self::numeric( $value ) || $value < 0 ) { return true; } $now = strtotime( (string) ( $row['ts'] ?? '' ) . ' UTC' ); $since = false === $now ? PHP_INT_MIN : $now - (int) ( $budget['window'] ?? 0 ); $total = $value; foreach ( $earlier as $spent ) { $at = strtotime( (string) ( $spent['ts'] ?? '' ) . ' UTC' ); $amount = self::normalized( $spent )['facts'][ $fact ] ?? null; if ( false !== $at && $at > $since && in_array( $spent['status'] ?? '', self::SPENT, true ) && self::numeric( $amount ) && $amount > 0 ) { $total += $amount; } } return $total > ( $budget['max'] ?? 0 ); } }