1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 
<?php

namespace SAREhub\Client\Processor;

use SAREhub\Client\Message\Exchange;

class SimpleFilterProcessor implements Processor
{

    /**
     * @var callable
     */
    private $predicate;

    /**
     * @var Processor
     */
    private $to;

    public function __construct(callable $predicate)
    {
        $this->predicate = $predicate;
    }

    public static function newWithPredicate(callable $predicate)
    {
        return new self($predicate);
    }

    public function to(Processor $to): SimpleFilterProcessor
    {
        $this->to = $to;
        return $this;
    }

    public function process(Exchange $exchange)
    {
        if (($this->predicate)($exchange)) {
            $this->getTo()->process($exchange);
        }
    }

    public function getPredicate(): callable
    {
        return $this->predicate;
    }


    public function getTo(): Processor
    {
        return $this->to;
    }

    public function __toString()
    {
        $predicateInfo = method_exists($this->getPredicate(), '__toString') ? (string)$this->predicate : '*closure*';
        return 'SimpleFilterProcessor[' . $predicateInfo . '? ' . $this->getOnPass() . ']';
    }
}