Source of file KeyValueArrayFormData.php
Size: 1,781 Bytes - Last Modified: 2021-12-23T10:39:20+00:00
/var/www/docs.ssmods.com/process/src/src/Api/FormData/Parts/KeyValueArrayFormData.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 | <?php namespace Sunnysideup\BuildDataObject\Api\FormData\Parts; use SilverStripe\ORM\ArrayList; use SilverStripe\View\ArrayData; use Sunnysideup\BuildDataObject\Api\DBTypeConverter; class KeyValueArrayFormData implements InterfaceForFormDataParts { /** * @var int */ public const KEY = 0; /** * @var int */ public const VALUE = 1; private $array = []; public function setKeyAt($index, $key) { $this->initElementAt($index); $this->array[$index][self::KEY] = $key; } public function setValueAt($index, $value) { $this->initElementAt($index); $this->array[$index][self::VALUE] = $value; } public function getArray() { return $this->array; } public function toLiveObject() { $arrayList = ArrayList::create(); foreach ($this->array as $pair) { if (isset($pair[self::KEY], $pair[self::VALUE])) { $key = $pair[self::KEY]; $value = $pair[self::VALUE]; $valuePairArray = []; $useRawValue = false; if ('true' === $value || true === $value) { $useRawValue = true; } $valuePairArray = [ 'Key' => $key, 'Value' => DBTypeConverter::fromDropdown($value)->toDataObject(), 'RawValue' => $value, 'UseRawValue' => $useRawValue, ]; $arrayList->push(ArrayData::create($valuePairArray)); } } return $arrayList; } private function initElementAt($index) { if (! isset($this->array[$index])) { $this->array[$index] = []; } } } |