Last active
July 5, 2017 22:32
-
-
Save naosim/ba8300f7cc70f7ee4a8bbc1b9f43b45f to your computer and use it in GitHub Desktop.
いろいろPHP
This file contains hidden or 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
<?php | |
interface DateTimeFactory { | |
public function createDateTime(): DateTime; | |
public function createUnixDateTime(): int; | |
} | |
class DateTimeFactoryImpl implements DateTimeFactory { | |
public function createDateTime(): DateTime { | |
return new DateTime(); | |
} | |
public function createUnixDateTime(): int { | |
return (new DateTime())->getTimestamp(); | |
} | |
} |
This file contains hidden or 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
<?php | |
interface SQLiteWrapper { | |
function executeSql(string $sql, $args); | |
function selectSql(string $sql, $args); | |
} | |
class SQLiteWrapperImpl implements SQLiteWrapper { | |
private $dbfile; | |
function __construct(string $dbfile) { | |
$this->dbfile = $dbfile; | |
} | |
function isTable(string $tableName) { | |
try { | |
$this->executeSql("SELECT ROWID FROM $tableName WHERE ROWID = 1", []); | |
return true; | |
} catch(Exception $e) { | |
return false; | |
} | |
} | |
function executeSql(string $sql, $args) { | |
$pdo = new PDO('sqlite:' . $this->dbfile); | |
// SQL実行時にもエラーの代わりに例外を投げるように設定 | |
// (毎回if文を書く必要がなくなる) | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// デフォルトのフェッチモードを連想配列形式に設定 | |
// (毎回PDO::FETCH_ASSOCを指定する必要が無くなる) | |
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
$stmt = $pdo->prepare($sql); | |
$ary = []; | |
foreach($args as $v) { | |
if(method_exists($v, 'getDbValue')) { | |
$ary[] = $v->getDbValue(); | |
} else if(method_exists($v, 'getValue')) { | |
$ary[] = $v->getValue(); | |
} else if(method_exists($v, 'getTimestamp')) { | |
$ary[] = $v->getTimestamp(); | |
} else { | |
$ary[] = $v; | |
} | |
} | |
$stmt->execute($ary); | |
return $stmt; | |
} | |
function getLastRowId(string $tableName) { | |
return $this->selectSql("SELECT ROWID FROM $tableName WHERE ROWID = last_insert_rowid()", []); | |
} | |
function selectSql(string $sql, $args) { | |
$stmt = $this->executeSql($sql, $args); | |
return $stmt->fetchAll(); | |
} | |
} | |
class SQLiteWrapperFactory { | |
public function create(string $dbfile): SQLiteWrapper { | |
return new SQLiteWrapperImpl($dbfile); | |
} | |
} |
This file contains hidden or 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
<?php | |
class Stream { | |
private $ary; | |
function __construct($ary) { | |
$this->ary = $ary !== null ? $ary : []; | |
} | |
function filter($f): Stream { | |
$a = []; | |
foreach ($this->ary as $k => $v) { | |
if($f($v, $k)) { | |
$a[] = $v; | |
} | |
} | |
return self::ofAll($a); | |
} | |
function map($f): Stream { | |
$a = []; | |
foreach ($this->ary as $k => $v) { | |
$a[] = $f($v, $k); | |
} | |
return self::ofAll($a); | |
} | |
function peek($f): Stream { | |
$this->map($f); | |
return $this; | |
} | |
function forEach($f): void { | |
$this->peek($f); | |
} | |
function reduce($f_memo_v, $memo) { | |
foreach ($this->ary as $k => $v) { | |
$memo = $f_memo_v($memo, $v, $k); | |
} | |
return $memo; | |
} | |
function toArray() { | |
return $this->ary; | |
} | |
function toJson() { | |
return json_encode($this->toArray()); | |
} | |
function count(): int { | |
return count($this->ary); | |
} | |
/** | |
* Get the first value or throw if list is empty | |
*/ | |
function get() { | |
return $this->getOrThrow(function(){ return new RuntimeException('array is empty'); }); | |
} | |
/** | |
* Get the first value or $default param if list is empty | |
*/ | |
function getOrElse($default) { | |
if(count() == 0) { | |
return $default; | |
} | |
return $this->toArray()[0]; | |
} | |
function getOrThrow($f) { | |
if(count() == 0) { | |
throw $f(); | |
} | |
return $this->toArray()[0]; | |
} | |
function isEmpty(): boolean { | |
return $this->count() === 0; | |
} | |
function isDefined(): boolean { | |
return !$this->isEmpty(); | |
} | |
public static function ofAll($ary) { | |
return new Stream($ary); | |
} | |
public static function of(...$v) { | |
return new Stream($v); | |
} | |
} |
This file contains hidden or 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
<?php | |
class Test { | |
function setupAll() {} | |
function cleanupAll() {} | |
function setup() {} | |
function cleanup() {} | |
function run() { | |
assert_options(ASSERT_ACTIVE, 1); | |
assert_options(ASSERT_WARNING, 1); | |
assert_options(ASSERT_QUIET_EVAL, 0); | |
assert_options(ASSERT_EXCEPTION, 1); | |
$result = []; | |
$className = new TestClassName(get_class($this)); | |
$this->setupAll(); | |
foreach (get_class_methods($this) as $methodName) { | |
if(strpos($methodName, 'test') !== false) { | |
$result[] = $this->_run($className, new TestMethodName($methodName)); | |
} | |
} | |
$this->cleanupAll(); | |
return $result; | |
} | |
private function _run(TestClassName $testClassName, TestMethodName $testMethodName) { | |
try { | |
$this->setup(); | |
$methodName = $testMethodName->getValue(); | |
$this->$methodName(); | |
return new TestResult( | |
$testClassName, | |
$testMethodName, | |
TestResultType::ok() | |
); | |
} catch(Exception $e) { | |
return new TestResult( | |
$testClassName, | |
$testMethodName, | |
TestResultType::ng($e) | |
); | |
} catch(Error $e) { | |
return new TestResult( | |
$testClassName, | |
$testMethodName, | |
TestResultType::ng($e) | |
); | |
} finally { | |
$this->cleanup(); | |
} | |
} | |
} | |
class TestClassName extends StringVO {} | |
class TestMethodName extends StringVO {} | |
class TestResultType extends StringVO { | |
public $ngReason; | |
public function __construct(string $value, string $ngReason = null) { | |
$this->value = $value; | |
$this->ngReason = $ngReason; | |
} | |
public static function ok() { return new TestResultType("ok"); } | |
public static function ng(string $ngReason) { | |
return new TestResultType("ng", $ngReason); | |
} | |
} | |
class TestResult { | |
function __construct( | |
TestClassName $testClassName, | |
TestMethodName $testMethodName, | |
TestResultType $testResultType | |
) { | |
eachArgs(func_get_args(), function($k, $v){ $this->$k = $v; }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment