Rx\Observable::filter PHP Method

filter() public method

Emit only those items from an Observable that pass a predicate test.
public filter ( callable $predicate ) : Rx\Observable\AnonymousObservable
$predicate callable
return Rx\Observable\AnonymousObservable
    public function filter(callable $predicate)
    {
        return $this->lift(function () use($predicate) {
            return new FilterOperator($predicate);
        });
    }

Usage Example

Example #1
0
 /**
  * ConnectionSubject constructor.
  * @param ObservableInterface $rawDataIn
  * @param ObserverInterface $rawDataOut
  * @param bool $mask
  * @param bool $useMessageObject
  * @param string $subProtocol
  * @param RequestInterface $request
  * @param ResponseInterface $response
  */
 public function __construct(ObservableInterface $rawDataIn, ObserverInterface $rawDataOut, $mask = false, $useMessageObject = false, $subProtocol = "", RequestInterface $request, ResponseInterface $response)
 {
     $this->request = $request;
     $this->response = $response;
     $this->rawDataIn = new AnonymousObservable(function ($observer) use($rawDataIn) {
         return $rawDataIn->subscribe($observer);
     });
     $this->rawDataOut = $rawDataOut;
     $this->mask = $mask;
     $this->subProtocol = $subProtocol;
     // This can be used instead of the subjecg when this issue is addressed:
     // https://github.com/asm89/Rx.PHP/issues/20
     // Actually - using the subject is better so that the framing doesn't get done for every
     // subscriber.
     //$frames = $this->rawDataIn
     //    ->lift(new WebsocketFrameOperator());
     $frames = new Subject();
     $this->rawDataIn->lift(function () {
         return new WebsocketFrameOperator();
     })->subscribe(new CallbackObserver([$frames, "onNext"], function ($error) use($frames) {
         $close = $this->createCloseFrame();
         if ($error instanceof WebsocketErrorException) {
             $close = $this->createCloseFrame($error->getCloseCode());
         }
         $this->sendFrame($close);
         $this->rawDataOut->onCompleted();
         // TODO: Should this error through to frame observers?
         $frames->onCompleted();
     }, function () use($frames) {
         $this->rawDataOut->onCompleted();
         $frames->onCompleted();
     }));
     $this->controlFrames = $frames->filter(function (Frame $frame) {
         return $frame->getOpcode() > 2;
     });
     // default ping handler (ping received from far end
     $this->controlFrames->filter(function (Frame $frame) {
         return $frame->getOpcode() === $frame::OP_PING;
     })->subscribe(new CallbackObserver(function (Frame $frame) {
         $pong = new Frame($frame->getPayload(), true, Frame::OP_PONG);
         $this->sendFrame($pong);
     }));
     $frames->filter(function (Frame $frame) {
         return $frame->getOpcode() < 3;
     })->lift(function () use($mask, $useMessageObject) {
         return new WebsocketMessageOperator($mask, $useMessageObject);
     })->subscribe(new CallbackObserver(function ($x) {
         parent::onNext($x);
     }, function ($x) {
         parent::onError($x);
     }, function () {
         parent::onCompleted();
     }));
     $this->subProtocol = $subProtocol;
 }
All Usage Examples Of Rx\Observable::filter