symfony从实体文件约束中获取mimeTypes

时间:2017-02-22 17:39:57

标签: file symfony constraints mime-types

在我的API控制器中,我需要从文件约束(断言)中获取允许的mimeTypes

use Symfony\Component\Validator\Constraints as Assert;

...

    /**
     * @Assert\File(
     *     maxSize="3M",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Vich\UploadableField(mapping="live_thumbnail_image", fileNameProperty="ThumbURI")
     *
     * @var File $Picture
     */
    protected $Picture;

我试过了

$validator = $this->get('validator');
/** @var ClassMetadata $meta */
$meta = $validator->getMetadataFor(ChannelMetadata::class);
$constraints = $meta->getPropertyMetadata('Picture');

然后我有一个PropertyMetadataInterface数组,依此类推。

是否有更简单的方法来获取此信息?

1 个答案:

答案 0 :(得分:0)

我用它来获取注释值。

    $annotation = $reader->getPropertyAnnotation('Picture', \Symfony\Component\Validator\Constraints\File::class);

$ reader是这个服务@annotation_reader