mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 22:26:43 +02:00
fetchAssoc() && fetchPairs() throws exceptions
This commit is contained in:
@@ -211,6 +211,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
|||||||
*
|
*
|
||||||
* @param string associative descriptor
|
* @param string associative descriptor
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
final function fetchAssoc($assoc)
|
final function fetchAssoc($assoc)
|
||||||
{
|
{
|
||||||
@@ -221,6 +222,13 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
|||||||
$data = NULL;
|
$data = NULL;
|
||||||
$assoc = explode(',', $assoc);
|
$assoc = explode(',', $assoc);
|
||||||
|
|
||||||
|
// check fields
|
||||||
|
foreach ($assoc as $as) {
|
||||||
|
if ($as !== '*' && $as !== '#' && !array_key_exists($as, $row)) {
|
||||||
|
throw new InvalidArgumentException("Unknown column '$as' in associative descriptor");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (count($assoc) === 1) { // speed-up
|
if (count($assoc) === 1) { // speed-up
|
||||||
$as = $assoc[0];
|
$as = $assoc[0];
|
||||||
do {
|
do {
|
||||||
@@ -271,6 +279,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
|||||||
* @param string associative key
|
* @param string associative key
|
||||||
* @param string value
|
* @param string value
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
final function fetchPairs($key = NULL, $value = NULL)
|
final function fetchPairs($key = NULL, $value = NULL)
|
||||||
{
|
{
|
||||||
@@ -281,25 +290,34 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
|||||||
$data = array();
|
$data = array();
|
||||||
|
|
||||||
if ($value === NULL) {
|
if ($value === NULL) {
|
||||||
if ($key !== NULL) return FALSE; // error
|
if ($key !== NULL) {
|
||||||
|
throw new InvalidArgumentException("Either none or both fields must be specified");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($row) < 2) {
|
||||||
|
throw new LoginException("Result must have at least two columns");
|
||||||
|
}
|
||||||
|
|
||||||
// autodetect
|
// autodetect
|
||||||
if (count($row) < 2) return FALSE;
|
|
||||||
$tmp = array_keys($row);
|
$tmp = array_keys($row);
|
||||||
$key = $tmp[0];
|
$key = $tmp[0];
|
||||||
$value = $tmp[1];
|
$value = $tmp[1];
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (!array_key_exists($value, $row)) return FALSE;
|
if (!array_key_exists($value, $row)) {
|
||||||
|
throw new InvalidArgumentException("Unknown value column '$value'");
|
||||||
|
}
|
||||||
|
|
||||||
if ($key === NULL) { // autodetect
|
if ($key === NULL) { // indexed-array
|
||||||
do {
|
do {
|
||||||
$data[] = $row[$value];
|
$data[] = $row[$value];
|
||||||
} while ($row = $this->fetch());
|
} while ($row = $this->fetch());
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!array_key_exists($key, $row)) return FALSE;
|
if (!array_key_exists($key, $row)) {
|
||||||
|
throw new InvalidArgumentException("Unknown key column '$key'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
Reference in New Issue
Block a user