Unified ab initio formulation of flexoelectricity and strain-gradient elasticity