Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function bind(string $key, Data $data) : BindResult
 {
     if (!$data->hasKey($key)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.required'));
     }
     return BindResult::fromValue($data->getValue($key));
 }
 public function testBindResultFromFormErrorSequence()
 {
     $bindResult = BindResult::fromFormErrorSequence(new FormErrorSequence(new FormError('foo', 'bar')));
     $this->assertFalse($bindResult->isSuccess());
     FormErrorAssertion::assertErrorMessages($this, $bindResult->getFormErrorSequence(), ['foo' => 'bar']);
     $this->expectException(AssertionFailedException::class);
     $bindResult->getValue();
 }
 /**
  * {@inheritdoc}
  */
 public function bind(string $key, Data $data) : BindResult
 {
     switch ($data->getValue($key, 'false')) {
         case 'true':
             return BindResult::fromValue(true);
         case 'false':
             return BindResult::fromValue(false);
     }
     return BindResult::fromFormErrors(new FormError($key, 'error.boolean'));
 }
Exemple #4
0
 /**
  * {@inheritdoc}
  */
 public function bind(string $key, Data $data) : BindResult
 {
     if (!$data->hasKey($key)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.required'));
     }
     $dateTime = DateTimeImmutable::createFromFormat('!Y-m-d', $data->getValue($key), $this->timeZone);
     if (false === $dateTime) {
         return BindResult::fromFormErrors(new FormError($key, 'error.date'));
     }
     return BindResult::fromValue($dateTime);
 }
 /**
  * {@inheritdoc}
  */
 public function bind(string $key, Data $data) : BindResult
 {
     if (!$data->hasKey($key)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.required'));
     }
     $value = $data->getValue($key);
     if (!preg_match('(^-?[1-9]*\\d+$)', $value)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.integer'));
     }
     return BindResult::fromValue((int) $data->getValue($key));
 }
 /**
  * {@inheritdoc}
  */
 public function bind(string $key, Data $data) : BindResult
 {
     if (!$data->hasKey($key)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.required'));
     }
     $value = $data->getValue($key);
     if (!is_numeric($value)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.float'));
     }
     return BindResult::fromValue((double) $data->getValue($key));
 }
Exemple #7
0
 /**
  * {@inheritdoc}
  */
 public function bind(string $key, Data $data) : BindResult
 {
     if (!$data->hasKey($key)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.required'));
     }
     // Technically, seconds must always be present, according to the spec, but at least Chrome seems to ommit them.
     if (!preg_match('(^(?<hour>\\d{2}):(?<minute>\\d{2})(?::(?<second>\\d{2})(?:\\.(?<microsecond>\\d{1,6}))?)?$)', $data->getValue($key), $matches)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.time'));
     }
     return BindResult::fromValue(DateTimeImmutable::createFromFormat('!H:i:s.u', sprintf('%s:%s:%s.%s', $matches['hour'], $matches['minute'], $matches['second'] ?? '00', $matches['microsecond'] ?? '0'), $this->timeZone));
 }
 public function testBindAppliesConstraints()
 {
     $data = Data::fromFlatArray(['foo' => 'bar']);
     $binder = $this->prophesize(FormatterInterface::class);
     $binder->bind('foo', $data)->willReturn(BindResult::fromValue('bar'));
     $constraint = $this->prophesize(ConstraintInterface::class);
     $constraint->__invoke('bar')->willReturn(new ValidationResult(new ValidationError('bar')));
     $mapping = (new FieldMapping($binder->reveal()))->withPrefixAndRelativeKey('', 'foo')->verifying($constraint->reveal());
     $bindResult = $mapping->bind($data);
     $this->assertFalse($bindResult->isSuccess());
     $this->assertSame('bar', $bindResult->getFormErrorSequence()->getIterator()->current()->getMessage());
     $this->assertSame('foo', $bindResult->getFormErrorSequence()->getIterator()->current()->getKey());
 }
 public function testBindAppliesConstraintsToValidResult()
 {
     $data = Data::fromNestedArray(['foo' => ['bar' => ['baz']]]);
     $wrappedMapping = $this->prophesize(MappingInterface::class);
     $wrappedMapping->withPrefixAndRelativeKey('foo[bar]', '0')->willReturn($wrappedMapping->reveal());
     $wrappedMapping->bind($data)->willReturn(BindResult::fromValue('baz'));
     $constraint = $this->prophesize(ConstraintInterface::class);
     $constraint->__invoke(['baz'])->willReturn(new ValidationResult(new ValidationError('bar', [], '0')));
     $mapping = (new RepeatedMapping($wrappedMapping->reveal()))->withPrefixAndRelativeKey('foo', 'bar')->verifying($constraint->reveal());
     $bindResult = $mapping->bind($data);
     $this->assertFalse($bindResult->isSuccess());
     $this->assertSame('bar', $bindResult->getFormErrorSequence()->getIterator()->current()->getMessage());
     $this->assertSame('foo[bar][0]', $bindResult->getFormErrorSequence()->getIterator()->current()->getKey());
 }
 /**
  * {@inheritdoc}
  */
 public function bind(string $key, Data $data) : BindResult
 {
     if (!$data->hasKey($key)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.required'));
     }
     // Technically, seconds must always be present, according to the spec, but at least Chrome seems to ommit them.
     if (!preg_match('(^
             (?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})[Tt]
             (?<hour>\\d{2}):(?<minute>\\d{2})(?::(?<second>\\d{2})(?:\\.(?<microsecond>\\d{1,6}))?)?
             (?<timezone>[Zz]|[+-]\\d{2}:\\d{2})?
         $)x', $data->getValue($key), $matches)) {
         return BindResult::fromFormErrors(new FormError($key, 'error.date-time'));
     }
     return BindResult::fromValue(DateTimeImmutable::createFromFormat('!Y-m-d\\TH:i:s.u' . ($this->localTime ? '' : 'P'), sprintf('%s-%s-%sT%s:%s:%s.%s%s', $matches['year'], $matches['month'], $matches['day'], $matches['hour'], $matches['minute'], !empty($matches['second']) ? $matches['second'] : '00', !empty($matches['microsecond']) ? $matches['microsecond'] : '00', $matches['timezone'] ?? ''), $this->timeZone)->setTimezone($this->timeZone));
 }
 public function bind(Data $data) : BindResult
 {
     $values = [];
     $formErrorSequence = new FormErrorSequence();
     foreach ($data->getIndexes($this->key) as $index) {
         $bindResult = $this->wrappedMapping->withPrefixAndRelativeKey($this->key, $index)->bind($data);
         if (!$bindResult->isSuccess()) {
             $formErrorSequence = $formErrorSequence->merge($bindResult->getFormErrorSequence());
             continue;
         }
         $values[] = $bindResult->getValue();
     }
     if (!$formErrorSequence->isEmpty()) {
         return BindResult::fromFormErrorSequence($formErrorSequence);
     }
     return $this->applyConstraints($values, $this->key);
 }
