コード例 #1
0
 /**
  * Serializes a model to an array.
  *
  * @param Model $model model to be serialized
  *
  * @return array properties
  */
 public function toArray(Model $model)
 {
     // start with the base representation of the model
     $result = $model->toArray();
     // apply namespacing to excluded properties
     $namedExc = [];
     foreach ($this->exclude as $k) {
         array_set($namedExc, $k, true);
     }
     // apply namespacing to included properties
     $namedInc = [];
     foreach ($this->include as $k) {
         array_set($namedInc, $k, true);
     }
     // apply namespacing to expanded properties
     $namedExp = [];
     foreach ($this->expand as $k) {
         array_set($namedExp, $k, true);
     }
     // remove excluded properties
     foreach (array_keys($result) as $k) {
         if (isset($namedExc[$k]) && !is_array($namedExc[$k])) {
             unset($result[$k]);
         }
     }
     // add included properties
     foreach (array_keys($namedInc) as $k) {
         if (!isset($result[$k]) && isset($namedInc[$k])) {
             $result[$k] = $model->{$k};
         }
     }
     // expand any relational model properties
     $result = $this->expand($model, $result, $namedExc, $namedInc, $namedExp);
     // apply hooks, if available
     if (method_exists($model, 'toArrayHook')) {
         $model->toArrayHook($result, $namedExc, $namedInc, $namedExp);
     }
     // order the properties array by name for consistency
     // since it is constructed in a random order
     ksort($result);
     return $result;
 }