Beispiel #1
0
 /**
  * Export the current value(s)
  *
  * note: when exporting from a collection (collection->getValues()), this
  * method is not called if `$this->exportHandler === Field::EXPORT_SKIP`
  * @param ?integer $exportHandler A handler to override the 
  * @return array<number|string>|number|string
  */
 public function exportValue($exportHandler = null)
 {
     if ($exportHandler === null) {
         $exportHandler = $this->exportHandler;
     }
     if ($exportHandler === self::EXPORT_NORMAL || $exportHandler === self::EXPORT_SKIP) {
         $values = $this->getValuesAsArray();
         return count($values) === 1 ? $values[0] : $values;
     } else {
         if ($exportHandler === self::EXPORT_ARRAY) {
             if ($this->value === null) {
                 return $this->defaultValue === null ? [] : $this->defaultValue;
             }
             return Arr::cast($this->value);
         } else {
             return call_user_func($exportHandler, $this->getValuesAsArray());
         }
     }
 }
Beispiel #2
0
 /**
  * Add values to the fields in the collection
  *
  * @param array.<string,mixed> $fieldValues
  */
 public function addValues(array $fieldValues)
 {
     foreach ($fieldValues as $fieldName => $values) {
         $values = Arr::cast($values);
         if (null === ($field = $this->getField($fieldName))) {
             $message = "unknown field";
             $len = count($values);
             if ($len !== 1) {
                 $message .= " (encountered {$len} values)";
             }
             $error = new Error($message);
             $error->setName($fieldName);
             $this->addError($error);
         } else {
             foreach ($values as $index => $value) {
                 $field->addValue($value, $index);
             }
         }
     }
 }