Refactor based on Java API 

This is a partial refactor of the Java API with interfaces cloned to PHP.