Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     $config->setValueComparison($this->valueComparison);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_RANGE, true);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_COMPARISON, true);
     $config->addViewTransformer(new IntegerToLocalizedStringTransformer($options['precision'], $options['grouping'], $options['rounding_mode']));
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     $config->setValueComparison($this->valueComparison);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_RANGE, true);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_COMPARISON, true);
     $config->addViewTransformer(new MoneyToLocalizedStringTransformer($options['precision'], $options['grouping'], null, $options['divisor'], $options['default_currency']));
 }
Exemplo n.º 3
0
 private function removeDuplicatesInValuesBag(FieldConfigInterface $config, ValuesBag $valuesBag)
 {
     $comparison = $config->getValueComparison();
     $options = $config->getOptions();
     $this->removeDuplicateValues($valuesBag->getSimpleValues(), $valuesBag, $comparison, $options);
     $this->removeDuplicateValues($valuesBag->getExcludedSimpleValues(), $valuesBag, $comparison, $options, true);
     $this->removeDuplicateRanges($valuesBag->get(Range::class), $valuesBag, $comparison, $options);
     $this->removeDuplicateRanges($valuesBag->get(ExcludedRange::class), $valuesBag, $comparison, $options, true);
     $this->removeDuplicateComparisons($valuesBag, $comparison, $options);
     $this->removeDuplicateMatchers($valuesBag, $comparison, $options);
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     if (!$options['choice_list'] && !$options['choices'] instanceof \Traversable && !is_array($options['choices'])) {
         throw new InvalidConfigurationException('Either the option "choices" or "choice_list" must be set.');
     }
     if ($options['label_as_value']) {
         $config->addViewTransformer(new ChoiceToLabelTransformer($options['choice_list']));
     } else {
         $config->addViewTransformer(new ChoiceToValueTransformer($options['choice_list']));
     }
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     $config->setValueComparison($this->valueComparison);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_RANGE, true);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_COMPARISON, true);
     $dateFormat = is_int($options['format']) ? $options['format'] : self::DEFAULT_FORMAT;
     $timeFormat = \IntlDateFormatter::NONE;
     $calendar = \IntlDateFormatter::GREGORIAN;
     $format = is_string($options['format']) ? $options['format'] : null;
     if (!in_array($dateFormat, self::$acceptedFormats, true)) {
         throw new InvalidConfigurationException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) ' . 'or a string representing a custom format.');
     }
     if (null !== $format && (false === strpos($format, 'y') || false === strpos($format, 'M') || false === strpos($format, 'd'))) {
         throw new InvalidConfigurationException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $format));
     }
     $config->addViewTransformer(new DateTimeToLocalizedStringTransformer('UTC', 'UTC', $dateFormat, $timeFormat, $calendar, $format));
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     $format = 'H';
     if ($options['with_seconds'] && !$options['with_minutes']) {
         throw new InvalidConfigurationException('You can not disable minutes if you have enabled seconds.');
     }
     if ($options['with_minutes']) {
         $format .= ':i';
     }
     if ($options['with_seconds']) {
         $format .= ':s';
     }
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_RANGE, true);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_COMPARISON, true);
     $config->setValueComparison($this->valueComparison);
     $config->addViewTransformer(new DateTimeToStringTransformer('UTC', 'UTC', $format));
 }
