Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Mos\Dice;
- /**
- * Generating histogram data.
- */
- class Histogram
- {
- /**
- * @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());
- if ($min && $max) {
- for ($i = $min; $i <= $max; $i++) {
- if (array_key_exists($i, $dataHistogram)) {
- echo ' <br>' . "$i. " . str_repeat("*", $dataHistogram[$i]);
- } else {
- echo ' <br>' . "$i. ";
- }
- }
- } else {
- foreach ($dataHistogram as $key => $value) {
- echo ' <br>' . "$key. " . str_repeat("*", $value);
- }
- }
- }
- /**
- * Foreign interface binding method
- *
- * @return void
- */
- public function injectData(HistogramInterface $object)
- {
- $this->serie = $object->getHistogramSerie();
- $this->min = $object->getHistogramMin();
- $this->max = $object->getHistogramMax();
- }
- }
- /**
- * A dice which has the ability to present data to be used for creating
- * a histogram.
- */
- class DiceHistogram2 extends Dice implements HistogramInterface
- {
- use HistogramTrait2;
- /**
- * Get max value for the histogram.
- *
- * @return int with the max value.
- */
- public function getHistogramMax()
- {
- return count($this->arrayRand);
- }
- /**
- * Roll the dice, remember its value in the serie and return
- * its value.
- *
- * @return int the value of the rolled dice.
- */
- public function roll()
- {
- $this->serie[] = parent::roll();
- return $this->getLastRoll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement