/** * @param RateLimit[] $annotations */ protected function findBestMethodMatch(Request $request, array $annotations) { // Empty array, check the path limits if (count($annotations) == 0) { return $this->pathLimitProcessor->getRateLimit($request); } $best_match = null; foreach ($annotations as $annotation) { // cast methods to array, even method holds a string $methods = is_array($annotation->getMethods()) ? $annotation->getMethods() : array($annotation->getMethods()); if (in_array($request->getMethod(), $methods)) { $best_match = $annotation; } // Only match "default" annotation when we don't have a best match if (count($annotation->getMethods()) == 0 && $best_match == null) { $best_match = $annotation; } } return $best_match; }
/** @test */ function itMatchesTheMostSpecificPathFirst() { $plp = new PathLimitProcessor(array('api' => array('path' => 'api', 'methods' => array('GET'), 'limit' => 5, 'period' => 1), 'api_emails' => array('path' => 'api/users/emails', 'methods' => array('GET'), 'limit' => 100, 'period' => 60))); $result = $plp->getRateLimit(Request::create('/api/users/emails', 'GET')); $this->assertEquals(100, $result->getLimit()); $this->assertEquals(60, $result->getPeriod()); $this->assertEquals(array('GET'), $result->getMethods()); }