3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php namespace east\movie\library\finder; use east, east\movie, east\movie\library ; final class selector { private $library, $directorCriteria, $movieDirector, $titleCriteria, $movieTitle ; function __construct() { $this->library = new movie\library; } function libraryIs(movie\library $library) { $selector = clone $this; $selector->library = $library; return $selector; } function directorShouldBe(movie\director $director) { $selector = $this->libraryIs(new movie\library); $selector->directorCriteria = $director; return $selector; } function titleShouldBe(movie\title $title) { $selector = $this->libraryIs(new movie\library); $selector->titleCriteria = $title; return $selector; } function movieIs(movie $movie) { $selector = clone $this; $selector->movieDirector = null; $selector->movieTitle = null; $movie->selectorIs($selector); switch (true) { case $selector->directorCriteria && $selector->titleCriteria && $selector->movieDirector == $selector->directorCriteria && $selector->movieTitle == $selector->titleCriteria: case $selector->directorCriteria && ! $selector->titleCriteria && $selector->movieDirector == $selector->directorCriteria: case $selector->titleCriteria && ! $selector->directorCriteria && $selector->movieTitle == $selector->titleCriteria: $this->library->newMovie($movie); } return $this; } function movieDirectorIs(movie\director $director) { $this->movieDirector = $director; return $this; } function movieTitleIs(movie\title $title) { $this->movieTitle = $title; return $this; } function listerIs(movie\lister $lister) { $this->library->listerIs($lister); return $this; } } namespace east\movie\library; use east\movie ; final class finder { private $selector ; function __construct(finder\selector $selector = null) { $this->selector = $selector ?: new finder\selector; } function directorShouldBe(movie\director $director) { $finder = clone $this; $finder->selector = $this->selector->directorShouldBe($director); return $finder; } function titleShouldBe(movie\title $title) { $finder = clone $this; $finder->selector = $this->selector->titleShouldBe($title); return $finder; } function movieIs(movie $movie) { $this->selector->movieIs($movie); return $this; } function libraryIs(movie\library $library) { $finder = clone $this; $finder->selector = $this->selector->libraryIs(new movie\library); $library->finderIs($finder); return $finder; } function listerIs(movie\lister $lister) { $this->selector->listerIs($lister); return $this; } } namespace east\movie; use east ; final class library { private $movies ; function __construct() { $this->movies = []; } function newMovie(east\movie $movie) { $this->movies[] = $movie; return $this; } function finderIs(library\finder $finder) { foreach ($this->movies as $movie) { $finder->movieIs($movie); } return $this; } function listerIs(lister $lister) { foreach ($this->movies as $movie) { $movie->listerIs($lister); } return $this; } } final class director { private $value ; function __construct($value) { if (! is_string($value)) { throw new \domainException('Movie Director should be a string'); } $this->value = $value; } function __toString() { return $this->value; } } final class title { private $value ; function __construct($value) { if (! is_string($value)) { throw new \domainException('Movie title should be a string'); } $this->value = $value; } function __toString() { return $this->value; } } final class lister { function newMovie() { echo '-------------------------' . PHP_EOL; return $this; } function noMoreDataAboutMovie() { return $this; } function movieDirectorIs(director $director) { echo 'Director: ' . $director . PHP_EOL; return $this; } function movieTitleIs(title $title) { echo 'Title: ' . $title . PHP_EOL; return $this; } } namespace east; use east\movie ; class movie { private $director, $title ; function __construct(movie\director $director, movie\title $title) { $this->director = $director; $this->title = $title; } function selectorIs(movie\library\finder\selector $selector) { $selector ->movieDirectorIs($this->director) ->movieTitleIs($this->title) ; return $this; } function listerIs(movie\lister $lister) { $lister ->newMovie() ->movieTitleIs($this->title) ->movieDirectorIs($this->director) ->noMoreDataAboutMovie() ; return $this; } } var_dump(serialize((new movie\library\finder) ->directorShouldBe(new movie\Director('Ridley Scott')) ->libraryIs((new movie\library) ->newMovie(new movie(new movie\director('Ridley Scott'), new movie\title('Alien'))) ->newMovie(new movie(new movie\director('Christopher Nolan'), new movie\title('Interstellar'))) ->newMovie(new movie(new movie\director('Ridley Scott'), new movie\title('Gladiator'))) ) ->listerIs(new movie\lister))) ;
Output for 5.4.0 - 7.2.0
------------------------- Title: Alien Director: Ridley Scott ------------------------- Title: Gladiator Director: Ridley Scott string(1040) "O:25:"east\movie\library\finder":1:{s:35:"east\movie\library\finderselector";O:34:"east\movie\library\finder\selector":5:{s:43:"east\movie\library\finder\selectorlibrary";O:18:"east\movie\library":1:{s:26:"east\movie\librarymovies";a:2:{i:0;O:10:"east\movie":2:{s:20:"east\moviedirector";O:19:"east\movie\director":1:{s:26:"east\movie\directorvalue";s:12:"Ridley Scott";}s:17:"east\movietitle";O:16:"east\movie\title":1:{s:23:"east\movie\titlevalue";s:5:"Alien";}}i:1;O:10:"east\movie":2:{s:20:"east\moviedirector";O:19:"east\movie\director":1:{s:26:"east\movie\directorvalue";s:12:"Ridley Scott";}s:17:"east\movietitle";O:16:"east\movie\title":1:{s:23:"east\movie\titlevalue";s:9:"Gladiator";}}}}s:52:"east\movie\library\finder\selectordirectorCriteria";O:19:"east\movie\director":1:{s:26:"east\movie\directorvalue";s:12:"Ridley Scott";}s:49:"east\movie\library\finder\selectormovieDirector";N;s:49:"east\movie\library\finder\selectortitleCriteria";N;s:46:"east\movie\library\finder\selectormovieTitle";N;}}"