Exemplo n.º 7
0
 /**
  * @param FieldConfigInterface $config
  * @param ValuesBag            $valuesBag
  */
 private function normalizeRangesInValuesBag(FieldConfigInterface $config, ValuesBag $valuesBag)
 {
     $comparison = $config->getValueComparison();
     $options = $config->getOptions();
     // Optimize the ranges before simple values, so we have less ranges to loop trough.
     // Each operation is run separate to reduce complexity and prevent hard to find bugs,
     // this results in less performance but better readability;
     // Results should be cached anyway.
     if ($valuesBag->has(Range::class)) {
         $this->removeOverlappingRanges($valuesBag->get(Range::class), $valuesBag, $comparison, $options);
         $this->optimizeConnectedRanges($valuesBag->get(Range::class), $valuesBag, $comparison, $options);
         $this->removeOverlappingSingleValues($valuesBag->getSimpleValues(), $valuesBag->get(Range::class), $valuesBag, $comparison, $options);
     }
     if ($valuesBag->has(ExcludedRange::class)) {
         $this->removeOverlappingRanges($valuesBag->get(ExcludedRange::class), $valuesBag, $comparison, $options);
         $this->optimizeConnectedRanges($valuesBag->get(ExcludedRange::class), $valuesBag, $comparison, $options, true);
         $this->removeOverlappingSingleValues($valuesBag->getExcludedSimpleValues(), $valuesBag->get(ExcludedRange::class), $valuesBag, $comparison, $options, true);
     }
 }
Exemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     $config->setValueComparison($this->valueComparison);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_RANGE, true);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_COMPARISON, true);
     $viewTransformers = $config->getViewTransformers();
     $config->resetViewTransformers();
     $config->addViewTransformer(new BirthdayTransformer($viewTransformers, $options['allow_age'], $options['allow_future_date']));
 }
Exemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     $config->setValueComparison($this->valueComparison);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_RANGE, true);
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_COMPARISON, true);
     if ($options['pattern']) {
         $options['format'] = $options['pattern'];
     }
     if (null === $options['format']) {
         if (!in_array($options['date_format'], self::$acceptedFormats, true)) {
             throw new InvalidConfigurationException('The "date_format" option must be one of the IntlDateFormatter constants ' . '(FULL, LONG, MEDIUM, SHORT) or the "format" must be a string representing a custom format.');
         }
         if (!in_array($options['time_format'], self::$acceptedFormats, true)) {
             throw new InvalidConfigurationException('The "time_format" option must be one of the IntlDateFormatter constants ' . '(FULL, LONG, MEDIUM, SHORT) or the "format" must be a string representing a custom format.');
         }
     }
     if (self::HTML5_FORMAT === $options['format']) {
         $config->addViewTransformer(new DateTimeToRfc3339Transformer($options['model_timezone'], $options['view_timezone']));
     } else {
         $config->addViewTransformer(new DateTimeToLocalizedStringTransformer($options['model_timezone'], $options['view_timezone'], $options['date_format'], $options['time_format'], \IntlDateFormatter::GREGORIAN, $options['format']));
     }
 }
Exemplo n.º 10
0
 /**
  * Converts a list of values to ranges.
  *
  * @param array                $values
  * @param ValuesBag            $valuesBag
  * @param FieldConfigInterface $config
  * @param bool                 $exclude
  */
 private function listToRanges($values, ValuesBag $valuesBag, FieldConfigInterface $config, $exclude = false)
 {
     $class = $exclude ? ExcludedRange::class : Range::class;
     /** @var ValueIncrementerInterface $comparison */
     $comparison = $config->getValueComparison();
     $options = $config->getOptions();
     $prevIndex = null;
     $prevValue = null;
     $rangeLower = null;
     $rangeUpper = null;
     $valuesCount = count($values);
     $curCount = 0;
     foreach ($values as $valIndex => $value) {
         ++$curCount;
         if (null === $prevValue) {
             $prevIndex = $valIndex;
             $prevValue = $value;
             continue;
         }
         $unsetIndex = null;
         $increasedValue = $comparison->getIncrementedValue($prevValue, $options);
         if ($comparison->isEqual($value, $increasedValue, $options)) {
             if (null === $rangeLower) {
                 $rangeLower = $prevValue;
             }
             $rangeUpper = $value;
         }
         if (null !== $rangeUpper) {
             $unsetIndex = $prevIndex;
             if ($curCount === $valuesCount || !$comparison->isEqual($value, $increasedValue, $options)) {
                 $range = new $class($rangeLower, $rangeUpper, true, true);
                 $valuesBag->add($range);
                 $unsetIndex = $prevIndex;
                 if ($curCount === $valuesCount) {
                     $unsetIndex = $valIndex;
                 }
                 $rangeLower = $rangeUpper = null;
             }
             $prevIndex = $valIndex;
             $prevValue = $value;
         }
         if (null !== $unsetIndex) {
             if ($exclude) {
                 $valuesBag->removeExcludedSimpleValue($unsetIndex);
             } else {
                 $valuesBag->removeSimpleValue($unsetIndex);
             }
         }
     }
 }
