Spot\Query::addMethod PHP Method

addMethod() public static method

Add a custom user method via closure or PHP callback
public static addMethod ( string $method, callable $callback )
$method string Method name to add
$callback callable Callback or closure that will be executed when missing method call matching $method is made
    public static function addMethod($method, callable $callback)
    {
        if (method_exists(__CLASS__, $method)) {
            throw new \InvalidArgumentException("Method '" . $method . "' already exists on " . __CLASS__);
        }
        self::$_customMethods[$method] = $callback;
    }

Usage Example

示例#1
0
 public function testQueryPagerExtension()
 {
     $mapper = test_spot_mapper();
     \Spot\Query::addMethod('page', function (\Spot\Query $query, $limit, $perPage = 20) {
         return $query->limit($limit, $perPage);
     });
     $posts = $mapper->all('\\Spot\\Entity\\Post')->page(1, 1);
     // Do this instead of $posts->count() because it drops LIMIT clause to count the full dataset
     $postCount = count($posts->toArray());
     $this->assertEquals(1, $postCount);
 }