protected function getOrCreateArrayDataType(phNameInfo $name, $keys, $currentKeyIndex, phFormViewElement $element, phArrayFormDataItem $currentDataItem) { $currentKey = $keys[$currentKeyIndex]->isAutoKey() ? $currentDataItem->getNextAutoKey() : $keys[$currentKeyIndex]->getKey(); if ($currentKeyIndex == sizeof($keys) - 1) { // last key if (isset($currentDataItem[$currentKey])) { $finalItem = $currentDataItem[$currentKey]; } else { $finalItem = $this->createNormalDataItem($currentKey, $element); $currentDataItem->registerArrayKey($keys[$currentKeyIndex], $finalItem); } return $finalItem; } else { if (isset($currentDataItem[$currentKey])) { $nextDataItem = $currentDataItem[$currentKey]; if (!$nextDataItem instanceof phArrayFormDataItem) { $path = $name->getName(); for ($x = 0; $x <= $currentKeyIndex; $x++) { $path .= "[{$keys[$currentKeyIndex]->getKey()}]"; } throw new phFormException("Unable to register array type data at {$path} is not an array data type"); } } else { $nextDataItem = $this->createArrayDataItem($keys[$currentKeyIndex + 1], $element, $keys[$currentKeyIndex]->getKey()); $currentDataItem->registerArrayKey($keys[$currentKeyIndex], $nextDataItem); } return $this->getOrCreateArrayDataType($name, $keys, ++$currentKeyIndex, $element, $nextDataItem); } }
/** * tests the auto key calcs properly */ public function testAutoKeys() { $testData = new phArrayFormDataItem('test'); $this->assertEquals($testData->getNextAutoKey(), 0, 'auto key before registering anything is 0'); $info = new phArrayKeyInfo('', true, phArrayKeyInfo::NUMERIC); $testData->registerArrayKey($info, new phFormDataItem('test')); $this->assertEquals($testData->getNextAutoKey(), 1, 'auto key after one register is 1'); $info = new phArrayKeyInfo('', true, phArrayKeyInfo::NUMERIC); $testData->registerArrayKey($info, new phFormDataItem('test')); $this->assertEquals($testData->getNextAutoKey(), 2, 'auto key after two register is 2'); }