The fact that it is not perfect is pretty irrelevant to the need for this kind of basic functionality. I'm going to assume iPhone here because I'd be surprised if Android doesn't have this capability.
Andoird does. I think they added it in Lolipop about 5 years ago (only allowing calls/notifications from contacts). You have to set up do not disturb and then go and make exceptions for contacts.