Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace My\Dice;
- /**
- * Generating histogram data.
- */
- class DiceHistogram
- {
- /**
- * @var array $serie The numbers stored in sequence.
- * @var int $min The lowest possible number.
- * @var int $max The highest possible number.
- */
- private $serie = [];
- private $min;
- private $max;
- /**
- * Get the serie.
- *
- * @return array with the serie.
- */
- public function getSerie()
- {
- return $this->serie;
- }
- /**
- * Get the min.
- *
- * @return array with the serie.
- */
- public function getMin()
- {
- return $this->min;
- }
- /**
- * Get the max.
- *
- * @return array with the serie.
- */
- public function getMax()
- {
- return $this->max;
- }
- /**
- * Return a string with a textual representation of the histogram.
- *
- * @return string representing the histogram.
- */
- public function getAsText()
- {
- $min = $this->getMin();
- $max = $this->getMax();
- $dataHistogram = array_count_values($this->getSerie());
- $string = "";
- if ($min && $max) {
- for ($i = $min; $i <= $max; $i++) {
- if (array_key_exists($i, $dataHistogram))
- {
- $string .= ' <br>' . "$i. " . str_repeat("*", $dataHistogram[$i]);
- }
- else
- {
- $string .= ' <br>' . "$i. ";
- }
- }
- }
- else
- {
- foreach ($dataHistogram as $key => $value)
- {
- $string .= ' <br>' . "$key. " . str_repeat("*", $value);
- }
- }
- return $string;
- }
- /**
- * Foreign interface binding method
- *
- * @param object $object interface
- *
- * @return void
- */
- public function injectData(DiceHistogramInterface $object)
- {
- $this->serie = $object->getHistogramSerie();
- $this->min = $object->getHistogramMin();
- $this->max = $object->getHistogramMax();
- }
- }
Add Comment
Please, Sign In to add comment