±ÌLÍPHP Advent Calendar 2017Ì2úÚÅ·B
±±ÅßA{ÆÌÙ¤ªÊÙÇZµA·Á©èuOð¢Ä¢È©Á½@HirakuÅ·B
¾¢ÔO̱ÆÅ·ªA¡NÌPHPJt@XªÅáOÌbðµÄ«Üµ½B
±ÌÌ\ÅÍáOÉÖ·éTvªå¾Á½ÌÅAáONX»ÌàÌÌÝvÉ¢Äâ«ð¢ÄÝé±ÆɵܷB
áOÌ é¢EÏ
PHP7Å͢©ÌG[ªáOƯ¶®ð¦·æ¤ÉÈèAG[ÅÍÈáO@\ðg¤¾êÉlÏíèµÜµ½B
áOÆ¢¤ÌÍA©ÈèNª«Ì¢TOÅ èAáOÌ é¢EÅÍȺÌOñɧ±ƪÃṲ̀¿Év³êÜ·B
- DZÅàáOª¶·éÂ\«ª é
- áOÉæÁÄf³êÄàA\tgEFAªsKØÈóÔÉÈçÈ¢æ¤ÉÛØ·éÌÍevO}ÌÓC(áOÀS)
½Æ¦ÎAȺÌÖÅáOª¶·éÂ\«ª éÌÍÇÌsŵ天H
function createStruct(DateTimeImmutable $now): Struct {
$obj = new Struct; // 1sÚ
$obj->setNow($now); // 2sÚ
return $obj->init(); // 3sÚ
}
³ðÍ1~3SÄ
Å·B±êÍStruct
NXª ȽÌèÉæÁÄ©êAS\bhÅáOðthrowµÄ¢È¢ÆµÄà ¶·éÂ\«ÍÁ¦Ü¹ñB
à¤APHP;ê©ÌªáOð°é±Æª é¢EÉÈÁĢܷBá¦ÎVOiâA[U[ÉæéNGXgfÈÇB¡ÌdlÅÍáOÅÍÈÄàA¢ÂáOÉÈÁÄ਩µÈ¢à̪ èÜ·B
»êÉA éúËRAStructNXª¼ÌNX/CuÉ˶·éæ¤ÈR[hÉÏX³ê½Æ·éÆA©®IÉ˶æÌNX/Cuªthrow·éáOªp³³êéæ¤ÉÈèÜ·B
±ñÈ¢EÅÍAutry~catchÅSáOð¨¦ÄN±³È¢æ¤É·é¼IvÆ¢¤Ì͵µ·¬Ü·B»¤ÅÍÈAȺðOñÉ·é׫ŷB
- DZÅàáOª¶·éÂ\«ª é
- áOÉæÁÄf³êÄàA\tgEFAªsKØÈóÔÉÈçÈ¢æ¤ÉÛØ·éÌÍevO}ÌÓC(áOÀS)
áOÀSÉÖµÄͪ̿Ţ½ÌÅ»¿çðQƵĺ³¢B
PHPÅthrowÅ«éàÌ
uDZÅàáOª¶·éÂ\«ª év
»ñÈsm©ÈêÅAR[hÈñÄ¢Äçêé©IÆ¢¤C¿ÉàÈèÜ·ªAêûÅPHPÌêAthrow³êÄéàÌÉ¢§Àª éÌŵ½B
PHPÅÍthrowåÌãëɯéàÌÍThrowable
C^[tF[Xðp³µ½CX^X¾¯É§À³êĢܷB(PHP5nÅÍException
NX©A»êðp³µ½NXÌCX^XÌÝB)
ÈÌÅAáOƵÄòñÅ«½àÌÍK¸ÈºÌ\bhª¶¦Ä¢Ü·B
- getMessage(): string
- getCode(): int
- getPrevious(): Throwable
ÂlIÉͱ̧ñÍÖ¾ÆvÁÄ¢ÄAÅáÀÌC^[tF[XªÆLxÈÌÅA±ÌtH[}bgÉFªí¹êÎFXƶ«â·ÈèÜ·B
á) PHPUnitÌáOnhO
¢Â©Ìt[[NÅÍAÇñÈáOªN«½ÆµÄàK¸±êçÌ\bhª¶¦Ä¢é±ÆðpµÄAtH[}bg»µÄáOð\¦µÄêÜ·B
½Æ¦ÎeXgt[[NÅ éPHPUnitÅAeXgÀsÉáOªN«éÆAȺÌæ¤È\¦ÉÈèÜ·B
.E..................................
Time: 59 ms, Memory: 4.00MB
There was 1 error:
1) Spindle\Collection\CollectionTest::testFoo
Exception: example message
/Users/hiraku/src/github.com/spindle/spindle-collection/tests/CollectionTest.php:16
{áONX¼}: {getMessage()Ìg} Æ¢¤´¶Å·ËB
á) symfony/consoleÌáOnhO
R}hCc[ðìéÆ«ÉægíêéAsymfony/console¾ÆAÉáOª¶·éÆȺÌæ¤È\¦ð©®ÅìÁÄêÜ·B
±êàáONX¼ÆgetMessageÌgðÌpµÄ¢Ü·ËB
messageâcodeͽð׫©
áOÌRXgN^Í3ÂøðæèÜ·B
- string $message
- int $code
- Throwable $previous
¡ÜÅÉq×½æ¤Ègíêûðzè·éÉA$message
ͽªN«½Ì©ðÈé×Ú×É׫ŵå¤B
ú{ê©pê©ÍÁÉKè³êĢȢÌÅvWFNgÌûjæÅ·ªAÅ趾¾¯AÆ©ÅÍÈóµðڵྷéidâÈÇàßÞÆǢŵå¤B
$code
ÉÖµÄÍAPHPàÅàÁɱêƾÁ½WÍÈ¢æ¤Év¢Ü·BLb`¤Æ¬ÁÄÁèÌR[hðüêÄࢢŷµAgíȯêÎȪ©0Å©ÜíȢŵå¤B
¿ÈÝÉAwebAvP[VÌGh[U[ÉεÄÍÚ×·¬éG[Í\¦·é׫ÅÍÈ¢Aݽ¢Èc_à èÜ·ªA
- áOÍ ÜÅÚ×Èmessageðìé
- Bµ½¢ÈçêxLb`µÄnhO·é
Æ¢¤ûjÅ é׫ŷB»à»àAáOð°éûÍANª±ÌáOðó¯~ßé©ð§äÅ«È¢ÌÅ·©çB
áOÌ°¼µÆ$previous
Ìp
êÉæÁÄÍA˶æÌCuª°Ä«½áOðLb`µÄAÊÌáOƵݼ·±Æª èÜ·B
àµf¼ÉáOðì輵İĵܤÆAÀ¿$e
ª¬èÂÔ³êA^Ì´öªí©çÈÈÁĵܢܷB
try {
$foo = doSomething();
} catch (\Exception $e) {
throw new MyException('doSomethingɸsµÜµ½');
}
±¤¢¤Æ«ÍARXgN^ÌæOøÅ é $previous
ðpµÜµå¤B
try {
$foo = doSomething();
} catch (\Exception $e) {
throw new MyException('doSomethingɸsµÜµ½', 500, $e);
}
±¤·éÆA^Ì´öðHêéæ¤ÉµÂÂA\ÊãÌáONXð·µÖ¦é±ÆªÅ«Ü·B symfony/consoleÆ©ÅàA¿áñÆpreviousÉÍγêÄÄA±ñÈÉ\¦µÄêÜ·B
àÁÆ¡GÈáOðìé
½Æ¦ÎAHTMLtH[Ìof[VG[ðl¦ÄÝÜ·B
- family_name
- first_name
- family_name_kana
- first_name_kana
- gender
±êç̤¿Afirst_name_kanaÆemailÉüÍG[ª Á½ÆµÜ·B messageÍAKÉüsÅ¡bZ[WðlßÄàǢŵå¤B
first_name_kanaÉJ^JiÅü͵ľ³¢B
emailÍ100¶ÈàÅü͵ľ³¢B
µ©µAÀÛÌêÊðl¦éÆAȺÌæ¤Èðìè½ÈéÌÅÍȢŵ天B
- tH[ÌïÌIÈêÉÔ¶ÅG[ðoµ½¢
- »à»àÌüÍlà\¦µ½¢
±¤¢¤Æ«A½¾ÌstringÅ é $message
¾¯Åâèè·éÌ͵µ¢à̪ èÜ·BÈÌÅAáONXɼÌlà~¦ÄµÜ¤ÆǢŵå¤B
class ValidationException extends \RuntimeException
{
private $params;
private $errors;
public function setParams(array $params): self
{
$this->params = $params;
return $this;
}
public function getParams(): array
{
return $this->params;
}
public function setErrors(array $errors): self
{
$this->errors = $errors;
return $this;
}
public function getErrors(): array
{
return $this->errors;
}
}
ÅAáOð°éêÊűñÈɵܷB
/* //±ñÈC[W
$params = [
'first_name_kana' => '½ë¤',
'email' => '[email protected]',
//...
];
*/
$errors = [
'first_name_kana' => 'first_name_kanaÉJ^JiÅü͵ľ³¢B',
'email' => 'emailÍ100¶ÈàÅü͵ľ³¢B',
];
throw (new ValidationException(implode("\n", $errors), 400))
->setParams($params)
->setErrors($errors);
±êÈçAcatchµ½ValidationException©ç$paramsâ$errorsðæèo¹ÄAãàµâ·¢Åµå¤B
áOÉsetterð¶â·Z
PHPÌáOÍAnewµ½ÓÅ©®IÉX^bNg[XðûWµA~¦éÆ¢¤Á¥ª èÜ·B ±Ì@\ð©·ÈçÎAáONXÌnewÍÉÍx^Å¿µÄAÖ»ÈÇàð¯½Ù¤ªÖÅ·B
êûÅAáONXÌRXgN^ÍWªÜÁĨèARXgN^øðÏX·éÌÍ ÜèsVª¢¢ÆྦܹñB
»±ÅAæöÌæ¤Éreturn $this
·ésetterð¶â·æ¤É·éÆA»ÌêÅg£p[^ðⷱƪūÄg¢èªÇÈèÜ·B
ÜÆß
PHPÌáOÆ¢¤ÆAÆà·êÎÇñÈáO^ðg¤×«©Aݽ¢Èc_ª½È調ŷªA
Ç¿ç©Æ¾¦Î $message
ɽð׫©ÌûªdvÈ|Cg¾Æv¢Ü·B
¤ÜáO̦·_ñÉæéÆAPHPUnitÈÇÌeít[[NÆàß²µâ·ÈèÜ·ÌÅAêxáOÝv𩼵ÄÝÄÍ¢©ªÅµå¤©B
PHPAhxgJ_[2017Ìê https://qiita.com/advent-calendar/2017/php