Kahlan\Filter\Filter::filters PHP Method

filters() public static method

Or sets the whole filters data.
public static filters ( mixed $context = null, string | null $method = null ) : array
$context mixed If `null` returns the whole filters data. If `$context` is an array use `$context` as the whole filters data. Otherwise `$context` stands for the class/instance context.
$method string | null The name of the method to get the filters from or `null` to get all of them.
return array The whole filters data or filters associated to a class/instance's method.
    public static function filters($context = null, $method = null)
    {
        if (!func_num_args()) {
            return static::$_methodFilters;
        }
        if (is_array($context)) {
            return static::$_methodFilters = $context;
        }
        $result = [];
        if (is_object($context)) {
            $result = $context->methodFilters()->filters($method);
            $context = get_class($context);
        }
        return array_merge($result, static::_classFilters($context, $method));
    }

Usage Example

コード例 #1
0
ファイル: FilterSpec.php プロジェクト: Ilyes512/kahlan
             $closure = function () use($class) {
                 Filter::apply($class, 'filterable', 'spec.unexisting_closure');
             };
             expect($closure)->toThrow(new Exception('Undefined filter `spec.unexisting_closure`.'));
         });
     });
     describe("::filters()", function () {
         it("exports filters setted as a class level", function () {
             Filter::apply($this->class, 'filterable', 'spec.my_prefix');
             $filters = Filter::filters();
             expect($filters)->toHaveLength(1);
             expect(isset($filters[$this->class]))->toBe(true);
         });
         it("imports class based filters", function () {
             Filter::filters([$this->class => [Filter::registered('spec.my_prefix')]]);
             $filters = Filter::filters();
             expect($filters)->toHaveLength(1);
             expect(isset($filters[$this->class]))->toBe(true);
         });
     });
 });
 describe("::apply()", function () {
     it("throws an Exception when trying to apply a filter on an unfilterable context", function () {
         $closure = function () {
             Filter::apply(null, 'filterable', 'spec.my_prefix');
         };
         expect($closure)->toThrow(new Exception("Error this context can't be filtered."));
     });
 });
 describe("::registered()", function () {
     it("exports the `Filter` class data", function () {