/** * Converts an object into an INI formatted string * - Unfortunately, there is no way to have ini values nested further than two * levels deep. Therefore we will only go through the first two levels of * the object. * * @param object $object Data source object. * @param array $options Options used by the formatter. * * @return string INI formatted string. * * @since 1.0 */ public function objectToString($object, $options = array()) { $options = array_merge(self::$options, $options); $local = array(); $global = array(); $variables = get_object_vars($object); $last = count($variables); // Assume that the first element is in section $in_section = true; // Iterate over the object to set the properties. foreach ($variables as $key => $value) { // If the value is an object then we need to put it in a local section. if (is_object($value)) { // Add an empty line if previous string wasn't in a section if (!$in_section) { $local[] = ''; } // Add the section line. $local[] = '[' . $key . ']'; // Add the properties for this section. foreach (get_object_vars($value) as $k => $v) { if (is_array($v) && $options['supportArrayValues']) { $assoc = ArrayHelper::isAssociative($v); foreach ($v as $array_key => $item) { $array_key = $assoc ? $array_key : ''; $local[] = $k . '[' . $array_key . ']=' . $this->getValueAsINI($item); } } else { $local[] = $k . '=' . $this->getValueAsINI($v); } } // Add empty line after section if it is not the last one if (0 != --$last) { $local[] = ''; } } elseif (is_array($value) && $options['supportArrayValues']) { $assoc = ArrayHelper::isAssociative($value); foreach ($value as $array_key => $item) { $array_key = $assoc ? $array_key : ''; $global[] = $key . '[' . $array_key . ']=' . $this->getValueAsINI($item); } } else { // Not in a section so add the property to the global array. $global[] = $key . '=' . $this->getValueAsINI($value); $in_section = false; } } return implode("\n", array_merge($global, $local)); }
/** * Method to determine if an array is an associative array. * * @param array $array An array to test. * * @return boolean True if the array is an associative array. * * @since 11.1 * @deprecated 4.0 Use Joomla\Utilities\ArrayHelper::isAssociative instead */ public static function isAssociative($array) { return ArrayHelper::isAssociative($array); }
/** * Method to recursively bind data to a parent object. * * @param object $parent The parent object on which to attach the data values. * @param mixed $data An array or object of data to bind to the parent object. * @param boolean $recursive True to support recursive bindData. * @param boolean $allowNull True to allow null values. * * @return void * * @since 1.0 */ protected function bindData($parent, $data, $recursive = true, $allowNull = true) { // Ensure the input data is an array. $data = is_object($data) ? get_object_vars($data) : (array) $data; foreach ($data as $k => $v) { if (!$allowNull && !($v !== null && $v !== '')) { continue; } if ($recursive && (is_array($v) && ArrayHelper::isAssociative($v) || is_object($v))) { if (!isset($parent->{$k})) { $parent->{$k} = new \stdClass(); } $this->bindData($parent->{$k}, $v); continue; } $parent->{$k} = $v; } }
/** * Method to bind data to the form for the group level. * * @param string $group The dot-separated form group path on which to bind the data. * @param mixed $data An array or object of data to bind to the form for the group level. * * @return void * * @since 11.1 */ protected function bindLevel($group, $data) { // Ensure the input data is an array. if (is_object($data)) { if ($data instanceof Registry) { // Handle a Registry. $data = $data->toArray(); } elseif ($data instanceof JObject) { // Handle a JObject. $data = $data->getProperties(); } else { // Handle other types of objects. $data = (array) $data; } } // Process the input data. foreach ($data as $k => $v) { $level = $group ? $group . '.' . $k : $k; if ($this->findField($k, $group)) { // If the field exists set the value. $this->data->set($level, $v); } elseif (is_object($v) || ArrayHelper::isAssociative($v)) { // If the value is an object or an associative array, hand it off to the recursive bind level method. $this->bindLevel($level, $v); } } }
/** * Method to recursively bind data to a parent object. * * @param object $parent The parent object on which to attach the data values. * @param mixed $data An array or object of data to bind to the parent object. * @param boolean $recursive True to support recursive bindData. * * @return void * * @since 1.0 */ protected function bindData($parent, $data, $recursive = true) { // Ensure the input data is an array. if (is_object($data)) { $data = get_object_vars($data); } else { $data = (array) $data; } foreach ($data as $k => $v) { if ($v === '' || $v === null) { continue; } if (is_array($v) && ArrayHelper::isAssociative($v) || is_object($v) && $recursive) { if (!isset($parent->{$k})) { $parent->{$k} = new \stdClass(); } $this->bindData($parent->{$k}, $v); } else { $parent->{$k} = $v; } } }
/** * Method to recursively bind data to a parent object. * * @param object $parent The parent object on which to attach the data values. * @param mixed $data An array or object of data to bind to the parent object. * * @return void * * @since 11.1 */ protected function bindData($parent, $data) { // Ensure the input data is an array. if (is_object($data)) { $data = get_object_vars($data); } else { $data = (array) $data; } foreach ($data as $k => $v) { if (is_array($v) && ArrayHelper::isAssociative($v) || is_object($v)) { $parent->{$k} = new stdClass(); $this->bindData($parent->{$k}, $v); } else { $parent->{$k} = $v; } } }
/** * Method to bind data to the form for the group level. * * @param string $group The dot-separated form group path on which to bind the data. * @param mixed $data An array or object of data to bind to the form for the group level. * * @return void * * @since 11.1 */ protected function bindLevel($group, $data) { // Ensure the input data is an array. settype($data, 'array'); // Process the input data. foreach ($data as $k => $v) { if ($this->findField($k, $group)) { // If the field exists set the value. $this->data->set($group . '.' . $k, $v); } elseif (is_object($v) || ArrayHelper::isAssociative($v)) { // If the value is an object or an associative array, hand it off to the recursive bind level method $this->bindLevel($group . '.' . $k, $v); } } }
/** * Test the ArrayHelper::isAssociate method. * * @return void * * @since 1.0 * @sovers ArrayHelper::isAssociative */ public function testIsAssociative() { $this->assertThat(ArrayHelper::isAssociative(array(1, 2, 3)), $this->isFalse(), 'Line: ' . __LINE__ . ' This array should not be associative.'); $this->assertThat(ArrayHelper::isAssociative(array('a' => 1, 'b' => 2, 'c' => 3)), $this->isTrue(), 'Line: ' . __LINE__ . ' This array should be associative.'); $this->assertThat(ArrayHelper::isAssociative(array('a' => 1, 2, 'c' => 3)), $this->isTrue(), 'Line: ' . __LINE__ . ' This array should be associative.'); }