Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array
- (
- [aaa@mail.com] => ccc@mail.com
- [bbb@mail.com] => ddd@mail.com
- [ccc@mail.com] => aaa@mail.com
- [ddd@mail.com] => bbb@mail.com
- )
- <?php namespace ironwoodsphp_classes;
- final class PairSelector
- {
- private $assigned_pairs;
- private $emails;
- private $total_emails = 0;
- private $err_no_emails = 'No emails registered or less of two!';
- /**
- * Gets pairs of emails in an associative array
- *
- * @param array $arr_emails
- * @return array
- */
- public function getPairs(array $arr_emails)
- {
- $this->emails = $arr_emails;
- /**
- * Is necesary setted at least two emails to use this class
- *
- */
- if (! $this->searchForMinimalCondicion()) {
- die('ERR -> ' . $this->err_no_emails);
- }
- $total_emails = count($arr_emails);
- $this->total_emails = $total_emails;
- $i = 0;
- $this->assigned_pairs = array();
- while ($i < $total_emails) {
- $pos = $this->getPosition($i); //for email 2
- $email_1 = $arr_emails[$i]; //Take each email of array one time
- $email_2 = $arr_emails[$pos]; //Take each email of array one time
- $this->assigned_pairs[$email_1] = $email_2;
- $i++;
- }
- return $this->assigned_pairs;
- }
- /**
- * Gets a value from the array different to actual and unassigned before
- *
- * @param int $actual_pos
- * @return int
- */
- private function getPosition(int $actual_pos)
- {
- $pos = $actual_pos;
- $max = $this->total_emails - 1;
- while ($pos === $actual_pos || $this->isAssigned($pos)) {
- $pos = rand(0, $max);
- }
- return $pos;
- }
- /**
- * Searches an email between the destinataries already assigned
- *
- * @param int $pos
- * @return boolean
- */
- private function isAssigned(int $pos)
- {
- $email = $this->emails[$pos];
- foreach ($this->assigned_pairs as $destinatary) {
- if ($email === $destinatary) {
- return true;
- }
- }
- return false;
- }
- /**
- * Searches if the number of emails are at least two
- *
- * @return boolean
- */
- private function searchForMinimalCondicion()
- {
- return ($this->emails && (count($this->emails) > 1));
- }
- } //class
- <?php namespace ironwoodsphp_classestests;
- require '../pairselector.php';
- use ironwoodsphp_classesPairSelector;
- $arr_emails = [
- 'aaa@mail.com',
- 'bbb@mail.com',
- 'ccc@mail.com',
- 'ddd@mail.com',
- ];
- $ps = new PairSelector();
- $res = $ps->getPairs($arr_emails);
- echo '<pre>';
- print_r($res);
- echo '<hr>';
- $res = $ps->getPairs(['usu@kas.es']);
- print_r($res);
- <?php namespace ironwoodsphp_classestests;
- /*
- cd c:/xampp/htdocs/php__classes/tests
- phpunit PairSelectorTest.php --colors=always --repeat 10
- */
- use PHPUnitFrameworkTestCase;
- use ironwoodsphp_classesPairSelector;
- $_BASE_PATH = dirname( __FILE__, 2 ) . '/'; //Only PHP 7
- require $_BASE_PATH . 'pairselector.php';
- class PairSelectorTest extends TestCase
- {
- private $emails = [
- 'aaa@mail.com',
- 'bbb@mail.com',
- 'ccc@mail.com',
- 'ddd@mail.com',
- ];
- /**
- * @coversDefaultClass ironwoodsphp_classes
- * @covers PairSelector::getPairs
- */
- public function testIfReturnPairs()
- {
- $arr_pairs = $this->getPairs();
- self::assertTrue(is_array($arr_pairs)); //we have an array
- self::assertFalse(empty($arr_pairs)); //our array has elements
- return $arr_pairs;
- }
- /**
- * @coversDefaultClass ironwoodsphp_classes
- * @covers PairSelector::getPairs
- * @depends testIfReturnPairs
- */
- public function testIfReturnEqualNumberOfPairsThatEmailsInOrigin(
- Array $arr_pairs
- )
- {
- self::assertEquals(
- count($arr_pairs),
- count($this->emails)
- );
- return $arr_pairs;
- }
- /**
- * @coversDefaultClass ironwoodsphp_classes
- * @covers PairSelector::getPairs
- * @depends testIfReturnEqualNumberOfPairsThatEmailsInOrigin
- */
- public function testIfReturnAsKeysSameEmailsThatEmailsInOrigin(
- Array $arr_pairs
- )
- {
- $i = 0;
- foreach ($arr_pairs as $key => $value) {
- $email = $this->emails[$i];
- $msg = 'Email from pairs-key and origin-emails are different';
- self::assertEquals( $email, $key, $msg );
- $i++;
- }
- return $arr_pairs;
- }
- /**
- * @coversDefaultClass ironwoodsphp_classes
- * @covers PairSelector::getPairs
- * @depends testIfReturnAsKeysSameEmailsThatEmailsInOrigin
- */
- public function testIfKeysAndValuesAreDifferentsInArrayOfPairs(
- Array $arr_pairs
- )
- {
- foreach ($arr_pairs as $key => $value) {
- $msg = 'Email in key is the same that email in value';
- self::assertNotEquals($key, $value, $msg);
- }
- return $arr_pairs;
- }
- /**
- * @coversDefaultClass ironwoodsphp_classes
- * @covers PairSelector::getPairs
- * @depends testIfKeysAndValuesAreDifferentsInArrayOfPairs
- */
- public function testIfReturnArrayValuesAreAllDifferent(
- Array $arr_pairs
- )
- {
- $arr_pairs = array_unique($arr_pairs); //Filters duplicate elements
- $msg = 'Values in array of pairs have duplicate mails';
- self::assertEquals(
- count($this->emails),
- count($arr_pairs),
- $msg
- );
- return $arr_pairs;
- }
- /**
- * Runs "PairSelector->getPairs()" to get the pairs of emails
- *
- * @return array
- */
- private function getPairs()
- {
- $ps = new PairSelector();
- return $ps->getPairs($this->emails);
- }
- } //class
Add Comment
Please, Sign In to add comment