private function firstTagOr($errorMessage, $name) { try { if (!isset($this->tags[$name])) { throw new \Exception('Tag not found.'); } return Either::right(Arrays::head($this->tags[$name])); } catch (\Exception $e) { return Either::left($errorMessage); } }
/** * @return Either([String]) Validated and possibly modified request data */ static function validate(array $params) { if (empty($params['user'])) { return Either::left("Missing required parameter \"user\""); } else { if (empty($params['room'])) { return Either::left("Missing required parameter \"room\""); } else { return Either::right($params); } } }
{ public $id; public $user; public $room; public $message; public $time; public static $fromRequest; public static $fromJson; function __construct($user, $room, $message, $time, $id = null) { $this->user = $user; $this->room = $room; $this->message = $message; $this->time = $time; $this->id = $id; } function toJson() { return json_encode($this); } } FunctionalChatPost::$fromRequest = function (FunctionalChatRequest $request, $time) { if (isset($request->user) && isset($request->room) && isset($request->message)) { return Either::right(new FunctionalChatPost($request->user, $request->room, $request->message, $time)); } else { return Either::left('invalid request'); } }; FunctionalChatPost::$fromJson = function ($json) { return new FunctionalChatPost($json->user, $json->room, $json->message, $json->time, $json->id); };
/** * Converts a Failure to a Left, and Success to a Right * @return Left|Right */ public function toUnbiasedDisjunction() { return $this->isLeft() ? Either::ofLeft($this->merge()) : Either::ofRight($this->merge()); }
static function validateFromList(array $required, array $params) { foreach ($required as $name) { if (!isset($params[$name])) { return Either::left("Bad request: missing required parameter '{$name}'"); } } return Either::right($params); }