Created
June 13, 2016 21:43
-
-
Save dantleech/d04e918b27003d1a27a6c73f5118aaa0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| /* | |
| * This file is part of the PHPBench package | |
| * | |
| * (c) Daniel Leech <daniel@dantleech.com> | |
| * | |
| * For the full copyright and license information, please view the LICENSE | |
| * file that was distributed with this source code. | |
| */ | |
| namespace PhpBench\Model; | |
| /** | |
| * Represents the result of a single iteration executed by an executor. | |
| */ | |
| class ResultCollection implements \IteratorAggregate | |
| { | |
| private $results; | |
| /** | |
| * @param ResultInterface[] $results | |
| */ | |
| public function __construct(array $results = []) | |
| { | |
| foreach ($results as $result) { | |
| $this->addResult($result); | |
| } | |
| } | |
| /** | |
| * Add a result to the collection. | |
| * | |
| * Only one result per class is permitted. | |
| * | |
| * @param ResultInterface | |
| */ | |
| public function addResult(ResultInterface $result) | |
| { | |
| $class = get_class($result); | |
| if (isset($this->results[$class])) { | |
| throw new \InvalidArgumentException(sprintf( | |
| 'Result of class "%s" has already been set.', | |
| $class | |
| )); | |
| } | |
| $this->results[$class] = $result; | |
| } | |
| /** | |
| * Replace any result of the class of the given object with the given | |
| * object. | |
| * | |
| * @param ResultInterface $result | |
| */ | |
| public function replaceResult(ResultInterface $result) | |
| { | |
| $this->results[get_class($result)] = $result; | |
| } | |
| /** | |
| * Return true if there is a result for the given class name. | |
| * | |
| * @param string $class | |
| * | |
| * @return bool | |
| */ | |
| public function hasResult($class) | |
| { | |
| return isset($this->results[$class]); | |
| } | |
| /** | |
| * Return the result of the given class, throw an exception | |
| * if it does not exist. | |
| * | |
| * @param string $class | |
| * | |
| * @throws \RuntimeException | |
| * | |
| * @return ResultInterface | |
| */ | |
| public function getResult($class) | |
| { | |
| if (!isset($this->results[$class])) { | |
| throw new \RuntimeException(sprintf( | |
| 'Result of class "%s" has not been set', | |
| $class | |
| )); | |
| } | |
| return $this->results[$class]; | |
| } | |
| /** | |
| * Return the named metric for the given result class. | |
| * | |
| * @param string $class | |
| * @param string $metric | |
| * | |
| * @return mixed | |
| * | |
| * @throws \InvalidArgumentException | |
| */ | |
| public function getMetric($class, $metric) | |
| { | |
| $metrics = $this->getResult($class)->getMetrics(); | |
| if (!isset($metrics[$metric])) { | |
| throw new \InvalidArgumentException(sprintf( | |
| 'Unknown metric "%s" for result class "%s". Available metrics: "%s"', | |
| $metric, $class, implode('", "', array_keys($metrics)) | |
| )); | |
| } | |
| return $metrics[$metric]; | |
| } | |
| /** | |
| * Return the named metric or the default value if the *result class* has | |
| * not been set. | |
| * | |
| * If the metric does not exist but the class *does* exist then there is | |
| * clearly a problem and we should allow an error to be thrown. | |
| * | |
| * @param string $class | |
| * @param string $metric | |
| * @param mixed $default | |
| * | |
| * @return mixed | |
| */ | |
| public function getMetricOrDefault($class, $metric, $default = null) | |
| { | |
| if (false === $this->hasResult($class)) { | |
| return $default; | |
| } | |
| return $this->getMetric($class, $metric); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getIterator() | |
| { | |
| return new \ArrayIterator($this->results); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment