value ) ); } /** * @param mixed $other * * @return mixed */ public function getOrElse( $other ) { return $this->value; } /** * @param callable $fn * * @return Just|Nothing */ public function filter( $fn = null ) { $fn = $fn ?: Fns::identity(); return Maybe::fromNullable( $fn( $this->value ) ? $this->value : null ); } /** * @param callable $fn * * @return Just|Nothing */ public function reject( $fn = null ) { $fn = $fn ?: Fns::identity(); return $this->filter( Logic::complement( $fn ) ); } /** * @param callable $fn * * @return mixed */ public function chain( callable $fn ) { return $fn( $this->value ); } /** * @return bool */ public function isJust() { return true; } } class Nothing extends Maybe { use ConstApplicative; /** * @param callable $fn * * @return Nothing */ public function map( callable $fn ) { return $this; } /** * @return void * @throws \Exception */ public function get() { throw new \Exception( "Can't extract the value of Nothing" ); } /** * @param mixed|callable $other * * @return mixed */ public function getOrElse( $other ) { return value( $other ); } /** * @param callable $fn * * @return Nothing */ public function filter( callable $fn ) { return $this; } /** * @param callable $fn * * @return Nothing */ public function reject( callable $fn ) { return $this; } /** * @param callable $fn * * @return Nothing */ public function chain( callable $fn ) { return $this; } /** * @return bool */ public function isNothing() { return true; } } Maybe::init();