<?php
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/

class Mentor
{

  //------------------------
  // MEMBER VARIABLES
  //------------------------

  //Mentor Attributes
  private $name;

  //Mentor Associations
  private $students;

  //------------------------
  // CONSTRUCTOR
  //------------------------

  public function __construct($aName, $allStudents)
  {
    $this->name = $aName;
    $this->students = array();
    $didAddStudents = $this->setStudents($allStudents);
    if (!$didAddStudents)
    {
      throw new Exception("Unable to create Mentor, must have at least 5 students. See https://manual.umple.org?RE002ViolationofAssociationMultiplicity.html");
    }
  }

  //------------------------
  // INTERFACE
  //------------------------

  public function setName($aName)
  {
    $wasSet = false;
    $this->name = $aName;
    $wasSet = true;
    return $wasSet;
  }

  public function getName()
  {
    return $this->name;
  }

  public function getStudent_index($index)
  {
    $aStudent = $this->students[$index];
    return $aStudent;
  }

  public function getStudents()
  {
    $newStudents = $this->students;
    return $newStudents;
  }

  public function numberOfStudents()
  {
    $number = count($this->students);
    return $number;
  }

  public function hasStudents()
  {
    $has = $this->numberOfStudents() > 0;
    return $has;
  }

  public function indexOfStudent($aStudent)
  {
    $wasFound = false;
    $index = 0;
    foreach($this->students as $student)
    {
      if ($student->equals($aStudent))
      {
        $wasFound = true;
        break;
      }
      $index += 1;
    }
    $index = $wasFound ? $index : -1;
    return $index;
  }

  public static function minimumNumberOfStudents()
  {
    return 5;
  }

  public function addStudent($aStudent)
  {
    $wasAdded = false;
    if ($this->indexOfStudent($aStudent) !== -1) { return false; }
    $existingMentor = $aStudent->getMentor();
    if ($existingMentor != null && $existingMentor->numberOfStudents() <= self::minimumNumberOfStudents())
    {
      return $wasAdded;
    }
    else if ($existingMentor != null)
    {
      unset($existingMentor->students[$existingMentor->indexOfStudent($aStudent)]);
      $existingMentor->students = array_values($existingMentor->students);
    }
    $this->students[] = $aStudent;
    $this->setMentor($aStudent,$this);
    $wasAdded = true;
    return $wasAdded;
  }

  public function removeStudent($aStudent)
  {
    $wasRemoved = false;
    if ($this->indexOfStudent($aStudent) != -1 && $this->numberOfStudents() > self::minimumNumberOfStudents())
    {
      unset($this->students[$this->indexOfStudent($aStudent)]);
      $this->students = array_values($this->students);
      $this->setMentor($aStudent,null);
      $wasRemoved = true;
    }
    return $wasRemoved;
  }

  public function setStudents($newStudents)
  {
    $wasSet = false;
    if (count($newStudents) < self::minimumNumberOfStudents())
    {
      return $wasSet;
    }

    $checkNewStudents = array();
    $mentorToNewStudents = array();
    foreach ($newStudents as $aStudent)
    {
      if (array_search($aStudent,$checkNewStudents) !== false)
      {
        return $wasSet;
      }
      else if ($aStudent->getMentor() != null && $this !== $aStudent->getMentor())
      {
        $existingMentor = $aStudent->getMentor();
        $existingSerializedMentor = serialize($existingMentor);
        if (!array_key_exists($existingSerializedMentor, $mentorToNewStudents))
        {
          $mentorToNewStudents[$existingSerializedMentor] = $existingMentor->numberOfStudents();
        }
        $nextCount = $mentorToNewStudents[$existingSerializedMentor] - 1;
        if ($nextCount < self::minimumnumberOfStudents())
        {
          return $wasSet;
        }
        $mentorToNewStudents[$existingSerializedMentor] = $nextCount;
      }
      $checkNewStudents[] = $aStudent;
    }

    foreach ($this->students as $orphan) 
    {
      $wasFound = false;
      foreach ($checkNewStudents as $fosterCare)
      {
        if ($orphan == $fosterCare)
        {
          $wasFound = true;
          break;
        }
      }
      
      if (!$wasFound)
      {
        $this->setMentor($orphan, null);
      }
    }

    $this->students = array();
    foreach ($newStudents as $aStudent)
    {
      if ($aStudent->getMentor() != null)
      {
        unset($aStudent->getMentor()->students[$aStudent->getMentor()->indexOfStudent($aStudent)]);
        $aStudent->getMentor()->students = array_values($aStudent->getMentor()->students);
      }
      $this->setMentor($aStudent, $this);
      $this->students[] = $aStudent;
    }
    $wasSet = true;
    return $wasSet;
  }

