Coseva\CSV::filter PHP Method

filter() public method

Allows you to register any number of filters on a particular column or an entire row.
public filter ( integer | callable $column, callable $callable = null ) : CSV
$column integer | callable Specific column number or the callable to be applied. Optional: Zero-based column number. If this parameter is preset the $callable will recieve the contents of the current column (as a string), and will receive the entire (array based) row otherwise.
$callable callable Either the current row (as an array) or the current column (as a string) as the first parameter. The callable must return the new filtered row or column. Note: You can also use any native PHP functions that permit one parameter and return the new value, like trim, htmlspecialchars, urlencode, etc.
return CSV $this
    public function filter($column, $callable = null)
    {
        // Get the function arguments.
        $args = func_get_args();
        $column = array_shift($args);
        // Check if we actually have a column or a callable.
        if (is_numeric($column)) {
            $callable = array_shift($args);
        } else {
            $callable = $column;
            $column = null;
        }
        // Check the function arguments.
        if (!is_callable($callable)) {
            throw new InvalidArgumentException('The $callable parameter must be callable.');
        }
        if (isset($column) && !is_numeric($column)) {
            throw new InvalidArgumentException('No proper column index provided. Expected a numeric, while given ' . var_export($column, true));
        }
        // Add the filter to our stack. Apply it to the whole row when our column
        // appears to be the callable, being the only present argument.
        $this->_filters[] = array('callable' => $callable, 'column' => isset($column) ? (int) $column : null, 'args' => $args);
        return $this;
    }