| 1 | <?php |
||
| 8 | class ApiSerializer extends ArraySerializer |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Serialize a collection. |
||
| 12 | * |
||
| 13 | * @param string $resourceKey |
||
| 14 | * @param array $data |
||
| 15 | * @return array |
||
| 16 | */ |
||
| 17 | public function collection( $resourceKey, array $data ) |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Serialize an item. |
||
| 24 | * |
||
| 25 | * @param string $resourceKey |
||
| 26 | * @param array $data |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function item( $resourceKey, array $data ) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Serialize a null resource. |
||
| 38 | * |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | public function null() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Serialize the meta. |
||
| 51 | * |
||
| 52 | * @param array $meta |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | public function meta( array $meta ) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Indicates if includes should be side-loaded. |
||
| 62 | * |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | public function sideloadIncludes() |
||
| 69 | |||
| 70 | public function mergeIncludes( $transformedData, $includedData ) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Serialize the included data. |
||
| 83 | * |
||
| 84 | * @param ResourceInterface $resource |
||
| 85 | * @param array $data |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function includedData( ResourceInterface $resource, array $data ) |
||
| 93 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.