private function getAssociatedPartner(Registration $current, array &$registrations) { $result = null; if (null !== $current->getPartner()) { // If the registration has a partner: done! if (($key = array_search($current->getPartner(), $registrations)) !== false) { $result = $current->getPartner(); // We withdraw the partner from the list of remaining registrations unset($registrations[$key]); // Force reassignment of the key from 0 to end of array $registrations = array_values($registrations); } } elseif ($registrations) { // Else if there are still some remaining registrations, // We try to find a partner to our current registration. // We pick the first one available. $temp = array_shift($registrations); if (null !== $temp->getPartner()) { // If this one has a partner, it cannot be the partner of our lonely registration. // So we try to find a partner among the remaining registrations. $result = $this->getAssociatedPartner($current, $registrations); array_unshift($registrations, $temp); } else { // If this registration has no partner, we match it with the current registration. $result = $temp; } } return $result; }
/** * Set partner * * @param \GS\FestivalBundle\Entity\Person $partner * * @return Registration */ public function setPartner(\GS\FestivalBundle\Entity\Registration $partner = null) { $this->partner = $partner; if (null === $partner->getPartner()) { $partner->setPartner($this); } return $this; }