/** * Ensures that each subarray in $currentFields corresponds to a JSON properties structure * definition defined in some SortableWidget subclass. * * @param array $expectedFields The array with key => default value pairs * @param array $currentFields The array to copy values from * @return array */ private function normalizeToWidgetJSONPropertiesStructures($expectedFields, $currentFields) { $fields = array(); foreach ($currentFields as $key => $val) { // widget class name can optionally be followed by a sequence of digits. This is // used for widget cloning $widgetClassName = preg_replace("/_\\w+\$/", '', $key); if (is_array($val) && isset($currentFields[$key]) && is_array($expectedFields[$widgetClassName])) { // JSON property structure definitions can be nested $fields[$key] = ArrayUtil::normalizeToArrayR($expectedFields[$widgetClassName], $currentFields[$key]); } } foreach ($expectedFields as $key => $val) { if (!isset($fields[$key])) { $fields[$key] = $expectedFields[$key]; } } return $fields; }
/** * JSON-decodes the value stored in the database column for the attribute, * and then normalizes it to the structure defined in {@link fields} * @param string $name The attribute to be unpacked * @return type */ public function unpackAttribute($name) { $fields = $this->fields($name); $attribute = CJSON::decode($this->getOwner()->{$name}); $attribute = is_array($attribute) ? ArrayUtil::normalizeToArrayR($fields, $attribute, $this->maintainCurrentFieldsOrder) : $fields; return $attribute; }