lithium\util\Collection::toArray()

public static method

Exports a Collection instance to an array. Used by Collection::to().

Parameters

  • mixed $data

    Either a Collection instance, or an array representing a Collection's internal state.

  • array $options

    Options used when converting $data to an array:

    • 'handlers' array: An array where the keys are fully-namespaced class names, and the values are closures that take an instance of the class as a parameter, and return an array or scalar value that the instance represents.

Returns

array

Returns the value of $data as a pure PHP array, recursively converting all sub-objects and other values to their closest array or scalar equivalents.

Source

	public static function toArray($data, array $options = []) {
		$defaults = ['handlers' => []];
		$options += $defaults;
		$result = [];

		foreach ($data as $key => $item) {
			switch (true) {
				case is_array($item):
					$result[$key] = static::toArray($item, $options);
				break;
				case (!is_object($item)):
					$result[$key] = $item;
				break;
				case (isset($options['handlers'][$class = get_class($item)])):
					$result[$key] = $options['handlers'][$class]($item);
				break;
				case (method_exists($item, 'to')):
					$result[$key] = $item->to('array', $options);
				break;
				case ($vars = get_object_vars($item)):
					$result[$key] = static::toArray($vars, $options);
				break;
				case (method_exists($item, '__toString')):
					$result[$key] = (string) $item;
				break;
				default:
					$result[$key] = $item;
				break;
			}
		}
		return $result;
	}