Guest User

Untitled

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