<?php
class Cesta {
private $itens;
public function __construct
(array $itens) { $this->itens = $itens;
}
public function show() {
echo "Minha cesta:\n";
foreach ($this->itens as $item) {
echo $item."\n";
}
}
}
class CestaBuilder {
private $itens = array(); static public function create() {
return new static;
}
public function adicionar($item) {
$this->itens[] = $item;
return $this;
}
public function build() {
return new Cesta($this->itens);
}
}
$minha_cesta = CestaBuilder::create()
->adicionar("Pão")
->adicionar("Queijo")
->adicionar("Mortadela")
->build();
$minha_cesta->show();
PD9waHAKCmNsYXNzIENlc3RhIHsKCXByaXZhdGUgJGl0ZW5zOwoJcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KGFycmF5ICRpdGVucykgewoJCSR0aGlzLT5pdGVucyA9ICRpdGVuczsKCX0KCXB1YmxpYyBmdW5jdGlvbiBzaG93KCkgewoJCWVjaG8gIk1pbmhhIGNlc3RhOlxuIjsKCQlmb3JlYWNoICgkdGhpcy0+aXRlbnMgYXMgJGl0ZW0pIHsKCQkJZWNobyAkaXRlbS4iXG4iOwoJCX0KCX0KfQoKY2xhc3MgQ2VzdGFCdWlsZGVyIHsKCXByaXZhdGUgJGl0ZW5zID0gYXJyYXkoKTsKCXN0YXRpYyBwdWJsaWMgZnVuY3Rpb24gY3JlYXRlKCkgewoJCXJldHVybiBuZXcgc3RhdGljOwoJfQoJcHVibGljIGZ1bmN0aW9uIGFkaWNpb25hcigkaXRlbSkgewoJCSR0aGlzLT5pdGVuc1tdID0gJGl0ZW07CgkJcmV0dXJuICR0aGlzOwoJfQoJcHVibGljIGZ1bmN0aW9uIGJ1aWxkKCkgewoJCXJldHVybiBuZXcgQ2VzdGEoJHRoaXMtPml0ZW5zKTsKCX0KfQoKJG1pbmhhX2Nlc3RhID0gQ2VzdGFCdWlsZGVyOjpjcmVhdGUoKQoJLT5hZGljaW9uYXIoIlDDo28iKQoJLT5hZGljaW9uYXIoIlF1ZWlqbyIpCgktPmFkaWNpb25hcigiTW9ydGFkZWxhIikKCS0+YnVpbGQoKTsKJG1pbmhhX2Nlc3RhLT5zaG93KCk7Cg==