Author: Bernhard Schussek (bschussek@gmail.com)
 /**
  * {@inheritdoc}
  */
 public function reverseTransform($value)
 {
     if (!call_user_func($this->condition, $value)) {
         return $value;
     }
     return $this->baseTransformer->reverseTransform($value);
 }
 public function testReverseTransformWithStringValue()
 {
     $this->choiceLoader->expects($this->any())->method('loadChoicesForValues')->will($this->returnCallback(function () {
         return array('test');
     }));
     $this->assertSame('test', $this->transformer->reverseTransform('TEST'));
 }
 function it_reverse_transforms_using_configured_transformer(DataTransformerInterface $decoratedTransformer)
 {
     $decoratedTransformer->reverseTransform('abc')->willReturn('ABC');
     $decoratedTransformer->reverseTransform('cde')->willReturn('CDE');
     $decoratedTransformer->reverseTransform('fgh')->willReturn('FGH');
     $this->reverseTransform(new ArrayCollection(['abc', 'cde', 'fgh']))->shouldBeLike(new ArrayCollection(['ABC', 'CDE', 'FGH']));
 }
Example #4
0
 /**
  * (non-PHPdoc)
  * @see Symfony\Component\Form.AbstractType::buildForm()
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     if (null !== $options['class']) {
         $this->transformer->setClass($options['class']);
         $builder->addModelTransformer($this->transformer);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function reverseTransform($values)
 {
     $this->assertTransformationValueType($values, Collection::class);
     return $values->map(function ($value) {
         return $this->decoratedTransformer->reverseTransform($value);
     });
 }
Example #6
0
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $options['file_options']['mapped'] = false;
     $builder->add('file', 'file', $options['file_options'])->add('crop_data', 'hidden', array('mapped' => false, 'data' => '{}'));
     //$builder->addModelTransformer($this->transformer);
     $builder->addViewTransformer($this->transformer);
     $this->transformer->setPropertyPaths(array('mime' => $options['mime_mapping'], 'size' => $options['size_mapping'], 'name' => $options['name_mapping'], 'data' => $options['data_mapping']));
     $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use($builder, $options) {
         $data = $event->getData();
         $this->transformer->setCropData(json_decode($data['crop_data'], true));
         if ($data['file'] instanceof UploadedFile) {
             $this->transformer->setUploadedFile($data['file']);
         }
     });
 }
 public function preBind(FormEvent $event)
 {
     $form = $event->getForm();
     $propertyName = $form->getName();
     $obj = $form->getParent()->getData();
     //For oneToMany at SonataAdmin
     if (!$obj) {
         return;
     }
     $mapping = $this->mappingFactory->getMappingFromField($obj, $this->dataStorage->getReflectionClass($obj), $propertyName);
     if ($mapping) {
         /*        $form->add('file', 'file', array('required' => false))
                   ->add('delete', 'checkbox', array('required' => false));*/
         $this->transformer->setMapping($mapping, $mapping->getFileUploadPropertyName() == $propertyName ? FileDataTransformer::MODE_UPLOAD_FIELD : FileDataTransformer::MODE_FILEDATA_FIELD);
     }
 }
Example #8
0
 /**
  * @param Email  $email
  * @param string $importance
  */
 protected function processImportance(Email $email, $importance)
 {
     if ($email->getId()) {
         if ($email->getImportance() != $importance) {
             throw $this->createInvalidPropertyException('Importance', $this->emailImportanceTransformer->transform($email->getImportance()), $this->emailImportanceTransformer->transform($importance));
         }
     } else {
         $email->setImportance($importance);
     }
 }
 /**
  * Transforms a string (usernames) to a Collection of UserInterface
  *
  * @param string $usernames
  *
  * @throws UnexpectedTypeException
  * @throws TransformationFailedException
  * @return Collection $recipients
  */
 public function reverseTransform($usernames)
 {
     if (null === $usernames || '' === $usernames) {
         return null;
     }
     if (!is_string($usernames)) {
         throw new UnexpectedTypeException($usernames, 'string');
     }
     $recipients = new ArrayCollection();
     $recipientsNames = array_filter(explode(',', $usernames));
     foreach ($recipientsNames as $username) {
         $user = $this->userToUsernameTransformer->reverseTransform(trim($username));
         if (!$user instanceof UserInterface) {
             throw new TransformationFailedException(sprintf('User "%s" does not exists', $username));
         }
         $recipients->add($user);
     }
     return $recipients;
 }