  private function setMentor($aStudent, $aMentor)
  {
    $aStudent->mentor = $aMentor;
    //$prop = new ReflectionProperty($aStudent, 'mentor');
    //$prop->setAccessible(true);
    //$prop->setValue($aStudent,$aMentor);
    //$prop->setAccessible(false);
  }

  public function addStudentAt($aStudent, $index)
  {  
    $wasAdded = false;
    if($this->addStudent($aStudent))
    {
      if($index < 0 ) { $index = 0; }
      if($index > $this->numberOfStudents()) { $index = $this->numberOfStudents() - 1; }
      array_splice($this->students, $this->indexOfStudent($aStudent), 1);
      array_splice($this->students, $index, 0, array($aStudent));
      $wasAdded = true;
    }
    return $wasAdded;
  }

  public function addOrMoveStudentAt($aStudent, $index)
  {
    $wasAdded = false;
    if($this->indexOfStudent($aStudent) !== -1)
    {
      if($index < 0 ) { $index = 0; }
      if($index > $this->numberOfStudents()) { $index = $this->numberOfStudents() - 1; }
      array_splice($this->students, $this->indexOfStudent($aStudent), 1);
      array_splice($this->students, $index, 0, array($aStudent));
      $wasAdded = true;
    } 
    else 
    {
      $wasAdded = $this->addStudentAt($aStudent, $index);
    }
    return $wasAdded;
  }

  public function equals($compareTo)
  {
    return $this == $compareTo;
  }

  public function delete()
  {
    foreach ($this->students as $aStudent)
    {
      $this->setMentor($aStudent,null);
    }
    $this->students = array();
  }

}
?>
<?php
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE 1.35.0.7523.c616a4dce modeling language!*/

class Student
{

  //------------------------
  // MEMBER VARIABLES
  //------------------------

  //Student Attributes
  private $number;

  //Student Associations
  public $mentor; //until PHP 5.3 (setAccessible)

  //------------------------
  // CONSTRUCTOR
  //------------------------

  public function __construct($aNumber)
  {
    $this->number = $aNumber;
  }

  //------------------------
  // INTERFACE
  //------------------------

  public function setNumber($aNumber)
  {
    $wasSet = false;
    $this->number = $aNumber;
    $wasSet = true;
    return $wasSet;
  }

  public function getNumber()
  {
    return $this->number;
  }

  public function getMentor()
  {
    return $this->mentor;
  }

  public function hasMentor()
  {
    $has = $this->mentor != null;
    return $has;
  }

  public function setMentor($aMentor)
  {
    //
    // This source of this source generation is association_SetOptionalOneToMandatoryMany.jet
    // This set file assumes the generation of a maximumNumberOfXXX method does not exist because 
    // it's not required (No upper bound)
    //   

    $wasSet = false;
    
    $existingMentor = $this->mentor;

    if ($existingMentor == null)
    {
      if ($aMentor != null)
      {
        if ($aMentor->addStudent($this))
        {
          $existingMentor = $aMentor;
          $wasSet = true;
        }
      }
    } 
    else if ($existingMentor != null)
    {
      if ($aMentor == null)
      {
        if ($existingMentor->minimumNumberOfStudents() < $existingMentor->numberOfStudents())
        {
          $existingMentor->removeStudent($this);
          $existingMentor = $aMentor;  // aMentor == null
          $wasSet = true;
        }
      } 
      else
      {
        if ($existingMentor->minimumNumberOfStudents() < $existingMentor->numberOfStudents())
        {
          $existingMentor->removeStudent($this);
          $aMentor->addStudent($this);
          $existingMentor = $aMentor;
          $wasSet = true;
        }
      }
    }
    if ($wasSet)
    {
      $mentor = $existingMentor;
    }
    return $wasSet;
  }

  public function equals($compareTo)
  {
    return $this == $compareTo;
  }

  public function delete()
  {
    if ($this->mentor != null)
    {
      if ($this->mentor->numberOfStudents() <= 5)
      {
        $this->mentor->delete();
      }
      else
      {
        $this->mentor->removeStudent($this);
      }
    }
  }

}
?>