第一次分享东西
项目地址: https://github.com/lisqorz/option
除了这些,其实还想在里面加上 PHP 的 标准库方法,这样就可以更加函数式了
小例子
case 1
// Option
$res = Some($db->get())->expect(new \Exception("msg"));
// native
$res = $db->get();
if (is_null($res)) {
throw new \Exception("msg");
}
case2
// Option
$_POST["hello"] = null;
Some($_POST['hello'])->unwrapOr("hi"); // hi
$_POST["hello"] = "hello";
Some($_POST['hello'])->unwrapOr("hi"); // hello
// ... but we have $_POST["hello"]??"hi" 2333
Case3
// scenario
$obj = new Obj();
$obj->attr = null; // attr is Object;
// Option
// None
$attr = Some($obj->attr)->andThen(function($attr){
return "";
})->unwrapOr("no success");
//Some
$attr = Some(1)->andThen(function($attr){
return null;
})->unwrapOr("no success");
// native
$attr = "no success";
if ($obj->attr) {
$attr = (function($attr){return ""; })()
}
if (!$attr) {
$attr = "no success";
}
添加一个小例子,并不是用来声明Option有多好,而是想分享一下想法,如果有其它想法,可以一起延伸、讨论 case 0
Native
try {
$pdo = new PDO($db['dsn'], $db['username'], $db['password'], $options);
} catch (PDOException $e) {
throw new PDOException('数据库连接失败:' . $e->getMessage());
}
Option
/** @var PDO $conn */
Some(!$pdo->errorCode())->expect(new PDOException("数据库链接失败" . $pdo->errorCode()));
Native
$statement = $pdo->query("select count(id) from link");
$cnt = 0;
if (!$statement) {
$cnt = $statement->rowCount();
}
Option
$cnt = Some($pdo->query("select count(id) from link"))->andThen(function ($statement) {
return $statement->rowCount();
})->unwrapOr(0);
print_r($cnt);
Native
$query = $pdo->query("select id,name from link");
$result = [];
if ($query) {
$res = $query->fetchAll();
if ($res) {
foreach ($res as $key => $item) {
$result[] = [
"link_id" => $item["id"],
"link_name" => $item["name"] ?? "匿名链接",
];
}
}
}
Option
$result = Some($pdo->query("select id,name from link"))->andThen(function ($state) {
return $state->fetchAll();
})->filter(function ($item) {
return [
"link_id" => $item["id"],
"link_name" => Some($item["name"])->unwrapOr("匿名链接"),
];
})->unwrapOr([]);
print_r($result);
1
AngelCriss 2018-09-12 18:25:21 +08:00 via Android
啥叫函数式?
|