Last active
December 28, 2015 01:58
-
-
Save mikehaertl/e7a44203db883d1357f4 to your computer and use it in GitHub Desktop.
Revisions
-
mikehaertl revised this gist
Nov 7, 2014 . 1 changed file with 18 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,20 +18,26 @@ public function toArray(array $fields = [], array $expand = [], $recursive = tru $data['_embedded'] = []; } $data['_embedded'][$field] = $value; } elseif (is_array($value) && count($value)) { // If the first element is an object we assume a list of objects $first = current($value); if (is_object($first)) { if (!isset($data['_embedded'])) { $data['_embedded'] = []; } if (!isset($data['_embedded'][$field])) { $data['_embedded'][$field] = []; } foreach($value as $k => $v) { if ($recursive) { $v = ($v instanceof Arrayable) ? $v->toArray() : ArrayHelper::toArray($v); } $data['_embedded'][$field][$k] = $value; } } else { $data[$field] = $value; } } else { $data[$field] = $value; } } -
mikehaertl revised this gist
Nov 7, 2014 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,20 @@ public function toArray(array $fields = [], array $expand = [], $recursive = tru $data['_embedded'] = []; } $data['_embedded'][$field] = $value; } elseif (is_array($value)) { if (!isset($data['_embedded'])) { $data['_embedded'] = []; } if (!isset($data['_embedded'][$field])) { $data['_embedded'][$field] = []; } foreach($value as $k => $v) { if ($recursive) { $v = ($v instanceof Arrayable) ? $v->toArray() : ArrayHelper::toArray($v); } $data['_embedded'][$field][$k] = $value; } }else { $data[$field] = $value; } } -
mikehaertl revised this gist
Nov 6, 2014 . 2 changed files with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,8 @@ use yii\web\Link; use yii\web\Linkable; use yii\helpers\Url; // Use HAL trait use yii\rest\hal\ArrayableTrait; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ <?php // Only showing the modifications in this class // Removed the Link/Linkable 'use' on top trait ArrayableTrait { // ... -
mikehaertl revised this gist
Nov 6, 2014 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,6 @@ use yii\helpers\ArrayHelper; use yii\web\Link; use yii\web\Linkable; trait ArrayableTrait { -
mikehaertl revised this gist
Nov 6, 2014 . 2 changed files with 64 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ <?php namespace app\models; use Yii; use yii\web\Link; use yii\web\Linkable; use yii\helpers\Url; use yii\rest\hal\ArrayableTrait; class User extends ActiveRecord implements IdentityInterface, Linkable { use ArrayableTrait; public function extraFields() { return ['profile']; } public function getLinks() { return [ Link::REL_SELF => Url::to(['user/view', 'id' => $this->id], true), ]; } public function getProfile() { return $this->hasOne(Profile::className(), ['user_id' => 'id']); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ <?php use yii\helpers\ArrayHelper; use yii\web\Link; use yii\web\Linkable; use yii\base\ArrayableTrait as BaseArraybleTrait; trait ArrayableTrait { public function toArray(array $fields = [], array $expand = [], $recursive = true) { $data = []; foreach ($this->resolveFields($fields, $expand) as $field => $definition) { $value = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $field); if (is_object($value)) { if ($recursive) { $value = ($value instanceof Arrayable) ? $value->toArray() : ArrayHelper::toArray($value); } if (!isset($data['_embedded'])) { $data['_embedded'] = []; } $data['_embedded'][$field] = $value; } else { $data[$field] = $value; } } if ($this instanceof Linkable) { $data['_links'] = Link::serialize($this->getLinks()); } return $data; } } -
mikehaertl revised this gist
Nov 6, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ <?php // Only showing the modifications in this class trait ArrayableTrait { -
mikehaertl created this gist
Nov 6, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ // Only showing the modifications in this class trait ArrayableTrait { // ... public function toArray(array $fields = [], array $expand = [], $recursive = true) { $data = []; foreach ($this->resolveFields($fields, $expand) as $field => $definition) { $data[$field] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $field); } // Remove the Linkable stuff return $recursive ? ArrayHelper::toArray($data) : $data; } // Method required to be public public function resolveFields(array $fields, array $expand) {