Exemplo n.º 11
0
 /**
  * Checks if the given field accepts the given value-type.
  *
  * @param FieldConfigInterface $fieldConfig
  * @param string               $type
  *
  * @throws UnsupportedValueTypeException
  *
  * @deprecated
  */
 protected function assertAcceptsType(FieldConfigInterface $fieldConfig, $type)
 {
     if (!$fieldConfig->supportValueType($type)) {
         throw new UnsupportedValueTypeException($fieldConfig->getName(), $type);
     }
 }
 /**
  * Returns the validation groups of the given field.
  *
  * @param FieldConfigInterface $field The field
  *
  * @return array The validation groups.
  */
 private static function getValidationGroups(FieldConfigInterface $field)
 {
     $groups = $field->getOption('validation_groups', [Constraint::DEFAULT_GROUP]);
     if (!is_string($groups) && is_callable($groups)) {
         $groups = call_user_func($groups, $field);
     }
     return (array) $groups;
 }
Exemplo n.º 13
0
 /**
  * Reverse transforms a value if a value transformer is set.
  *
  * @param string               $value  The value to reverse transform
  * @param FieldConfigInterface $config
  *
  * @return mixed Returns null when the value is empty or invalid
  */
 protected function viewToNorm($value, FieldConfigInterface $config)
 {
     $transformers = $config->getViewTransformers();
     if (!$transformers) {
         return '' === $value ? null : $value;
     }
     try {
         for ($i = count($transformers) - 1; $i >= 0; --$i) {
             $value = $transformers[$i]->reverseTransform($value);
         }
         return $value;
     } catch (TransformationFailedException $e) {
         return $e;
     }
 }
Exemplo n.º 14
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     $config->setValueComparison($this->valueComparison);
 }
Exemplo n.º 15
0
 /**
  * {@inheritdoc}
  */
 public function buildType(FieldConfigInterface $config, array $options)
 {
     $config->setValueTypeSupport(ValuesBag::VALUE_TYPE_PATTERN_MATCH, true);
 }
Exemplo n.º 16
0
 /**
  * Transforms the value if a value transformer is set.
  *
  * @param mixed                $value
  * @param FieldConfigInterface $field
  *
  * @return string
  */
 protected function normToView($value, FieldConfigInterface $field) : string
 {
     // Scalar values should be converted to strings to
     // facilitate differentiation between empty ("") and zero (0).
     if (null === $value || !$field->getViewTransformers()) {
         if (null !== $value && !is_scalar($value)) {
             throw new \RuntimeException(sprintf('Norm value of type %s is not a scalar value or null and not cannot be ' . 'converted to a string. You must set a viewTransformer for field "%s" with type "%s".', gettype($value), $field->getName(), $field->getType()->getName()));
         }
         return (string) $value;
     }
     foreach ($field->getViewTransformers() as $transformer) {
         $value = $transformer->transform($value);
     }
     return (string) $value;
 }
 private function resolveEntity($fieldName, FieldConfigInterface $fieldConfig)
 {
     if (isset($this->entityFieldMapping[$fieldName])) {
         $entityName = $this->entityFieldMapping[$fieldName]['entity'];
     } else {
         $entityName = $fieldConfig->getModelRefClass();
     }
     // Skip if the field is has no model-mapping.
     if (null === $entityName) {
         return;
     }
     $entityName = $this->entityManager->getClassMetadata($entityName)->getName();
     // Skip if the entity has no registered alias
     // and no alias is set for the field explicitly
     if (!isset($this->entityClassMapping[$entityName]) && !isset($this->entityFieldMapping[$fieldName])) {
         return;
     }
     return $entityName;
 }