Exemple #12
0
 public function bind(Data $data) : BindResult
 {
     $arguments = [];
     $formErrorSequence = new FormErrorSequence();
     foreach ($this->mappings as $key => $mapping) {
         $bindResult = $mapping->bind($data);
         if (!$bindResult->isSuccess()) {
             $formErrorSequence = $formErrorSequence->merge($bindResult->getFormErrorSequence());
             continue;
         }
         $arguments[$key] = $bindResult->getValue();
     }
     if (!$formErrorSequence->isEmpty()) {
         return BindResult::fromFormErrorSequence($formErrorSequence);
     }
     $apply = $this->apply;
     $value = $apply(...array_values($arguments));
     Assertion::isInstanceOf($value, $this->className);
     return $this->applyConstraints($value, $this->key);
 }
Exemple #13
0
 protected function applyConstraints($value, string $key) : BindResult
 {
     $validationResult = new ValidationResult();
     foreach ($this->constraints as $constraint) {
         $validationResult = $validationResult->merge($constraint($value));
     }
     if ($validationResult->isSuccess()) {
         return BindResult::fromValue($value);
     }
     return BindResult::fromFormErrors(...array_map(function (ValidationError $validationError) use($key) {
         if ('' === $key) {
             $finalKey = $validationError->getKeySuffix();
         } elseif ('' === $validationError->getKeySuffix()) {
             $finalKey = $key;
         } else {
             $finalKey = $key . preg_replace('(^[^\\[]+)', '[\\0]', $validationError->getKeySuffix());
         }
         return new FormError($finalKey, $validationError->getMessage(), $validationError->getArguments());
     }, iterator_to_array($validationResult->getValidationErrors())));
 }
 public function testConstraintIsAppliedToValueReturn()
 {
     $data = Data::fromFlatArray(['foo' => 'bar']);
     $constraint = $this->prophesize(ConstraintInterface::class);
     $constraint->__invoke('bar')->willReturn(new ValidationResult(new ValidationError('bar')));
     $wrappedMapping = $this->prophesize(MappingInterface::class);
     $wrappedMapping->bind($data)->willReturn(BindResult::fromValue('bar'));
     $wrappedMapping->withPrefixAndRelativeKey('', 'foo')->will(function () use($wrappedMapping) {
         return $wrappedMapping->reveal();
     });
     $mapping = (new OptionalMapping($wrappedMapping->reveal()))->verifying($constraint->reveal())->withPrefixAndRelativeKey('', 'foo');
     $bindResult = $mapping->bind($data);
     $this->assertFalse($bindResult->isSuccess());
     $this->assertSame('bar', $bindResult->getFormErrorSequence()->getIterator()->current()->getMessage());
 }
Exemple #15
0
 public function testTrimForBindFromRequestCanBeDisabled()
 {
     $request = $this->prophesize(ServerRequestInterface::class);
     $request->getMethod()->willReturn('GET');
     $request->getQueryParams()->willReturn(['foo' => ' bar ']);
     $mapping = $this->prophesize(MappingInterface::class);
     $mapping->bind(Argument::that(function (Data $data) {
         return $data->hasKey('foo') && ' bar ' === $data->getValue('foo');
     }))->willReturn(BindResult::fromValue(' bar '))->shouldBeCalled();
     (new Form($mapping->reveal()))->bindFromRequest($request->reveal(), false);
 }
 private function getMockedMapping(string $key, string $value = null, Data $data = null, $success = true) : MappingInterface
 {
     $mapping = $this->prophesize(MappingInterface::class);
     if (null !== $value) {
         $mapping->unbind($value)->willReturn(Data::fromFlatArray([$key => $value]));
     }
     if (null !== $value && null !== $data) {
         $mapping->bind($data)->willReturn($success ? BindResult::fromValue($value) : BindResult::fromFormErrors(new FormError($key, $value)));
     }
     $mapping->withPrefixAndRelativeKey('', $key)->willReturn($mapping->reveal());
     return $mapping->reveal();
 }