我正在构建一个REST API,我想让我的用户可以选择通过URL参数返回哪些字段,例如
/users?fields=username,email,address.city,address.country
有没有办法用JMSSerializerBundle实现这样的功能?
//编辑
请注意嵌入式收藏品
答案 0 :(得分:2)
我不认为这是JMSSerializer
的工作,至少不是完全的。相反,我会做的是:
// Do not serialize into JSON or XML, but to PHP array
$userSerialized = $jmsSerializer->toArray($user);
// Turn "username,email" into ['username' => 0, 'email' => 1, ... ]
$fields = array_flip(explode($request->query->get('fields')));
$userSerializedFiltered = array_intersect_key($userSerialized, $fields);
// Finally, put it into desired format, JSON for example:
$json = json_encode($userSerializedFiltered);
您可以使用Doctrine Partial objects:
$user = $em->createQuery("select partial u.{" . $fields . "} from MyApp\Domain\User u")->getResult();
$serialized = $jmsSerializer->serialize($user, 'json');
希望这会有所帮助......
答案 1 :(得分:1)
编辑:这个答案只涵盖了最初的问题,该问题没有要求更深层次的序列化。无论如何我会保留它,因为它可能会帮助其他人在最初的问题上挣扎。
我们以非常通用的方式完成了相同的工作。
我们将 ViewHandler 扩展为从当前的请求中读取,如果已将“字段”作为参数附加,并将 ExclusionStrategy 添加到的 SerializationContext 强>
值得注意的是,这种方法适用于FOS Rest Bundle 1.7.7(到目前为止我们没有迁移到最新的JMS版本),Symfony> 2.8和JMSSerializerBundle 1.1.0 - 但是将这种方法迁移到任何其他组合也不应该太难。
class ViewHandler extends \FOS\RestBundle\View\ViewHandler
{
/**
* Extends ViewHandler, adds the exclusion strategies FieldListExclusionStrategy to the SerializationContext.
*
* Reads Request Parameter "fields" (comma separated list) and parses it into an array. Does some clean-up on parameter
*/
protected function getSerializationContext(View $view)
{
$context = $view->getSerializationContext();
$request = $this->container->get('request_stack')->getCurrentRequest();
if ($request->isMethod('GET') && $request->query->has('fields')) {
$fieldList = explode(',', $request->query->get('fields'));
array_walk($fieldList, array(&$this, 'cleanString')); //clean special characters except - and _
$fieldList = array_filter($fieldList); // remove empty elements
$context->addExclusionStrategy(new FieldListExclusionStrategy($fieldList));
}
$view->setSerializationContext($context);
return parent::getSerializationContext($view);
}
/**
* Helper to remove special characters from String, Compatible with array_walk
*
* @param string $string -
* @param mixed $key -needed to be compatible with array_walk without raising a notice. (hands in 2 parameters)
*
* @return mixed
*/
private function cleanString(&$string, $key)
{
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-\_]/', '', $string); // Removes special chars.
return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}
}
这是FieldListExclusionStrategy类:
class FieldListExclusionStrategy implements ExclusionStrategyInterface
{
/**
* @var array
*/
private $fields = array();
/**
* @var int
*/
private $maxDepth;
/**
* FieldListExclusionStrategy constructor.
*
* @param array $fields
*/
public function __construct(array $fields)
{
$this->maxDepth = 1;
$this->fields = $fields;
}
/**
* Whether the class should be skipped.
*
* @param ClassMetadata $metadata
* @param Context $context
* @return boolean
*/
public function shouldSkipClass(ClassMetadata $metadata, Context $context)
{
return false;
}
/**
* Whether the property should be skipped.
*
* @param PropertyMetadata $property
* @param Context $context
*
* @return boolean
*/
public function shouldSkipProperty(PropertyMetadata $property, Context $context)
{
if (0 === count($this->fields)) {
return false;
}
if ($context->getDepth() > $this->maxDepth) {
return false;
}
$name = $property->serializedName ?: $property->name;
return !in_array($name, $this->fields, true);
}
}