The microscopic mechanisms of the magnetoelectric effect in rare-earth phosphates