/** * Adds a callback to an array. * * @param array $callbacks * An array of callbacks to add the callback to, passed by reference. * @param array|string $callback * The callback to add. * @param array|string $replace * If specified, the callback will instead replace the specified value * instead of being appended to the $callbacks array. * @param int $action * Flag that determines how to add the callback to the array. * * @return bool * TRUE if the callback was added, FALSE if $replace was specified but its * callback could be found in the list of callbacks. */ public static function addCallback(array &$callbacks, $callback, $replace = NULL, $action = Materialize::CALLBACK_APPEND) { // Replace a callback. if ($replace) { // Iterate through the callbacks. foreach ($callbacks as $key => $value) { // Convert each callback and match the string values. if (Unicode::convertCallback($value) === Unicode::convertCallback($replace)) { $callbacks[$key] = $callback; return TRUE; } } // No match found and action shouldn't append or prepend. if ($action !== self::CALLBACK_REPLACE_APPEND || $action !== self::CALLBACK_REPLACE_PREPEND) { return FALSE; } } // Append or prepend the callback. switch ($action) { case self::CALLBACK_APPEND: case self::CALLBACK_REPLACE_APPEND: $callbacks[] = $callback; return TRUE; case self::CALLBACK_PREPEND: case self::CALLBACK_REPLACE_PREPEND: array_unshift($callbacks, $callback); return TRUE; default: return FALSE; } }