Created
October 19, 2014 15:49
-
-
Save theshadow/b45d18d0d339d91b22ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Result | |
{ | |
public function fetch() | |
{ | |
static $rows = [ | |
['id' => 1, 'name' => 'one', 'value' => 1.0], | |
['id' => 2, 'name' => 'two', 'value' => 2.0], | |
['id' => 3, 'name' => 'three', 'value'=> 3.00], | |
]; | |
$row = array_pop($rows); | |
return $row; | |
} | |
public function close() { } | |
} | |
class RowModel | |
{ | |
protected $id; | |
protected $name; | |
protected $value; | |
/** | |
* @return mixed | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @param mixed $id | |
* @return static | |
*/ | |
public function setId($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* @param mixed $name | |
* @return static | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getValue() | |
{ | |
return $this->value; | |
} | |
/** | |
* @param mixed $value | |
* @return static | |
*/ | |
public function setValue($value) | |
{ | |
$this->value = $value; | |
return $this; | |
} | |
} | |
class RowHydrator | |
{ | |
public function hydrate($object, $data) | |
{ | |
/** @var RowModel $model */ | |
$model = clone $object; | |
$model->setId($data['id']) | |
->setName($data['name']) | |
->setValue($data['value']); | |
return $model; | |
} | |
} | |
$result = new Result(); | |
$hydrator = new RowHydrator(); | |
$collection = function () use ($result, $hydrator) { | |
while ($row = $result->fetch()) { | |
yield $row; | |
} | |
$result->close(); | |
}; | |
// never gets executed. | |
foreach ($collection as $model) { | |
echo $model->getName(), ': ', $model->getValue(), PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment