Improve Entity::toArray()

The parameter $reference now specify how references should be handled
when generating an array.