Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravelで学ぶDomain Model / Learn Domain model wit...

mpls104
January 17, 2019

Laravelで学ぶDomain Model / Learn Domain model with Laravel

mpls104

January 17, 2019
Tweet

More Decks by mpls104

Other Decks in Programming

Transcript

  1. EmployeeΫϥε <?php namespace App\Domain\Model; class Employee { private $employeeNumber; private

    $name; private $mailAddress; private $phoneNumber; /** * Employee constructor. * @param $employeeNumber * @param $name * @param $mailAddress * @param $phoneNumber */ public function __construct(EmployeeNumber $employeeNumber, Name $name, string $mailAddress, PhoneNumber $phoneNumber) { $this->employeeNumber = $employeeNumber; $this->name = $name; $this->mailAddress = $mailAddress; $this->phoneNumber = $phoneNumber; } public function toString() :string { return "Employee{" . "employeeNumber=" . $this->employeeNumber . ", name=" . $this->name . ", phoneNumber=" . $this->phoneNumber . ", mailAddress=" . $this->mailAddress .
  2. <?php namespace Tests\Unit; use App\Domain\Model\Employee; use App\Domain\Model\EmployeeNumber; use App\Domain\Model\MailAddress; use

    App\Domain\Model\Name; use App\Domain\Model\PhoneNumber; use Tests\TestCase; class EmployeeTest extends TestCase { private $employeeNumber; private $name; private $mailAddress; private $phoneNumber; public function setUp() { parent::setUp(); $this->employeeNumber = new EmployeeNumber("10111"); $this->name = new Name("ాதଠ࿠"); $this->mailAddress = new MailAddress("[email protected]"); $this->phoneNumber = new PhoneNumber("0120-111-111"); } ςετͷ४උ
  3. /** * * @return void * @test * @expectedException LogicException

    * @expectedExceptionMessage ϝʔϧΞυϨεͷܗ͕ࣜෆਖ਼Ͱ͢ */ public function ͋Γ͑ͳ͍ϝʔϧΞυϨε() { $illegalMailAddress = new MailAddress("test@@test.com"); $employee = new Employee( $this->employeeNumber, $this->name, $illegalMailAddress, $this->phoneNumber ); $employee->toString(); } } ࣦഊΛݟಧ͚Δ
  4. <?php namespace App\Domain\Model; class Employee { private $employeeNumber; private $name;

    private $mailAddress; private $phoneNumber; public function __construct(EmployeeNumber $employeeNumber, Name $name, ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹ//string $mailAddress, MailAddress $mailAddress, PhoneNumber $phoneNumber) { $this->employeeNumber = $employeeNumber; $this->name = $name; $this->mailAddress = $mailAddress; stringͰ͸දݱྗ͕ऑ͍
  5. <?php namespace App\Domain\Model; use Symfony\Component\Console\Exception\LogicException; class MailAddress { private $value

    = ""; /** * MailAddress constructor. * @param string $mailAddress */ public function __construct(?string $mailAddress) { $this->checkValidEmail($mailAddress); $this->value = $mailAddress; } private function checkValidEmail(String $mail) { if (!preg_match("/^[a-z0-9A-Z]+@[a-z0-9A-Z]+[a-z0-9A-Z.]/", $mail)) { throw new LogicException("ϝʔϧΞυϨεͷܗ͕ࣜෆਖ਼Ͱ͢"); } } } ࢓༷ΛΈͨ͞ͳ͍MailAddresΛ ࡞Βͤͳ͍
  6. /** * * @return void * @test * @expectedException LogicException

    * @expectedExceptionMessage ϝʔϧΞυϨεͷܗ͕ࣜෆਖ਼Ͱ͢ */ public function ͋Γ͑ͳ͍ϝʔϧΞυϨε() { $illegalMailAddress = new MailAddress("test@@test.com"); $employee = new Employee( $this->employeeNumber, $this->name, $illegalMailAddress, $this->phoneNumber ); $employee->toString(); } } ςετ͕௨Δ͜ͱΛݟಧ͚Δ
  7. ൃදޙͷิ଍ Entityʹ੹຿Λ࣋ͨͤΔͷͰ͋Ε͹ɺ ValueObject ʹू໿͢Δͷ͕๬·͍͠ɻ <?php namespace App\Domain\Model; class Employee {

    private $employeeNumber; private $employeeProfile; public function __construct( EmployeeNumber $employeeNumber, EmployeeProfile $employeeProfile ){ $this->employeeNumber = $employeeNumber; $this->employeeProfile = $employeeProfile; }