2017-04-17 71 views
0

我想添加我自己的規範化器,因爲我需要將一些原始值轉換(反規格化)到它的相關實體。這是我做了什麼:ObjectNormalizer會覆蓋RelationshipNormalizer導致代碼崩潰,爲什麼?

namespace MMI\IntegrationBundle\Serializer\Normalizer; 

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; 
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 

class RelationshipNormalizer implements NormalizerInterface, DenormalizerInterface 
{ 
    public function normalize($object, $format = null, array $context = []) 
    { 
     // @TODO implement this method 
    } 

    public function supportsNormalization($data, $format = null): bool 
    { 
     return $data instanceof \AgreementType; 
    } 

    public function denormalize($data, $class, $format = null, array $context = []) 
    { 
     // @TODO implement this method 
    } 

    public function supportsDenormalization($data, $type, $format = null): bool 
    { 
     $supportedTypes = [ 
      \AgreementType::class => true 
     ]; 

     return isset($supportedTypes[$type]); 
    } 
} 

這是我如何使用它從控制器:

$propertyNameConverter = new PropertyNameConverter(); 
    $encoder    = new JsonEncoder(); 

    $normalizer = new ObjectNormalizer(
     null, 
     $propertyNameConverter, 
     null, 
     new ReflectionExtractor() 
    ); 

    $serializer = new Serializer([ 
     new DateTimeNormalizer(), 
     new RelationshipNormalizer(), 
     $normalizer, 
     new ArrayDenormalizer(), 
    ], [$encoder]); 

當代碼達到this method

private function getNormalizer($data, $format, array $context) 
{ 
    foreach ($this->normalizers as $normalizer) { 
     if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format, $context)) { 
      return $normalizer; 
     } 
    } 
} 

使用的Xdebug和IDE我可以看到條件$data instanceof \AgreementType是如何完成的,但是代碼再次嘗試檢查Normalizer,然後執行this function

public function supportsDenormalization($data, $type, $format = null) 
{ 
    return class_exists($type); 
} 

而這正是我得到錯誤的正規化引起以下錯誤:

Notice: Uninitialized string offset: 0 in vendor/symfony/symfony/src/Symfony/Component/Inflector/Inflector.php at line 179

UPDATE:

我曾經嘗試這樣做其他方式的結果是完全一樣的含義之前同樣的錯誤信息:

$callback = function ($value) { 
    $value = $this->em->getRepository('QuoteBundle:' . $this->table_mapping[$this->entity])->find($value); 

    return $value; 
}; 

$entityNormalizer = new GetSetMethodNormalizer(); 
$entityNormalizer->setCallbacks([ 
    'agreementType' => $callback, 
]); 

$serializer = new Serializer([ 
    new DateTimeNormalizer(), 
    $normalizer, 
    $entityNormalizer, 
    new ArrayDenormalizer(), 
], [$encoder]); 

我失蹤 這裏?

回答

0

在Slack的#symfony-devs頻道上獲得一些幫助後,我發現該命令很重要。這裏是我的問題的解決方案(比較下面的代碼段和OP上的代碼段,你會看到區別):

$normalizer = new ObjectNormalizer(
    null, 
    $propertyNameConverter, 
    null, 
    new ReflectionExtractor() 
); 

// Notice how ObjectNormalizer() is the last normalizer 
$serializer = new Serializer([ 
    new ArrayDenormalizer(), 
    new DateTimeNormalizer(), 
    new RelationshipNormalizer($em), 
    $normalizer, 
], [$encoder]);