Guest User

Untitled

a guest
Dec 26th, 2017
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. Array
  2. (
  3. )
  4.  
  5. <?php namespace ironwoodsphp_classes;
  6.  
  7. final class PairSelector
  8. {
  9. private $assigned_pairs;
  10. private $emails;
  11. private $total_emails = 0;
  12.  
  13. private $err_no_emails = 'No emails registered or less of two!';
  14.  
  15. /**
  16. * Gets pairs of emails in an associative array
  17. *
  18. * @param array $arr_emails
  19. * @return array
  20. */
  21. public function getPairs(array $arr_emails)
  22. {
  23. $this->emails = $arr_emails;
  24.  
  25. /**
  26. * Is necesary setted at least two emails to use this class
  27. *
  28. */
  29. if (! $this->searchForMinimalCondicion()) {
  30. die('ERR -> ' . $this->err_no_emails);
  31. }
  32.  
  33. $total_emails = count($arr_emails);
  34. $this->total_emails = $total_emails;
  35.  
  36. $i = 0;
  37. $this->assigned_pairs = array();
  38. while ($i < $total_emails) {
  39.  
  40. $pos = $this->getPosition($i); //for email 2
  41.  
  42. $email_1 = $arr_emails[$i]; //Take each email of array one time
  43. $email_2 = $arr_emails[$pos]; //Take each email of array one time
  44. $this->assigned_pairs[$email_1] = $email_2;
  45.  
  46. $i++;
  47. }
  48.  
  49. return $this->assigned_pairs;
  50. }
  51.  
  52. /**
  53. * Gets a value from the array different to actual and unassigned before
  54. *
  55. * @param int $actual_pos
  56. * @return int
  57. */
  58. private function getPosition(int $actual_pos)
  59. {
  60. $pos = $actual_pos;
  61. $max = $this->total_emails - 1;
  62. while ($pos === $actual_pos || $this->isAssigned($pos)) {
  63.  
  64. $pos = rand(0, $max);
  65. }
  66.  
  67. return $pos;
  68. }
  69.  
  70. /**
  71. * Searches an email between the destinataries already assigned
  72. *
  73. * @param int $pos
  74. * @return boolean
  75. */
  76. private function isAssigned(int $pos)
  77. {
  78. $email = $this->emails[$pos];
  79. foreach ($this->assigned_pairs as $destinatary) {
  80.  
  81. if ($email === $destinatary) {
  82. return true;
  83. }
  84. }
  85.  
  86. return false;
  87. }
  88.  
  89. /**
  90. * Searches if the number of emails are at least two
  91. *
  92. * @return boolean
  93. */
  94. private function searchForMinimalCondicion()
  95. {
  96. return ($this->emails && (count($this->emails) > 1));
  97. }
  98. } //class
  99.  
  100. <?php namespace ironwoodsphp_classestests;
  101.  
  102. require '../pairselector.php';
  103. use ironwoodsphp_classesPairSelector;
  104.  
  105.  
  106. $arr_emails = [
  107. ];
  108.  
  109. $ps = new PairSelector();
  110. $res = $ps->getPairs($arr_emails);
  111.  
  112. echo '<pre>';
  113. print_r($res);
  114.  
  115. echo '<hr>';
  116.  
  117. $res = $ps->getPairs(['[email protected]']);
  118. print_r($res);
  119.  
  120. <?php namespace ironwoodsphp_classestests;
  121. /*
  122. cd c:/xampp/htdocs/php__classes/tests
  123. phpunit PairSelectorTest.php --colors=always --repeat 10
  124. */
  125. use PHPUnitFrameworkTestCase;
  126. use ironwoodsphp_classesPairSelector;
  127.  
  128. $_BASE_PATH = dirname( __FILE__, 2 ) . '/'; //Only PHP 7
  129. require $_BASE_PATH . 'pairselector.php';
  130.  
  131.  
  132. class PairSelectorTest extends TestCase
  133. {
  134. private $emails = [
  135. ];
  136.  
  137.  
  138. /**
  139. * @coversDefaultClass ironwoodsphp_classes
  140. * @covers PairSelector::getPairs
  141. */
  142. public function testIfReturnPairs()
  143. {
  144. $arr_pairs = $this->getPairs();
  145.  
  146. self::assertTrue(is_array($arr_pairs)); //we have an array
  147. self::assertFalse(empty($arr_pairs)); //our array has elements
  148.  
  149. return $arr_pairs;
  150. }
  151.  
  152. /**
  153. * @coversDefaultClass ironwoodsphp_classes
  154. * @covers PairSelector::getPairs
  155. * @depends testIfReturnPairs
  156. */
  157. public function testIfReturnEqualNumberOfPairsThatEmailsInOrigin(
  158. Array $arr_pairs
  159. )
  160. {
  161. self::assertEquals(
  162. count($arr_pairs),
  163. count($this->emails)
  164. );
  165.  
  166. return $arr_pairs;
  167. }
  168.  
  169. /**
  170. * @coversDefaultClass ironwoodsphp_classes
  171. * @covers PairSelector::getPairs
  172. * @depends testIfReturnEqualNumberOfPairsThatEmailsInOrigin
  173. */
  174. public function testIfReturnAsKeysSameEmailsThatEmailsInOrigin(
  175. Array $arr_pairs
  176. )
  177. {
  178. $i = 0;
  179. foreach ($arr_pairs as $key => $value) {
  180.  
  181. $email = $this->emails[$i];
  182. $msg = 'Email from pairs-key and origin-emails are different';
  183. self::assertEquals( $email, $key, $msg );
  184.  
  185. $i++;
  186. }
  187.  
  188. return $arr_pairs;
  189. }
  190.  
  191. /**
  192. * @coversDefaultClass ironwoodsphp_classes
  193. * @covers PairSelector::getPairs
  194. * @depends testIfReturnAsKeysSameEmailsThatEmailsInOrigin
  195. */
  196. public function testIfKeysAndValuesAreDifferentsInArrayOfPairs(
  197. Array $arr_pairs
  198. )
  199. {
  200. foreach ($arr_pairs as $key => $value) {
  201.  
  202. $msg = 'Email in key is the same that email in value';
  203. self::assertNotEquals($key, $value, $msg);
  204. }
  205.  
  206. return $arr_pairs;
  207. }
  208.  
  209. /**
  210. * @coversDefaultClass ironwoodsphp_classes
  211. * @covers PairSelector::getPairs
  212. * @depends testIfKeysAndValuesAreDifferentsInArrayOfPairs
  213. */
  214. public function testIfReturnArrayValuesAreAllDifferent(
  215. Array $arr_pairs
  216. )
  217. {
  218. $arr_pairs = array_unique($arr_pairs); //Filters duplicate elements
  219. $msg = 'Values in array of pairs have duplicate mails';
  220. self::assertEquals(
  221. count($this->emails),
  222. count($arr_pairs),
  223. $msg
  224. );
  225.  
  226. return $arr_pairs;
  227. }
  228.  
  229. /**
  230. * Runs "PairSelector->getPairs()" to get the pairs of emails
  231. *
  232. * @return array
  233. */
  234. private function getPairs()
  235. {
  236. $ps = new PairSelector();
  237.  
  238. return $ps->getPairs($this->emails);
  239. }
  240. } //class
Add Comment
Please, Sign In to add comment