-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer type of property based on what was passed to object constructor
- Loading branch information
Showing
8 changed files
with
149 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
namespace Lib; | ||
|
||
/** | ||
* Basically the same thing as Rust's std::Result is, only using PHP Exceptions | ||
*/ | ||
class Result | ||
{ | ||
public $isOk; | ||
public $result; | ||
public $error; | ||
|
||
public static function makeOk($result) | ||
{ | ||
return new self(true, $result); | ||
} | ||
|
||
public static function makeError(\Exception $error) | ||
{ | ||
return new self(false, null, $error); | ||
} | ||
|
||
private function __construct($isOk, $result, \Exception $error = null) | ||
{ | ||
if (!$isOk && is_null($error)) { | ||
$error = new \Exception('Result Error'); | ||
} | ||
|
||
$this->isOk = $isOk; | ||
$this->result = $result; | ||
$this->error = $error; | ||
} | ||
|
||
public function isOk() | ||
{ | ||
return $this->isOk; | ||
} | ||
|
||
public function unwrap() | ||
{ | ||
if ($this->isOk()) { | ||
return $this->result; | ||
} else { | ||
throw $this->error; | ||
} | ||
} | ||
|
||
public function getUsingDefault($default = null) | ||
{ | ||
if ($this->isOk()) { | ||
return $this->result; | ||
} else { | ||
return $default; | ||
} | ||
} | ||
|
||
/** @return \Exception */ | ||
public function getErrorUsingDefault($default = null) | ||
{ | ||
if ($this->isOk()) { | ||
return $default; | ||
} else { | ||
return $this->error; | ||
} | ||
} | ||
|
||
/** | ||
* @param \Closure $mapper - returns new Result | ||
* @return Result - same if was error or mapped if ok | ||
*/ | ||
public function flatMap(\Closure $mapper): Result | ||
{ | ||
return $this->isOk() ? $mapper($this->unwrap()) : $this; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
if ($this->isOk()) { | ||
return 'Result<Ok:'.strval($this->result).'>'; | ||
} else { | ||
return 'Result<Error>'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters