Skip to content

Commit

Permalink
Reimplemented original addToOffset method.
Browse files Browse the repository at this point in the history
  • Loading branch information
sheeep committed Jul 15, 2013
1 parent 04db98f commit 2db6ca7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Uploader/ErrorHandler/BlueimpErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public function addException(ResponseInterface $response, UploadException $excep
$message = $exception->getMessage();
}

$response->addToOffset('files', array('error' => $message));
$response->addToOffset(array('error' => $message), 'files');
}
}
32 changes: 27 additions & 5 deletions Uploader/Response/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,34 @@ public function offsetGet($offset)
return isset($this->data[$offset]) ? $this->data[$offset] : null;
}

public function addToOffset($offset, array $value)
/**
* The \ArrayAccess interface does not support multi-dimensional array syntax such as $array["foo"][] = bar
* This function will take a path of arrays and add a new element to it, creating the path if needed.
*
* @param mixed $value
* @param mixed $offset,...
*
* @throws \InvalidArgumentException if the path contains non-array or unset items.
*/
public function addToOffset($value, $offset)
{
if (!array_key_exists($offset, $this->data)) {
$this->data[$offset] = array();
}
$args = func_get_args();
array_shift($args);

$element =& $this->data;

$this->data[$offset] += $value;
foreach ($args as $offset) {
if (isset($element[$offset])) {
if (is_array($element[$offset])) {
$element =& $element[$offset];
} else {
throw new \InvalidArgumentException('The specified offset is set but is not an array at ' . $offset);
}
} else {
$element[$offset] = array();
$element =& $element[$offset];
}
}
$element[] = $value;
}
}

0 comments on commit 2db6ca7

Please sign in to comment.