Spain has passed legislation to protect thousands of unaccompanied African children arriving in the country.