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

The Language Of Collections

turanct
September 26, 2017

The Language Of Collections

turanct

September 26, 2017
Tweet

More Decks by turanct

Other Decks in Programming

Transcript

  1. you nd this in your codebase public function displayCoffeeRunsOpenForOrders() {

    $coffeeRuns = $this->coffeeRunsRepository->getAll(); $openForOrders = array(); foreach ($coffeeRuns as $coffeeRun) { if ($coffeeRun->ordersCanBeMade()) { $openForOrders[] = $coffeeRun; } } return $openForOrders; }
  2. we had this public function displayCoffeeRunsOpenForOrders() { $coffeeRuns = $this->coffeeRunsRepository->getAll();

    $openForOrders = array(); foreach ($coffeeRuns as $coffeeRun) { if ($coffeeRun->ordersCanBeMade()) { $openForOrders[] = $coffeeRun; } } return $openForOrders; }
  3. Repository implements Iterator public function displayCoffeeRunsOpenForOrders() { $coffeeRuns = $this->coffeeRunsRepository;

    $openForOrders = array(); foreach ($coffeeRuns as $coffeeRun) { if ($coffeeRun->ordersCanBeMade()) { $openForOrders[] = $coffeeRun; } } return $openForOrders; }
  4. dependency injection public function __construct( CoffeeRunsRepository $coffeeRuns ) { $this->coffeeRuns

    = $coffeeRuns; } foreach ($this->coffeeRuns as $coffeeRun) { // ... }
  5. interface CoffeeRunsRepository { /** * @return CoffeeRun[] A list of

    CoffeeRuns */ public function getAll(); } make Repostory interface extend Iterator interface? typehint on Iterator instead of the repository?
  6. we had this public function displayCoffeeRunsOpenForOrders() { $coffeeRuns = $this->coffeeRuns->getAll();

    $openForOrders = array(); foreach ($coffeeRuns as $coffeeRun) { if ($coffeeRun->ordersCanBeMade()) { $openForOrders[] = $coffeeRun; } } return $openForOrders; }
  7. interface changed interface CoffeeRunsRepository { // ... /** * @return

    CoffeeRun[] A list of CoffeeRuns open for orders */ public function openForOrders(); // ... }
  8. what if we want to have runs open for orders

    happening before noon? our interface will grow and grow and grow
  9. public function displayCoffeeRunsOpenForOrders() { $coffeeRuns = collect($this->coffeeRuns->getAll()); $openForOrders = $coffeeRuns->filter(

    function (CoffeeRun $run) { return $run->ordersCanBeMade(); } )->filter( function (CoffeeRun $run) { return $run->happensBeforeNoon(); } )->all(); return $openForOrders; } Eloquent always returns Collection s like this
  10. not so nice: foreach over array Repository as an Iterator

    speci c ltering methods on Repository jargon
  11. coffee runs -> that are ( open for orders )->

    and are ( happening before noon );
  12. interface CoffeeRuns { /** * @param callable function that takes

    a CoffeeRun * and returns a boolean * * @return CoffeeRuns that match the predicate */ public function thatAre(callable $matchingPredicate); /** * @return CoffeeRun[] An array of CoffeeRuns */ public function asArray(); }
  13. public function displayCoffeeRunsOpenForOrders() { $coffeeRuns = $this->coffeeRuns->getAll(); $runsOpenForOrders = $coffeeRuns

    ->thatAre($openForOrders); return $runsOpenForOrders->asArray(); } public function displayCoffeeRunsBeforeNoonOpenForOrders() { $coffeeRuns = $this->coffeeRuns->getAll(); $runsOpenForOrders = $coffeeRuns ->thatAre($happeningBeforeNoon) ->thatAre($openForOrders); return $runsOpenForOrders->asArray(); }
  14. interface CoffeeRuns { /** * @param Predicate * * @return

    CoffeeRuns that match the predicate */ public function thatAre(Predicate $matching); /** * @return CoffeeRun[] An array of CoffeeRuns */ public function asArray(); } we could have Predicate objects that can be translated to a MySQL statement, or anything else