129 lines
3.6 KiB
PHP
129 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace admin\foundation;
|
|
|
|
use yii\base\Arrayable;
|
|
use yii\base\Component;
|
|
use yii\base\InvalidArgumentException;
|
|
use yii\base\Model;
|
|
use yii\data\DataProviderInterface;
|
|
use yii\data\Pagination;
|
|
use yii\helpers\ArrayHelper;
|
|
use yii\web\Response;
|
|
use Yii;
|
|
|
|
class Serializer extends Component
|
|
{
|
|
|
|
public $field = '';
|
|
public $listkey = 'list';
|
|
public $pagekey = 'pagination';
|
|
|
|
public function serializeData($data)
|
|
{
|
|
if (Yii::$app->response->format == Response::FORMAT_RAW) {
|
|
return $data;
|
|
}
|
|
if ($data instanceof Model && $data->hasErrors()) {
|
|
throw new InvalidArgumentException('参数错误。' . implode('', $data->getFirstErrors()));
|
|
// $data = $this->serializeModelErrors($data);
|
|
} elseif ($data instanceof Arrayable) {
|
|
$data = $this->serializeModel($data);
|
|
} elseif ($data instanceof DataProviderInterface) {
|
|
$data = $this->serializeDataProvider($data);
|
|
} elseif ($data === null) {
|
|
$data = [];
|
|
}
|
|
|
|
return $data;
|
|
// return array_merge($this->extend_result,$data);
|
|
}
|
|
|
|
/**
|
|
* Serializes the validation errors in a model.
|
|
* @param Model $model
|
|
* @return array the array representation of the errors
|
|
*/
|
|
protected function serializeModelErrors($model)
|
|
{
|
|
$result = [];
|
|
foreach ($model->getFirstErrors() as $name => $message) {
|
|
$result[] = [
|
|
'field' => $name,
|
|
'message' => $message,
|
|
];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Serializes a data provider.
|
|
* @param DataProviderInterface $dataProvider
|
|
* @return array the array representation of the data provider.
|
|
*/
|
|
protected function serializeDataProvider($dataProvider)
|
|
{
|
|
if (!empty($this->field)) {
|
|
$models = ArrayHelper::toArray($dataProvider->getModels(), $this->field);
|
|
} else {
|
|
$models = array_values($dataProvider->getModels());
|
|
$models = $this->serializeModels($models);
|
|
}
|
|
$pagination = $dataProvider->getPagination();
|
|
$result = [
|
|
$this->listkey => $models,
|
|
];
|
|
if ($pagination !== false) {
|
|
return array_merge($result, $this->serializePagination($pagination));
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Serializes a set of models.
|
|
* @param array $models
|
|
* @return array the array representation of the models
|
|
*/
|
|
protected function serializeModels(array $models)
|
|
{
|
|
foreach ($models as $i => $model) {
|
|
if ($model instanceof Arrayable) {
|
|
$models[$i] = $model->toArray();
|
|
} elseif (is_array($model)) {
|
|
$models[$i] = ArrayHelper::toArray($model);
|
|
}
|
|
}
|
|
|
|
return $models;
|
|
}
|
|
/**
|
|
* Serializes a model object.
|
|
* @param Arrayable $model
|
|
* @return array the array representation of the model
|
|
*/
|
|
protected function serializeModel($model)
|
|
{
|
|
return $model->toArray();
|
|
}
|
|
|
|
/**
|
|
* Serializes a pagination into an array.
|
|
* @param Pagination $pagination
|
|
* @return array the array representation of the pagination
|
|
* @see addPaginationHeaders()
|
|
*/
|
|
protected function serializePagination($pagination)
|
|
{
|
|
return [
|
|
$this->pagekey => [
|
|
'total_count' => $pagination->totalCount,
|
|
'page_count' => $pagination->getPageCount(),
|
|
'current_page' => $pagination->getPage() + 1,
|
|
'per_page' => $pagination->getPageSize(),
|
|
],
|
|
];
|
|
}
|
|
}
|