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

Hackで作る堅実な アプリケーションアーキテクチャ / Hack-application-architecture

Hackで作る堅実な アプリケーションアーキテクチャ / Hack-application-architecture

PHP Conference Sendai 2019で利用したスライドです

yuuki takezawa

January 22, 2019
Tweet

More Decks by yuuki takezawa

Other Decks in Programming

Transcript

  1. Profile • ஛ᖒ ༗و / ytake • גࣜձࣾΞΠελΠϧ CTO •

    PHP, Hack, Go, Scala • Apache Hadoop, Apache Spark, Apache Kafka
 • twitter https://twitter.com/ex_takezawa • facebook https://www.facebook.com/yuuki.takezawa • github https://github.com/ytake
  2. Agenda • PHP Array / Hack Arrays • Shapes In

    Hack • Generics (With Dependency Injection) • Trait And Interface Requirements • Implementing DDD / Generic Repository
  3. Hack ϥΠϒϥϦ • facebook/hack-router Hackઐ༻ϧʔλʔ • facebook/hh-clilib CLIϥΠϒϥϦ • usox/hackttp

    Hack HTTPΠϯλʔϑΣʔε࣮૷ • ytake/hungrr Hack HTTPΠϯλʔϑΣʔε࣮૷ • hhpack/service-locator αʔϏεϩέʔλʔɾDI • nazg/glue αʔϏεϩέʔλʔɾDI • nazg/heredity ϛυϧ΢ΣΞσΟεύονϟ • nazg/hcache Ωϟογϡ
  4. <?php declare(strict_types=1); class ArraySet { protected $collect = []; public

    function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): array { return $this->collect; } }
  5. <?php declare(strict_types=1); class ArraySet { protected $collect = []; public

    function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): array { return $this->collect; } } ෆ࣮֬ͳܕͷࠞೖΛ๷͙ͨΊ
 ೖྗΛ੍ݶ
  6. <?php declare(strict_types=1); class ArraySet { protected $collect = []; public

    function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): array { return $this->collect; } } ੍ޚෆՄ
  7. class ExtendArraySet extends ArraySet { public function all(): array {

    $this->collect[1] = 2; return $this->collect; } } ੍ޚෆՄ
  8. <?hh // strict class ArraySet { protected darray<string, string> $collect

    = []; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): darray<string, string> { return $this->collect; } }
  9. <?hh // strict class ArraySet { protected darray<string, string> $collect

    = []; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): darray<string, string> { return $this->collect; } } ෆ࣮֬ͳܕͷࠞೖΛ๷͙ͨΊ
 ೖྗΛ੍ݶ
  10. <?hh // strict class ArraySet { protected darray<string, string> $collect

    = []; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): darray<string, string> { return $this->collect; } } ܧঝ࣌Ͱ΋ͦͷ··੍ޚՄ
  11. class ExtendArraySet extends ArraySet { public function all(): darray<string, string>

    { $this->collect[1] = 2; return $this->collect; } } ૠೖෆՄ
  12. class ExtendArraySet extends ArraySet { public function all(): darray<string, string>

    { $this->collect[1] = 2; return $this->collect; } } ໭ΓͷܕΛมߋ͢Δ͜ͱ΋ෆՄ
  13. src/ArraySet.php:19:5,25: Invalid assignment (Typing[4110]) src/ArraySet.php:5:20,25: This is a string src/ArraySet.php:19:20,20:

    It is incompatible with an int src/ArraySet.php:20:12,25: Invalid return type (Typing[4110]) src/ArraySet.php:18:33,38: This is a string src/ArraySet.php:19:20,20: It is incompatible with an int
  14. public function merge(): darray<mixed, mixed> { $a = new ArraySet();

    $a->add('PHP', 'Arrays'); $ar = $a->all(); $ar[1] = 11; return $ar; } औಘޙɺ ҟͳΔܕͷ஋Λࠞೖͤ͞Δ͜ͱ͸Մೳ *໭ΓͷܕΛఆٛ͢Δ͜ͱ
  15. <?hh // strict class Sample { protected varray<string> $varray =

    varray[ 'php', 'hack' ]; protected darray<int, string> $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray<int> { return $this->varray; } public function getDArray(): darray<string, string> { return $this->darray; } }
  16. <?hh // strict class Sample { protected varray<string> $varray =

    varray[ 'php', 'hack' ]; protected darray<int, string> $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray<int> { return $this->varray; } public function getDArray(): darray<string, string> { return $this->darray; } } ܕҧ͍
  17. <?hh // strict class Sample { protected varray<string> $varray =

    varray[ 'php', 'hack' ]; protected darray<int, string> $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray<int> { return $this->varray; } public function getDArray(): darray<string, string> { return $this->darray; } } ໭Γͷܕҧ͍
  18. <?hh // strict class Sample { protected varray<string> $varray =

    varray[ 'php', 'hack' ]; protected darray<int, string> $darray = darray[ 'testing' => 'testing', 1 => 'testing' ]; public function failedVArray(): varray<int> { return $this->varray; } public function getDArray(): darray<string, string> { return $this->darray; } } ໭Γͷܕҧ͍
  19. <?hh // strict class ArraySet { protected dict<string, string> $collect

    = dict[]; public function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): dict<string, string> { return $this->collect; } } dictࢦఆ
  20. Hack Arrays • Hack Arrays͸
 isԋࢉࢠ(HackͷΈͷԋࢉࢠ)Λ༻͍ͯ൑ఆՄೳ
 $dict is dict<_, _>

    , etc • Hack Arrays͸ͦΕͧΕͷ഑ྻʹม׵Մೳ
 (Collection͔ΒHack Arrays΍ͦͷٯ΋)
  21. abstract final class dict<+Tk as arraykey, +Tv> implements Indexish<Tk, Tv>,

    XHPChild {} abstract final class keyset<+T as arraykey> implements Indexish<T, T>, XHPChild {} abstract final class vec<+T> implements Indexish<int, T>, XHPChild {}
  22. class CollectionSet { protected Map<string, string> $collect = Map{}; public

    function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): Map<string, string> { return $this->collect; } } Mapࢦఆ
  23. class CollectionSet { protected Map<string, string> $collect = Map{}; public

    function add(string $key, string $value): void { $this->collect[$key] = $value; } public function all(): Map<string, string> { return $this->collect; } } ഑ྻͱಉ͡Α͏ʹૠೖ
  24. class HackMap { protected Map<string, string> $collect = Map{ 'a'

    => 'b', 'c' => 'd', }; public function filterRemove( string $keyName ): Map<string, string> { return $this->collect ->filterWithKey(($key, $_) ==> $key !== $keyName); } public function remove( string $keyName ): Map<string, string> { return $this->collect->remove($keyName); } } ࢦఆͨ͠ΩʔҎ֎ͷ΋ͷΛ MapͰฦ٫
  25. class HackMap { protected Map<string, string> $collect = Map{ 'a'

    => 'b', 'c' => 'd', }; public function filterRemove( string $keyName ): Map<string, string> { return $this->collect ->filterWithKey(($key, $_) ==> $key !== $keyName); } public function remove( string $keyName ): Map<string, string> { return $this->collect->remove($keyName); } } ࢦఆͨ͠ΩʔΛ࡟আ͠ɺ MapͰฦ٫
  26. <?php declare(strict_types=1); class GenerateArrayField { public function getArray(): array {

    return [ 'q' => 'qwerty', 'w' => 'wertyu', ]; } } ഑ྻͷϑΟʔϧυʹରͯ͠
 ੍ݶෆՄ
  27. <?hh // strict type SampleShape = shape('q' => string, 'w'

    => string); class GenerateArrayField { public function getArray(): SampleShape { return shape( 'q' => 'qwerty', 'w' => 'wertyu', ); } }
  28. <?hh // strict type SampleShape = shape('q' => string, 'w'

    => string); class GenerateArrayField { public function getArray(): SampleShape { return shape( 'q' => 'qwerty', 'w' => 'wertyu', ); } } ഑ྻͷϑΟʔϧυʹܕఆٛ
  29. <?hh // strict type SampleShape = shape('q' => string, 'w'

    => string); class GenerateArrayField { public function getArray(): SampleShape { return shape( 'q' => 'qwerty', 'w' => 'wertyu', ); } } shapeΛ࢖ͬͯهड़
  30. public function getArrays(): vec<SampleShape> { return vec[ shape('q' => 'qwerty',

    'w' => 'wertyu',), shape('q' => 'PHP', 'w' => 'Hack',), shape('q' => 'Java', 'w' => 'Scala',), ]; } vec഑ྻʹshapesΛ
 ૊Έ߹ΘͤͯΑΓ࣮֬ͳ഑ྻ΁
  31. namespace Acme\Shapes; type SampleShape = shape( 'a' => int, 'b'

    => int ); type NestShape = shape( 'sample' => SampleShape, 'vec' => vec<string> ); class NestedShape { public function nest(): NestShape { return shape( 'sample' => shape( 'a' => 0, 'b' => 1 ), 'vec' => vec[ 'shapes' ], ); } } Shapesಉ࢜Λ૊Έ߹Θͤͯ
 ఆٛՄ
  32. The field 'z' is not defined in this shape type,

    and this shape type does not allow unknown fields. The field 'z' is set in the shape. ఆٛ͞Ε͍ͯͳ͍ϑΟʔϧυ ར༻࣌ʹΤϥʔ
  33. class HalShape { const type embeddedLinks = shape( 'name' =>

    string, '_links' => shape( 'self' => shape( 'href' => string ) ) ); public function index(): void { $url = [ 'name' => 'hack', '_links' => [ 'self' => [ 'href' =>'uri' ] ] ]; \var_dump($url as this::embeddedLinks); } }
  34. class HalShape { const type embeddedLinks = shape( 'name' =>

    string, '_links' => shape( 'self' => shape( 'href' => string ) ) ); public function index(): void { $url = [ 'name' => 'hack', '_links' => [ 'self' => [ 'href' =>'uri' ] ] ]; \var_dump($url as this::embeddedLinks); } } type constantsΛ࢖ͬͯ
 shapeΛهड़
  35. class HalShape { const type embeddedLinks = shape( 'name' =>

    string, '_links' => shape( 'self' => shape( 'href' => string ) ) ); public function index(): void { $url = [ 'name' => 'hack', '_links' => [ 'self' => [ 'href' =>'uri' ] ] ]; \var_dump($url as this::embeddedLinks); } } ഑ྻͳͲͰ஋Λهड़
  36. class HalShape { const type embeddedLinks = shape( 'name' =>

    string, '_links' => shape( 'self' => shape( 'href' => string ) ) ); public function index(): void { $url = [ 'name' => 'hack', '_links' => [ 'self' => [ 'href' =>'uri' ] ] ]; \var_dump($url as this::embeddedLinks); } } shapeͷఆٛΛݩʹ഑ྻΛݕࠪ
  37. $container = new Container(); $container->set( \stdClass::class, ($container) ==> new \stdClass()

    ); $stdClass = $container->getInstance(\stdClass::class); ೖྗΛΫϥε໊ͷΈͱ੍ͯ͠ݶ
  38. $container = new Container(); $container->set( \stdClass::class, ($container) ==> new \stdClass()

    ); $stdClass = $container->getInstance(\stdClass::class); Typechecker͕൑ఆͰ͖ͳ͍৔߹͸ɺ isͳͲΛར༻ͯ͠อূ͢Δඞཁ͕͋Δ
  39. <?hh // strict namespace Acme; use namespace HH\Lib\{C, Str}; class

    Container<T> { private dict< string, (Scope, (function(\Acme\Container<T>): T)) > $map = dict[]; public function set( classname<T> $id, (function(\Acme\Container<T>): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); }
  40. <?hh // strict namespace Acme; use namespace HH\Lib\{C, Str}; class

    Container<T> { private dict< string, (Scope, (function(\Acme\Container<T>): T)) > $map = dict[]; public function set( classname<T> $id, (function(\Acme\Container<T>): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); } Genericsར༻
  41. <?hh // strict namespace Acme; use namespace HH\Lib\{C, Str}; class

    Container<T> { private dict< string, (Scope, (function(\Acme\Container<T>): T)) > $map = dict[]; public function set( classname<T> $id, (function(\Acme\Container<T>): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); } dictΛར༻͠ɺ ίʔϧόοΫͰTΛฦ٫͢Δͱఆٛ
  42. <?hh // strict namespace Acme; use namespace HH\Lib\{C, Str}; class

    Container<T> { private dict< string, (Scope, (function(\Acme\Container<T>): T)) > $map = dict[]; public function set( classname<T> $id, (function(\Acme\Container<T>): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); } classname<T> 
 ͭ·Γଘࡏ͢ΔΫϥε໊ͷΈڐՄͱ͢Δ
  43. <?hh // strict namespace Acme; use namespace HH\Lib\{C, Str}; class

    Container<T> { private dict< string, (Scope, (function(\Acme\Container<T>): T)) > $map = dict[]; public function set( classname<T> $id, (function(\Acme\Container<T>): T) $callback, Scope $scope = Scope::PROTOTYPE, ): void { $this->map[$id] = tuple($scope, $callback); } tupleͰΠϯελϯεੜ੒ํ๏ͱɺ ίʔϧόοΫΛอ࣋
  44. <<__Rx>> public function getInstance(classname<T> $t): T { return $this->resolve($t); }

    <<__Memoize>> protected function shared(classname<T> $id): T { list($_, $callable) = $this->map[$id]; return $callable($this); } <<__Rx>> public function has(string $id): bool { return C\contains_key($this->map, $id); }
  45. <<__Rx>> public function getInstance(classname<T> $t): T { return $this->resolve($t); }

    <<__Memoize>> protected function shared(classname<T> $id): T { list($_, $callable) = $this->map[$id]; return $callable($this); } <<__Rx>> public function has(string $id): bool { return C\contains_key($this->map, $id); } hhvm/hsl
 array_key_existsϥούʔؔ਺
  46. <<__Rx>> public function getInstance(classname<T> $t): T { return $this->resolve($t); }

    <<__Memoize>> protected function shared(classname<T> $id): T { list($_, $callable) = $this->map[$id]; return $callable($this); } <<__Rx>> public function has(string $id): bool { return C\contains_key($this->map, $id); } ϝϞԽࢦఆ Ҿ਺Λར༻ͯ͠Ωϟογϡ͞ΕΔ γϯάϧτϯ
  47. <<__Rx>> public function getInstance(classname<T> $t): T { return $this->resolve($t); }

    <<__Memoize>> protected function shared(classname<T> $id): T { list($_, $callable) = $this->map[$id]; return $callable($this); } <<__Rx>> public function has(string $id): bool { return C\contains_key($this->map, $id); } reactiveʹ
  48. <<__Rx>> protected function resolve(classname<T> $id): T { if ($this->has($id)) {

    list($scope, $callable) = $this->map[$id]; if ($callable is nonnull) { if ($scope === Scope::SINGLETON) { return $this->shared($id); } return $callable($this); } } throw new Exception\NotFoundException( Str\format('Identifier "%s" is not binding.', $id), ); }
  49. <<__Rx>> protected function resolve(classname<T> $id): T { if ($this->has($id)) {

    list($scope, $callable) = $this->map[$id]; if ($callable is nonnull) { if ($scope === Scope::SINGLETON) { return $this->shared($id); } return $callable($this); } } throw new Exception\NotFoundException( Str\format('Identifier "%s" is not binding.', $id), ); } nullͰͳ͍͔Ͳ͏͔
  50. <<__Rx>> protected function resolve(classname<T> $id): T { if ($this->has($id)) {

    list($scope, $callable) = $this->map[$id]; if ($callable is nonnull) { if ($scope === Scope::SINGLETON) { return $this->shared($id); } return $callable($this); } } throw new Exception\NotFoundException( Str\format('Identifier "%s" is not binding.', $id), ); } γϯάϧτϯͷ৔߹͸ɺ ϝϞԽ
  51. /** * Fire an event when a lockout occurs. *

    * @param \Illuminate\Http\Request $request * @return void */ protected function fireLockoutEvent(Request $request) { event(new Lockout($request)); } /** * Get the rate limiter instance. * * @return \Illuminate\Cache\RateLimiter */ protected function limiter() { return app(RateLimiter::class); }
  52. <?hh // strict namespace Acme; trait MessageTrait { require implements

    RequestInterface; private dict<string, vec<string>> $headers = dict[]; }
  53. <?hh // strict namespace Acme; trait MessageTrait { require implements

    RequestInterface; private dict<string, vec<string>> $headers = dict[]; } ࢦఆͨ͠ΠϯλʔϑΣʔεΛ࣮૷ ͨ͠ΫϥεͷΈʹར༻Λ੍ݶ
  54. <?hh // strict namespace Acme; class Request implements RequestInterface {

    use MessageTrait; protected function getVersion(): string { return '1.1'; } }
  55. trait MessageTrait { require implements RequestInterface; private dict<string, vec<string>> $headers

    = dict[]; public function castIntVersion(): int { return (int) $this->getVersion(); } }
  56. trait MessageTrait { require implements RequestInterface; private dict<string, vec<string>> $headers

    = dict[]; public function castIntVersion(): int { return (int) $this->getVersion(); } } ϦΫΤετΫϥεͷϝιουΛ ར༻͍ͨ͠
  57. src/MessageTrait.php:10:25,34: Could not find method getVersion in an object of

    type Acme\MessageTrait (Typing[4053]) src/MessageTrait.php:10:18,22: This is why I think it is an object of type Acme\MessageTrait src/MessageTrait.php:5:7,18: Declaration of Acme\MessageTrait is here ΠϯλʔϑΣʔεʹఆٛ͞Ε͍ͯͳ͍ ϝιου͸ίʔϧෆՄ
  58. <?hh // strict namespace Acme; trait MessageTrait { require implements

    RequestInterface; private dict<string, vec<string>> $headers = dict[]; protected \stdClass $class; public function getStdClass(): \stdClass { return $this->class; } }
  59. <?hh // strict namespace Acme; trait MessageTrait { require implements

    RequestInterface; private dict<string, vec<string>> $headers = dict[]; protected \stdClass $class; public function getStdClass(): \stdClass { return $this->class; } } stdClassΛϓϩύςΟͱͯ͠ѻ͏৔߹
  60. <?hh // strict namespace Acme; class Request implements RequestInterface {

    use MessageTrait; protected function getClass(): string { return \strval($this->class); } } Traitʹهड़͞ΕͨϓϩύςΟΛͦͷ·· ར༻͢ΔͱΤϥʔ
  61. <?hh // strict namespace Acme; class Request implements RequestInterface {

    use MessageTrait; public function __construct( protected \stdClass $class ) {} protected function getClass(): string { return \strval($this->class); } } ࣮֬ʹΠϯελϯεΛ౉͢͜ͱ
  62. <?hh // strict namespace Acme\Domain\Model; abstract class Identifier<T> { public

    function __construct( private T $id ) {} <<__Rx>> public function id(): T { return $this->id; } <<__Rx>> public function equals(Identifier<T> $id): bool { return $this->id === $id->id(); } }
  63. <?hh // strict namespace Acme\Domain\Model; abstract class Identifier<T> { public

    function __construct( private T $id ) {} <<__Rx>> public function id(): T { return $this->id; } <<__Rx>> public function equals(Identifier<T> $id): bool { return $this->id === $id->id(); } } Ҿ਺΍໭ΓͰར༻͢ΔܕΛҰͭʹ
  64. <?hh // strict namespace Acme\Domain\Model; abstract class Identifier<T> { public

    function __construct( private T $id ) {} <<__Rx>> public function id(): T { return $this->id; } <<__Rx>> public function equals(Identifier<T> $id): bool { return $this->id === $id->id(); } } int int int int
  65. <?hh // strict namespace Acme\Domain\Model\Article; use namespace Acme\Domain\Model; final class

    ArticleId<T> extends Model\Identifier<T> { } ྫ͑͹ɺIdͱ͍ͬͯ΋
 intͱ͸ݶΒͳ͍
  66. namespace Acme\Domain\Model\Article\Entity; use type DateTime; use type Acme\Domain\Model\EntityInterface; use type

    Acme\Domain\Model\Article\ArticleId; use type Acme\Domain\Model\Article\Body; class Article<T> implements EntityInterface<T> { const int EXPIRE_EDIT_TIME = 120; public function __construct( private ArticleId<T> $id, private Body $body, private DateTime $createdAt = new DateTime() ) {} <<__Rx>> public function getID(): T { return $this->id->id(); } // লུ }
  67. namespace Acme\Domain\Model\Article\Entity; use type DateTime; use type Acme\Domain\Model\EntityInterface; use type

    Acme\Domain\Model\Article\ArticleId; use type Acme\Domain\Model\Article\Body; class Article<T> implements EntityInterface<T> { const int EXPIRE_EDIT_TIME = 120; public function __construct( private ArticleId<T> $id, private Body $body, private DateTime $createdAt = new DateTime() ) {} <<__Rx>> public function getID(): T { return $this->id->id(); } // লུ } ୯ҰͷܕͷΈ ޡͬͨܕ͕հೖͮ͠Β͍
 (TypecheckerͰݕ஌)
  68. interface ArticleRepositoryInterface<TId, T> { public function add(T $entity): void; public

    function remove(T $entity): void; public function findById(TId $id): T; public function latestArticles(DateTime $date): Map<TId, public function query( SpecificationInterface<T> $specification ): Map<TId, T>; public function size(): int; }
  69. interface ArticleRepositoryInterface<TId, T> { public function add(T $entity): void; public

    function remove(T $entity): void; public function findById(TId $id): T; public function latestArticles(DateTime $date): Map<TId, public function query( SpecificationInterface<T> $specification ): Map<TId, T>; public function size(): int; } TId : Identifier͸intಛఆͷܕ͔Ͳ͏͔͸ɻɻʁ T: EntityͰ͸͋Δ͕ɺ͜ͷϦϙδτϦͰѻ͏΋ͷ͸શͯಉ͡ܕ
  70. abstract class BaseRepository<TId, T as EntityInterface<TId>> implements ArticleRepositoryInterface<TId, T> {

    protected Map<TId, T> $collect = Map{}; public function add(T $article): void { $this->collect->add(Pair{$article->getID(), $article}); } // লུ <<__Rx>> public function findById(TId $id): T { if($this->collect->contains($id)) { return $this->collect->at($id); } throw new \RuntimeException('Not Found.'); } }
  71. abstract class BaseRepository<TId, T as EntityInterface<TId>> implements ArticleRepositoryInterface<TId, T> {

    protected Map<TId, T> $collect = Map{}; public function add(T $article): void { $this->collect->add(Pair{$article->getID(), $article}); } // লུ <<__Rx>> public function findById(TId $id): T { if($this->collect->contains($id)) { return $this->collect->at($id); } throw new \RuntimeException('Not Found.'); } } TId : Identifier͸intಛఆͷܕ͔Ͳ͏͔͸ɻɻʁ T: EntityInterfaceΛ࣮૷ͯ͠Δ͕ɺTIdࢦఆ͞Εͨ΋ͷ
  72. abstract class BaseRepository<TId, T as EntityInterface<TId>> implements ArticleRepositoryInterface<TId, T> {

    protected Map<TId, T> $collect = Map{}; public function add(T $article): void { $this->collect->add(Pair{$article->getID(), $article}); } // লུ <<__Rx>> public function findById(TId $id): T { if($this->collect->contains($id)) { return $this->collect->at($id); } throw new \RuntimeException('Not Found.'); } } ίϨΫγϣϯ͸ɺ<TId, T> ͷϚοϓʹ Ұ؏ੑ͕͋Γɺ͜ΕҎ֎ͷ΋ͷ͸ར༻͞Εͳ͍
  73. <?hh // strict namespace Acme\Infrastructure\Persistence\Map; use type DateTime; use type

    Acme\Domain\Model\Article\Entity\Article; class ArticleRepository extends BaseRepository<int, Article<int>> { <<__Rx>> public function latestArticles( DateTime $date ): Map<int, Article<int>> { return $this->collect->filter( $v ==> $v->createdAt() > $date ); } }
  74. <?hh // strict namespace Acme\Infrastructure\Persistence\Map; use type DateTime; use type

    Acme\Domain\Model\Article\Entity\Article; class ArticleRepository extends BaseRepository<int, Article<int>> { <<__Rx>> public function latestArticles( DateTime $date ): Map<int, Article<int>> { return $this->collect->filter( $v ==> $v->createdAt() > $date ); } } ͲΜͳܕΛར༻͢Δͷ͔ɺ۩৅ΫϥεͰ໌ه
  75. <?hh // strict namespace Acme\Infrastructure\Persistence\Map; use type DateTime; use type

    Acme\Domain\Model\Article\Entity\Article; class ArticleRepository extends BaseRepository<int, Article<int>> { <<__Rx>> public function latestArticles( DateTime $date ): Map<int, Article<int>> { return $this->collect->filter( $v ==> $v->createdAt() > $date ); } } ಛఆͷू໿ݶఆͷॲཧͳͲ͕͋Ε͹ɺ
 ΠϯλʔϑΣʔε௥Ճ΍ɺઐ༻ͷॲཧͷΈهड़
  76. <?hh // strict namespace Acme\Infrastructure\Persistence\Map; use type Acme\Domain\Model\Bookmark\Entity\Bookmark; class BookmarkRepository

    extends BaseRepository<string, Bookmark<string>> { } ྫ͑͹ར༻͢Δܕ͚ͩΛม͑Δ ू໿ͷૢ࡞ͱͯ͠͸͜Ε͚ͩͰ׬݁
 ৄࡉͳॲཧ͸࢓༷ύλʔϯͳͲʹ
  77. class LatestPostSpecification implements SpecificationInterface<Article<int>> { public function __construct( private DateTime

    $since ) {} <<__Rx>> public function isSatisfiedBy( Article<int> $article ): bool { return $article->createdAt() > $this->since; } } ͜Ε·Ͱͷྫͱಉ༷ʹ۩৅Ϋϥεʹ
 ࣮ࡍʹద༻ͤ͞ΔܕΛهड़
  78. class LatestPostSpecification implements SpecificationInterface<Article<int>> { public function __construct( private DateTime

    $since ) {} <<__Rx>> public function isSatisfiedBy( Article<int> $article ): bool { return $article->createdAt() > $this->since; } } ࢦఆ͞Εͨ೔࣌ΑΓ΋৽͍͠هࣄͳΒ
 ࢓༷͕ຬͨ͞Ε·͢Α
  79. <<__Rx>> public function query( SpecificationInterface<T> $specification ): Map<TId, T> {

    return $this->collect->filter( $v ==> $specification->isSatisfiedBy($v) ); } ࢓༷ɾϦϙδτϦͱ૊Έ߹ΘͤΔ
  80. use type Vendor\Path\ArticleRepositoryInterface as Repository; use type Vendor\Path\\ArticleSpecificationFactoryInterface as SpecificationFactory;

    final class LatestArticleFeed { public function __construct( private Repository<int, Article<int>> $repository, private SpecificationFactory<Article<int>> $specification ) { } ࢦఆͨ͠ܕΛར༻͢ΔΑ͏ʹద༻͞Εͨ ΠϯλʔϑΣʔεΛ࣮૷ͨ͠΋ͷΛར༻͍ͯͩ͘͠͞
  81. public function execute( FeedRequestTransfer $request ): vec<shape( 'id' => int,

    'content' => string, 'created_at' => \DateTime)> { $result = $this->repository->query( $this->specificationFactory ->createLatestPosts($request->getDateTime() )); if(C\count($result)) { return Vec\map($result, ($v) ==> { return shape( 'id' => $v->getID(), 'content' => $v->body()->content(), 'created_at' => $v->createdAt() ); }); } return vec[]; } ࢓༷Λຬͨ͢هࣄΦϒδΣΫτΛϦϙδτϦ͔Βऔಘ
  82. public function execute( FeedRequestTransfer $request ): vec<shape( 'id' => int,

    'content' => string, 'created_at' => \DateTime)> { $result = $this->repository->query( $this->specificationFactory ->createLatestPosts($request->getDateTime() )); if(C\count($result)) { return Vec\map($result, ($v) ==> { return shape( 'id' => $v->getID(), 'content' => $v->body()->content(), 'created_at' => $v->createdAt() ); }); } return vec[]; } Collectionૢ࡞
 ϦϙδτϦ͔Βऔಘ͞ΕͨΦϒδΣΫτΛҰׅૢ࡞ *͜͜Ͱ͸shapeͷΈͷvec഑ྻʹ
  83. $service = new Application\Service\LatestArticleFeed( new Map\ArticleRepository(), new Map\ArticleSpecificationFactory() ); $result

    = $service->execute( new Application\FeedRequestTransfer([ 'datetime' => new DateTime('-4 hours'), ]) ); ϑΥʔϜ΍APIͳͲ͔Β஋Λૹ৴
  84. final class FeedRequestTransfer { const type FeedRequest = shape( 'datetime'

    => DateTime ); public function __construct( private array<arraykey, mixed> $request ) {} public function getDateTime(): DateTime { $request = $this->request as this::FeedRequest; return $request['datetime']; } } Ұൠతͳ഑ྻΛड͚औΔ
  85. final class FeedRequestTransfer { const type FeedRequest = shape( 'datetime'

    => DateTime ); public function __construct( private array<arraykey, mixed> $request ) {} public function getDateTime(): DateTime { $request = $this->request as this::FeedRequest; return $request['datetime']; } } ShapeͰ഑ྻʹରͯ͠ ظ଴͢ΔϑΟʔϧυͱܕఆٛ
  86. final class FeedRequestTransfer { const type FeedRequest = shape( 'datetime'

    => DateTime ); public function __construct( private array<arraykey, mixed> $request ) {} public function getDateTime(): DateTime { $request = $this->request as this::FeedRequest; return $request['datetime']; } } ϑΟʔϧυͱܕݕࠪ OKͳΒͦͷ··഑ྻ NGͳΒType Error͕εϩʔ