diff --git a/classes/fSession.php b/classes/fSession.php index 98afc652..8c09c106 100644 --- a/classes/fSession.php +++ b/classes/fSession.php @@ -15,7 +15,8 @@ * @package Flourish * @link http://flourishlib.com/fSession * - * @version 1.0.0b15 + * @version 1.0.0b16 + * @changes 1.0.0b16 Changed ::delete() to return the value of the key being deleted [wb, 2010-09-19] * @changes 1.0.0b15 Added documentation about `[sub-key]` syntax [wb, 2010-09-12] * @changes 1.0.0b14 Backwards Compatibility Break - ::add(), ::delete(), ::get() and ::set() now interpret `[` and `]` as array shorthand and thus they can not be used in keys - added `$beginning` parameter to ::add(), added ::remove() method [wb, 2010-09-12] * @changes 1.0.0b13 Fixed a bug that prevented working with existing sessions since they did not have the `fSession::expires` key [wb, 2010-08-24] @@ -185,20 +186,23 @@ static public function close() /** * Deletes a value from the session * - * @param string $key The key of the value to delete - array elements can be modified via `[sub-key]` syntax, and thus `[` and `]` can not be used in key names - * @return void + * @param string $key The key of the value to delete - array elements can be modified via `[sub-key]` syntax, and thus `[` and `]` can not be used in key names + * @param mixed $default_value The value to return if the `$key` is not set + * @return mixed The value of the `$key` that was deleted */ - static public function delete($key) + static public function delete($key, $default_value=NULL) { self::open(); + $value = $default_value; + if ($bracket_pos = strpos($key, '[')) { $original_key = $key; $array_dereference = substr($key, $bracket_pos); $key = substr($key, 0, $bracket_pos); if (!isset($_SESSION[$key])) { - return; + return $value; } preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER); @@ -208,7 +212,7 @@ static public function delete($key) foreach (array_slice($array_keys, 0, -1) as $array_key) { if (!isset($tip[$array_key])) { - return; + return $value; } elseif (!is_array($tip[$array_key])) { throw new fProgrammerException( '%1$s was called for an element, %2$s, which is not an array', @@ -218,11 +222,19 @@ static public function delete($key) } $tip =& $tip[$array_key]; } - unset($tip[end($array_keys)]); + + $key = end($array_keys); } else { - unset($_SESSION[$key]); + $tip =& $_SESSION; } + + if (isset($tip[$key])) { + $value = $tip[$key]; + unset($tip[$key]); + } + + return $value; } diff --git a/classes/fTemplating.php b/classes/fTemplating.php index 63d7357b..4fe46ade 100644 --- a/classes/fTemplating.php +++ b/classes/fTemplating.php @@ -10,7 +10,8 @@ * @package Flourish * @link http://flourishlib.com/fTemplating * - * @version 1.0.0b16 + * @version 1.0.0b17 + * @changes 1.0.0b17 Backwards Compatibility Break - ::delete() now returns the values of the element or elements that were deleted instead of returning the fTemplating instance [wb, 2010-09-19] * @changes 1.0.0b16 Fixed another bug with minifying JS regex literals [wb, 2010-09-13] * @changes 1.0.0b15 Fixed a bug with minifying JS regex literals that occur after a reserved word [wb, 2010-09-12] * @changes 1.0.0b14 Added documentation about `[sub-key]` syntax [wb, 2010-09-12] @@ -308,13 +309,33 @@ public function buffer() /** * Deletes an element from the template * - * @param string $element The element to delete - array elements can be modified via `[sub-key]` syntax, and thus `[` and `]` can not be used in element names - * @param array |$elements The elements to delete - * @return fTemplating The template object, to allow for method chaining + * @param string $element The element to delete - array elements can be modified via `[sub-key]` syntax, and thus `[` and `]` can not be used in element names + * @param mixed $default_value The value to return if the `$element` is not set + * @param array |$elements The elements to delete - an array of element names or an associative array of keys being element names and the values being the default values + * @return mixed The value of the `$element` that was deleted - an associative array of deleted elements will be returned if an array of `$elements` was specified */ - public function delete($element) + public function delete($element, $default_value=NULL) { - $tip =& $this->elements; + if (is_array($element)) { + $elements = $element; + + if (is_numeric(key($elements))) { + $new_elements = array(); + foreach ($elements as $element) { + $new_elements[$element] = NULL; + } + $elements = $new_elements; + } + + $output = array(); + foreach ($elements as $key => $default_value) { + $output[$key] = $this->delete($key, $default_value); + } + return $output; + } + + $tip =& $this->elements; + $value = $default_value; if ($bracket_pos = strpos($element, '[')) { $original_element = $element; @@ -327,7 +348,7 @@ public function delete($element) foreach (array_slice($array_keys, 0, -1) as $array_key) { if (!isset($tip[$array_key])) { - return $this; + return $value; } elseif (!is_array($tip[$array_key])) { throw new fProgrammerException( '%1$s was called for an element, %2$s, which is not an array', @@ -340,16 +361,12 @@ public function delete($element) $element = end($array_keys); } - if (is_array($element)) { - foreach ($element as $key) { - $this->delete($key); - } - return $this; + if (isset($tip[$element])) { + $value = $tip[$element]; + unset($tip[$element]); } - unset($tip[$element]); - - return $this; + return $value; }