<?php
// Simple in-memory (per PHP process) rate limiter; for multi-server use Redis.
class RateLimiter {
    private static $buckets = [];
    public static function allow($key, $maxPerMinute = 20) {
        $now = time();
        $window = intdiv($now,60); // minute window
        if (!isset(self::$buckets[$key]) || self::$buckets[$key]['window'] !== $window) {
            self::$buckets[$key] = ['window'=>$window,'count'=>0];
        }
        if (self::$buckets[$key]['count'] >= $maxPerMinute) {
            return false;
        }
        self::$buckets[$key]['count']++;
        return true;
    }
}
