title: How to normalize a given property using callback or any other method in Symfony 3?
body: <p>Let's say I have the following response coming from an API call:</p>
  
  <pre><code>[
    {
      "AgreementNumber": 20266,
      "StartDate": "2005-07-01T00:00:00",
      "EndDate": "2006-06-30T00:00:00",
      "AgreementTypeId": 1,
      "CfProgramLevelId": 2,
      "TamFlag": 0,
      "ParentAgreementNumber": null,
      "DistributorId": 16,
      "CustomerSiteId": 232,
      "RowId": 100,
      "Source": "Wonderware",
      "ActiveFlg": 1
    },
    ...
  ]
  </code></pre>
  
  <p>I am mapping that result to a <code>Agreement</code> entity as follow:</p>
  
  <pre><code>$propertyNameConverter = new PropertyNameConverter();
  $encoders              = [new XmlEncoder(), new JsonEncoder()];
  $normalizers           = [new ObjectNormalizer(null, $propertyNameConverter), new ArrayDenormalizer(),];
  $serializer            = new Serializer($normalizers, $encoders);
  
  $entities = $serializer-&gt;deserialize($data, "QuoteBundle\\Entity\\Agreement[]", 'json');
  </code></pre>
  
  <p>This is how the entity looks like:</p>
  
  <pre><code>class Agreement
  {
      /**
       * @var \DateTime
       * @ORM\Column(name="StartDate", type="string", nullable=false)
       */
      private $startDate;
  
      /**
       * @ORM\OneToOne(targetEntity="QuoteBundle\Entity\AgreementType")
       * @ORM\JoinColumn(name="AgreementTypeID", referencedColumnName="AgreementTypeID")
       */
      private $agreementType;
  
      /**
       * @param \DateTime $startDate
       */
      public function setStartDate(\DateTime $startDate)
      {
          $this-&gt;startDate = $startDate;
      }
  
      /**
       * @param AgreementType $agreementType
       */
      public function setAgreementType(AgreementType $agreementType)
      {
          $this-&gt;agreementType = $agreementType;
      }
  }
  </code></pre>
  
  <p>Each time I try to map the data I got the following error:</p>
  
  <blockquote>
    <p>Expected argument of type "DateTime", "string" given</p>
  </blockquote>
  
  <p>And it's fine since I am expecting a <code>DateTime</code> as the <code>startDate</code>. The solution here is to transform|normalize the data before map it to the entity.</p>
  
  <p>I have read <a href="http://symfony.com/doc/current/components/serializer.html#using-callbacks-to-serialize-properties-with-object-instances" rel="nofollow noreferrer">here</a> but I can't find how to apply this "callback" to a given property since the example is a simple use case and not a complex one.</p>
  
  <p>I have tried the following code:</p>
  
  <pre><code>$dateTimeNormalizer = new GetSetMethodNormalizer();
  $dateTimeCallback   = function ($dateTime) {
      return $dateTime instanceof \DateTime ? $dateTime-&gt;format(\DateTime::ATOM) : '';
  };
  
  $dateTimeNormalizer-&gt;setCallbacks(['startDate' =&gt; $dateTimeCallback, 'endDate' =&gt; $dateTimeCallback]);
  
  $normalizers = [
      new ObjectNormalizer(null, $propertyNameConverter),
      new ArrayDenormalizer(),
      $dateTimeNormalizer,
  ];
  
  $serializer = new Serializer($normalizers, $encoders);
  </code></pre>
  
  <p>But is not working since I am still getting the same error as above. I know there is a <a href="http://api.symfony.com/3.2/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.html" rel="nofollow noreferrer">DateTimeNormalizer</a> but adding it as a new entry on the <code>$normalizers</code> array doesn't make it to work. The result is the same meaning same error as above.</p>
  
  <p>Also I need a callback to normalize the property <code>AgreementTypeId</code> as <code>agreementType</code> since I am pretty sure it will fails as well. (In fact is not being mapped currently and I think the problem is because a name mismatch)</p>
  
  <p>Can any one help me to figure this out?</p>