repente

Untitled

Jun 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. <?php
  2.  
  3. namespace My\Dice;
  4.  
  5. /**
  6. * Generating histogram data.
  7. */
  8. class DiceHistogram
  9. {
  10. /**
  11. * @var array $serie The numbers stored in sequence.
  12. * @var int $min The lowest possible number.
  13. * @var int $max The highest possible number.
  14. */
  15. private $serie = [];
  16. private $min;
  17. private $max;
  18.  
  19.  
  20.  
  21. /**
  22. * Get the serie.
  23. *
  24. * @return array with the serie.
  25. */
  26. public function getSerie()
  27. {
  28. return $this->serie;
  29. }
  30.  
  31. /**
  32. * Get the min.
  33. *
  34. * @return array with the serie.
  35. */
  36. public function getMin()
  37. {
  38. return $this->min;
  39. }
  40.  
  41. /**
  42. * Get the max.
  43. *
  44. * @return array with the serie.
  45. */
  46. public function getMax()
  47. {
  48. return $this->max;
  49. }
  50.  
  51.  
  52. /**
  53. * Return a string with a textual representation of the histogram.
  54. *
  55. * @return string representing the histogram.
  56. */
  57. public function getAsText()
  58. {
  59. $min = $this->getMin();
  60. $max = $this->getMax();
  61. $dataHistogram = array_count_values($this->getSerie());
  62. $string = "";
  63. if ($min && $max) {
  64. for ($i = $min; $i <= $max; $i++) {
  65. if (array_key_exists($i, $dataHistogram))
  66. {
  67. $string .= ' <br>' . "$i. " . str_repeat("*", $dataHistogram[$i]);
  68. }
  69. else
  70. {
  71. $string .= ' <br>' . "$i. ";
  72. }
  73. }
  74. }
  75. else
  76. {
  77. foreach ($dataHistogram as $key => $value)
  78. {
  79. $string .= ' <br>' . "$key. " . str_repeat("*", $value);
  80. }
  81. }
  82. return $string;
  83. }
  84.  
  85. /**
  86. * Foreign interface binding method
  87. *
  88. * @param object $object interface
  89. *
  90. * @return void
  91. */
  92. public function injectData(DiceHistogramInterface $object)
  93. {
  94. $this->serie = $object->getHistogramSerie();
  95. $this->min = $object->getHistogramMin();
  96. $this->max = $object->getHistogramMax();
  97. }
  98. }
Add Comment
Please, Sign In to add comment