Example #10
0
 /**
  * {@inheritdoc}
  */
 public function initialize(array $posts)
 {
     $this->setString($this->dataTransformer->reverseTransform($posts));
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $defaultUpdateList = array('referenceType:*', 'isContinuationOfExistingPolicy:1', 'landlordTitle:Other', 'hasPossibleClaimCircumstances:1', 'propertyLetType:Let Only', 'tenancyAgreementType:' . TenancyAgreementTypes::COMPANY_TENANCY_TYPE);
     if (array_key_exists('data', $options)) {
         $builder->add('updateList', 'hidden');
     } else {
         $builder->add('updateList', 'hidden', array('data' => implode(',', $defaultUpdateList)));
     }
     $this->referenceTypeTransformer->setCurrentAsn($options['currentAsn']);
     $builder->add('policyLength', 'choice', array('choices' => array(6 => '6 Months', 12 => '12 Months'), 'label' => 'Policy Term', 'expanded' => true, 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add($builder->create('referenceType', 'choice', array('choices' => ReferenceTypes::getReferenceTypes(), 'empty_value' => false, 'expanded' => true))->addModelTransformer($this->referenceTypeTransformer))->add('isNilExcess', 'choice', array('choices' => array(1 => 'Nil Excess', 0 => '1 Month Excess'), 'label' => 'Excess', 'empty_value' => false, 'expanded' => true, 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('isContinuationOfExistingPolicy', 'choice', array('choices' => array(1 => 'Yes', 0 => 'No'), 'label' => 'Will this policy be a continuation of cover from a previous policy?', 'empty_value' => false, 'expanded' => true, 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('propertyLetType', 'choice', array('choices' => PropertyLetTypes::getPropertyLetTypes(), 'empty_value' => '- Please Select -', 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('propertyAddress1', 'text', array('label' => 'Property house number + street', 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('propertyAddress2', 'text', array('label' => 'Property town/city', 'required' => false))->add('propertyPostcode', 'text', array('attr' => array('class' => 'first'), 'required' => true, 'constraints' => array(new Assert\NotBlank(array('message' => 'Please enter postcode', 'groups' => 'fullValidation')))))->add('propertyRental', 'money', array('label' => 'Monthly rental amount', 'required' => true, 'constraints' => array(new Assert\GreaterThan(array('value' => 0, 'message' => 'Please enter a rental amount greater than £0', 'groups' => 'fullValidation')))))->add('propertyDeposit', 'money', array('label' => 'Deposit amount', 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('hasPaidRentInAdvance', 'choice', array('choices' => array('1' => 'Yes', '0' => 'No'), 'label' => 'Has the first month’s rent been paid in advance?', 'empty_value' => false, 'expanded' => true, 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('tenancyStartAt', 'date', array('label' => 'Tenancy start date (dd/mm/yyyy)', 'widget' => 'single_text', 'format' => 'dd/MM/yyyy', 'attr' => array('data-provide' => 'datepicker'), 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('policyStartAt', 'date', array('label' => 'Policy start date (dd/mm/yyyy)', 'widget' => 'single_text', 'format' => 'dd/MM/yyyy', 'attr' => array('data-provide' => 'datepicker'), 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('policyEndAt', 'text', array('label' => 'Policy end date (dd/mm/yyyy)', 'attr' => array('readonly' => 'readonly'), 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation'))), 'mapped' => false))->add('landlordTitle', 'choice', array('label' => 'Title', 'attr' => array('class' => 'first'), 'choices' => Titles::getTitles(), 'empty_value' => false, 'required' => true))->add('landlordFirstName', 'text', array('label' => 'First name', 'attr' => array('class' => 'first'), 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('landlordLastName', 'text', array('label' => 'Last name', 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('landlordEmailAddress', 'repeated', array('type' => 'email', 'first_name' => 'emailAddress', 'second_name' => 'confirmEmailAddress', 'required' => false, 'invalid_message' => 'Email addresses do not match'))->add('landlordPhoneNumber', 'text', array('label' => 'Phone number', 'required' => false))->add('landlordAddress1', 'text', array('label' => 'Landlord house number + street', 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('landlordAddress2', 'text', array('label' => 'Landlord town/city', 'required' => true))->add('landlordPostcode', 'text', array('attr' => array('class' => 'first'), 'required' => true, 'constraints' => array(new Assert\NotBlank(array('message' => 'Please enter postcode', 'groups' => 'fullValidation')))))->add('landlordCountry', 'choice', array('choices' => Countries::getCountries(), 'data' => Countries::GB, 'required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('isPayMonthly', 'choice', array('label' => 'Payment type', 'required' => true, 'choices' => array(0 => 'Annually', 1 => 'Monthly'), 'empty_value' => false, 'expanded' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('hasPossibleClaimCircumstances', 'choice', array('required' => true, 'choices' => array(1 => 'Yes', 0 => 'No'), 'label' => 'Are you aware of any circumstances which may give rise to a claim?', 'empty_value' => false, 'expanded' => true))->add('hasPermittedOccupiersOnly', 'choice', array('required' => true, 'choices' => array(1 => 'Yes', 0 => 'No'), 'label' => 'Will only Tenants or permitted occupiers be living at the property?', 'empty_value' => false, 'expanded' => true))->add('hasTenancyDisputes', 'choice', array('required' => true, 'choices' => array(1 => 'Yes', 0 => 'No'), 'label' => 'Any tenancy disputes, including late payment of rent or rental arrears?', 'empty_value' => false, 'expanded' => true))->add('hasPriorClaims', 'choice', array('required' => true, 'choices' => array(1 => 'Yes', 0 => 'No'), 'label' => 'Have there been any claims logged during the existing tenancy?', 'empty_value' => false, 'expanded' => true))->add('isDepositSufficient', 'choice', array('required' => true, 'choices' => array(1 => 'Yes', 0 => 'No'), 'label' => 'Was a deposit with a sum equivalent to (or greater than) 1 months rent taken prior to the commencement of the tenancy?', 'empty_value' => false, 'expanded' => true))->add('tenancyAgreementType', 'choice', array('label' => 'Type of tenancy agreement', 'required' => true, 'choices' => TenancyAgreementTypes::getTenancyAgreementTypes(), 'empty_value' => false, 'expanded' => true))->add('confirmation1', 'checkbox', array('required' => true, 'value' => 1, 'label' => 'I confirm that the statements above are true to the best of my knowledge', 'mapped' => 0))->add('confirmation2', 'checkbox', array('required' => true, 'value' => 1, 'label' => 'I confirm that I have read both the IDD and policy summary', 'mapped' => 0))->add('update', 'submit', array('attr' => array('value' => '1')))->add('isXhr', 'hidden', array('attr' => array('value' => '0')))->add('policyNumber', 'hidden')->setMethod('GET');
     /**
      * Form modifier for optional fields
      *
      * @param FormEvent $event
      */
     $verifyReferenceSubscriber = $this->verifyReferenceSubscriber;
     $verifyReferenceSubscriber->setCurrentAsn($options['currentAsn']);
     $referenceNumberTransformer = $this->referenceNumberTransformer;
     $referenceNumberTransformer->setCurrentAsn($options['currentAsn']);
     $optionsFormModifier = function (FormEvent $event) use($verifyReferenceSubscriber, $referenceNumberTransformer) {
         $form = $event->getForm();
         $data = $event->getData();
         $updateList = array();
         if (null !== $data) {
             if (is_array($data)) {
                 /** @var RentRecoveryPlusApplication $application */
                 $application = RentRecoveryPlusApplication::hydrate($data);
             } else {
                 if ($data instanceof RentRecoveryPlusApplication) {
                     $application = $data;
                 } else {
                     return;
                 }
             }
             $updateData = false;
             $updateList[] = 'referenceType:*';
             if ($application->getReferenceType()) {
                 if (ReferenceTypes::isProviderRequired($application->getReferenceType())) {
                     $form->add('otherProvider', 'text', array('required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))));
                     if ($form->has('referenceNumber')) {
                         $form->remove('referenceNumber');
                     }
                     if (array_key_exists('referenceNumber', $data)) {
                         unset($data['referenceNumber']);
                         $updateData = true;
                     }
                 } else {
                     $updateList[] = 'referenceNumber:*';
                     $form->add('references', 'collection', array('type' => new EventAwareTextType(array('subscribers' => $verifyReferenceSubscriber, 'view_transformers' => $referenceNumberTransformer)), 'allow_add' => true, 'options' => array('label' => 'Reference number', 'required' => false)));
                     if ($form->has('otherProvider')) {
                         $form->remove('otherProvider');
                     }
                     if (array_key_exists('otherProvider', $data)) {
                         unset($data['otherProvider']);
                         $updateData = true;
                     }
                 }
             }
             if (ReferenceTypes::isNilExcessAllowed($application->getReferenceType())) {
                 $form->remove('isNilExcess');
                 $form->add('isNilExcess', 'choice', array('required' => true, 'choices' => array(1 => 'Nil Excess', 0 => '1 Month Excess'), 'label' => 'Excess', 'empty_value' => false, 'expanded' => true));
             } else {
                 $form->remove('isNilExcess');
                 $form->add('isNilExcess', 'choice', array('choices' => array(0 => '1 Month Excess'), 'label' => 'Excess', 'empty_value' => false, 'expanded' => true));
                 if ($application->getIsNilExcess()) {
                     $form->get('isNilExcess')->setData(0);
                     $data['isNilExcess'] = 0;
                     $application->setIsNilExcess(0);
                     $updateData = true;
                 }
             }
             /* Phase 2
                if (
                    $application->getIsNilExcess() &&
                    ! $application->getPropertyDeposit() &&
                    $application->getPropertyDeposit() <= 0.0
                ) {
                    $form->add('hasNilDepositInsurance', 'checkbox', array(
                        'required' => true,
                        'value' => 1,
                        'label' => 'please confirm that you have a nil deposit insurance or warranty in place'
                            . ' which extends to the equivalent of one month’s rent',
                    ));
                }
                else {
                    if ($form->has('hasNilDepositInsurance')) {
                        $form->remove('hasNilDepositInsurance');
                    }
                    if (array_key_exists('hasNilDepositInsurance', $data)) {
                        unset($data['hasNilDepositInsurance']);
                        $updateData = true;
                    }
                }
                if ($application->getHasPaidRentInAdvance()) {
                    $form->add('numberMonthsRentInAdvance', 'decimal', array(
                        'required' => true,
                        'constraints' => array(
                            new Assert\Length(array(
                                'min' => 1,
                                'groups' => 'fullValidation',
                            ))
                        ),
                    ));
                }
                else {
                    if ($form->has('numberMonthsRentInAdvance')) {
                        $form->remove('numberMonthsRentInAdvance');
                    }
                    if (array_key_exists('numberMonthsRentInAdvance', $data)) {
                        unset($data['numberMonthsRentInAdvance']);
                        $updateData = true;
                    }
                }
                */
             if ($application->getIsContinuationOfExistingPolicy()) {
                 $updateList[] = 'isContinuationOfExistingPolicy:0';
                 $form->add('existingPolicyRef', 'text', array('required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))))->add('isExistingPolicyToBeCancelled', 'choice', array('required' => true, 'choices' => array(1 => 'Yes', 0 => 'No'), 'label' => 'Do you wish us to cancel this RG policy?', 'empty_value' => false, 'expanded' => true));
                 if ($application->getIsExistingPolicyToBeCancelled() === null || $application->getIsExistingPolicyToBeCancelled()) {
                     $updateList[] = 'isExistingPolicyToBeCancelled:0';
                     if ($form->has('cancellationObjections')) {
                         $form->remove('cancellationObjections');
                     }
                     if (array_key_exists('cancellationObjections', $data)) {
                         unset($data['cancellationObjections']);
                         $updateData = true;
                     }
                 } else {
                     $updateList[] = 'isExistingPolicyToBeCancelled:1';
                     $form->add('cancellationObjections', 'textarea', array('required' => true));
                 }
             } else {
                 $updateList[] = 'isContinuationOfExistingPolicy:1';
                 if ($form->has('existingPolicyRef')) {
                     $form->remove('existingPolicyRef');
                 }
                 if (array_key_exists('existingPolicyRef', $data)) {
                     unset($data['existingPolicyRef']);
                     $updateData = true;
                 }
                 if ($form->has('isExistingPolicyToBeCancelled')) {
                     $form->remove('isExistingPolicyToBeCancelled');
                 }
                 if (array_key_exists('isExistingPolicyToBeCancelled', $data)) {
                     unset($data['isExistingPolicyToBeCancelled']);
                     $updateData = true;
                 }
                 if ($form->has('cancellationObjections')) {
                     $form->remove('cancellationObjections');
                 }
                 if (array_key_exists('cancellationObjections', $data)) {
                     unset($data['cancellationObjections']);
                     $updateData = true;
                 }
             }
             if ($application->getLandlordTitle() == 'Other') {
                 $updateList[] = 'landlordTitle:*';
                 $form->add('landlordOtherTitle', 'text', array('label' => 'Please specify title', 'required' => true, 'constraints' => array(new Assert\Length(array('min' => 2, 'groups' => 'fullValidation')))));
             } else {
                 $updateList[] = 'landlordTitle:Other';
                 if ($form->has('landlordOtherTitle')) {
                     $form->remove('landlordOtherTitle');
                 }
                 if (array_key_exists('landlordOther', $data)) {
                     unset($data['landlordOtherTitle']);
                     $updateData = true;
                 }
             }
             if ($application->getHasPossibleClaimCircumstances()) {
                 $updateList[] = 'hasPossibleClaimCircumstances:0';
                 $form->add('claimInfo', 'textarea', array('required' => true, 'constraints' => array(new Assert\NotBlank(array('groups' => 'fullValidation')))));
             } else {
                 $updateList[] = 'hasPossibleClaimCircumstances:1';
                 if ($form->has('claimInfo')) {
                     $form->remove('claimInfo');
                 }
                 if (array_key_exists('claimInfo', $data)) {
                     unset($data['claimInfo']);
                     $updateData = true;
                 }
             }
             if (PropertyLetTypes::isLandlordPermissionRequired($application->getPropertyLetType())) {
                 $updateList[] = 'propertyLetType:*';
                 $form->add('hasLandlordPermission', 'choice', array('required' => true, 'choices' => array(1 => 'Yes', 0 => 'No'), 'label' => 'Where a Let Only agreement is in place you may give your Landlord permission to report and progress a claim on your behalf. Please tick this box to confirm if you have granted this permission, and that your Landlord has been made aware of, and has confirmed understanding of, the policy conditions around claiming', 'empty_value' => false, 'expanded' => true));
             } else {
                 $updateList[] = 'propertyLetType:Let Only';
                 if ($form->has('hasLandlordPermission')) {
                     $form->remove('hasLandlordPermission');
                 }
                 if (array_key_exists('hasLandlordPermission', $data)) {
                     unset($data['hasLandlordPermission']);
                     $updateData = true;
                 }
             }
             if (!$application->getTenancyAgreementType()) {
                 $updateList[] = 'tenancyAgreementType:' . TenancyAgreementTypes::COMPANY_TENANCY_TYPE;
             } else {
                 if (TenancyAgreementTypes::isAssuredShortholdTenancy($application->getTenancyAgreementType())) {
                     $updateList[] = 'tenancyAgreementType:' . TenancyAgreementTypes::COMPANY_TENANCY_TYPE;
                 } else {
                     $updateList[] = 'tenancyAgreementType:' . TenancyAgreementTypes::ASSURED_SHORTHOLD_TENANCY_TYPE;
                 }
             }
             $updateListString = implode(',', $updateList);
             if ($application->getUpdateList() != $updateListString) {
                 $updateData = true;
                 if (is_array($data)) {
                     $data['updateList'] = $updateListString;
                 } else {
                     $application->setUpdateList($updateListString);
                 }
             }
             if ($updateData) {
                 if (is_array($data)) {
                     $event->setData($data);
                 } else {
                     $event->setData($application);
                 }
             }
         }
     };
     $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use($optionsFormModifier) {
         $optionsFormModifier($event);
     })->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($optionsFormModifier) {
         $optionsFormModifier($event);
     });
 }
 public function reverseTransform($value)
 {
     return $this->transformer->transform($value);
 }
 /**
  * @test
  * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
  */
 public function testReverseTransformInvalidChars()
 {
     $this->transformer->reverseTransform('@');
 }
 function it_creates_a_transformer(IdentifiableObjectRepositoryInterface $repository, DataTransformerInterface $dummyTransformer)
 {
     $fqcn = ClassUtils::getClass($dummyTransformer->getWrappedObject());
     $this->beConstructedWith($fqcn);
     $this->create($repository, ['multiple' => false])->shouldReturnAnInstanceOf($fqcn);
 }