Exemple #1
0
 /**
  * {@inheritDoc}
  */
 public function getInputFilterSpecification()
 {
     $inputFilterSpec = parent::getInputFilterSpecification();
     $spec = ['name' => 'startDate', 'allow_empty' => !!(null === $this->getOption('create_empty_option') ? $this->elements['startDate']->getOption('create_empty_option') : $this->getOption('create_empty_option')), 'required' => true, 'validators' => []];
     if (empty($inputFilterSpec['startDate'])) {
         $inputFilterSpec['startDate'] = $spec;
     } else {
         $inputFilterSpec['startDate'] = array_merge($spec, $inputFilterSpec['startDate']);
     }
     $spec = ['name' => 'endDate', 'allow_empty' => !!(null === $this->getOption('create_empty_option') ? $this->elements['startDate']->getOption('create_empty_option') : $this->getOption('create_empty_option')), 'required' => true, 'validators' => [['name' => 'Callback', 'options' => ['messages' => [Callback::INVALID_VALUE => 'The end date ' . 'must be later or equal to the start date'], 'callback' => function ($value, $context = []) {
         $filter = new DateSelectFilter(['null_on_empty' => true]);
         if ($startDate = $filter->filter($context['startDate'])) {
             return new DateTime($value) >= new DateTime($startDate);
         }
         return false;
     }, 'break_chain_on_failure' => true]]]];
     if (empty($inputFilterSpec['endDate'])) {
         $inputFilterSpec['endDate'] = $spec;
     } else {
         $inputFilterSpec['endDate'] = array_merge($spec, $inputFilterSpec['endDate']);
     }
     foreach ($this as $element) {
         $name = $element->getName();
         $minGetter = 'getMin' . ucfirst($name);
         $maxGetter = 'getMax' . ucfirst($name);
         if (!method_exists($this, $minGetter) || !method_exists($this, $maxGetter)) {
             continue;
         }
         if ($this->{$minGetter}()) {
             $inputFilterSpec[$name]['validators'][] = ['name' => 'GreaterThan', 'options' => ['messages' => [GreaterThan::NOT_GREATER_INCLUSIVE => 'The date ' . 'must be not earlier than %min% inclusive'], 'messageVariables' => ['min' => ['abstractOptions' => 'fmt']], 'min' => $this->{$minGetter}()->format('Y-m-d'), 'fmt' => $this->format($this->{$minGetter}()), 'inclusive' => true, 'break_chain_on_failure' => true]];
         }
         if ($this->{$maxGetter}()) {
             $inputFilterSpec[$name]['validators'][] = ['name' => 'LessThan', 'options' => ['messages' => [LessThan::NOT_LESS_INCLUSIVE => 'The date ' . 'must be not later than %max% inclusive'], 'messageVariables' => ['max' => ['abstractOptions' => 'fmt']], 'max' => $this->{$maxGetter}()->format('Y-m-d'), 'fmt' => $this->format($this->{$maxGetter}()), 'inclusive' => true, 'break_chain_on_failure' => true]];
         }
     }
     return $inputFilterSpec;
 }
Exemple #2
0
 /**
  * {@inheritDoc}
  */
 public function populateValues($data)
 {
     if ($data instanceof Traversable) {
         $data = ArrayUtils::iteratorToArray($data, true);
     }
     if (!is_array($data)) {
         throw new Exception\InvalidArgumentException(sprintf('%s expects an array or Traversable set of data; received "%s"', __METHOD__, is_object($data) ? get_class($data) : gettype($data)));
     }
     if (!isset($data['amount']) || !isset($data['currency'])) {
         throw new Exception\InvalidArgumentException(sprintf('%s expects an array with money amount and currency specified; received "%s"', __METHOD__, print_r($data, true)));
     }
     $money = new MoneyObject($data['amount'], $data['currency']);
     if (null === ($precision = $this->getOption('precision'))) {
         $precision = MoneyObject::PRECISION_CURRENCY;
     }
     $data['amount'] = $money->setPrecision($precision)->toUnits();
     return parent::populateValues($data);
 }