随机足球比赛在PHP或JavaScript

I don't know how it happens in international football but in my country (Portugal) we have football matches divided by journeys. What I want to do is to make random football macthes between all the teams in a championship.

Example: (4 teams)

TeamA,TeamB,TeamC,TeamD

1st journey

  • TeamA vs TeamB
  • TeamC vs TeamD

2nd journey

  • TeamB vs TeamC
  • TeamD vs TeamA

3rd journey

  • TeamD vs TeamB
  • TeamC vs TeamA

//below games are easy, we just need to swap teams.

4th journey

  • TeamB vs TeamA
  • TeamD vs TeamC

5th journey

  • TeamC vs TeamB
  • TeamA vs TeamD

6th journey

  • TeamB vs TeamD
  • TeamA vs TeamC

Rules

  • In each championship, each team can only play with another once
  • After playing at home, the team becomes the visiting team in the next journey (with some exceptions because this isn't always possible)

I tried doing this with javascript and php but the result was not as expected. I search the web for a problem similar to this one, but no luck.

Can someone help me? At least with a theory?