[enh] adding offsetAndCount() cleanup function
This commit is contained in:
parent
a1a49955c2
commit
25fdd2c7eb
|
@ -39,6 +39,25 @@ class Alternc_Api_Legacyobject {
|
||||||
return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "[" . $err->clsid . "] " . $err->error));
|
return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "[" . $err->clsid . "] " . $err->error));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** ensure that offset & count are set properly from $options.
|
||||||
|
*/
|
||||||
|
protected function offsetAndCount($options, $max) {
|
||||||
|
$offset = -1;
|
||||||
|
$count = -1;
|
||||||
|
if (isset($options["count"]))
|
||||||
|
$count = intval($options["count"]);
|
||||||
|
if (isset($options["offset"]))
|
||||||
|
$offset = intval($options["offset"]);
|
||||||
|
if ($offset != -1 || $count != -1) {
|
||||||
|
if ($offset < 0 || $offset > $max)
|
||||||
|
$offset = 0;
|
||||||
|
if ($count < 0 || $count > 1000)
|
||||||
|
$count = 1000;
|
||||||
|
}
|
||||||
|
return array($offset, $count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Aternc_Api_Legacyobject */
|
}
|
||||||
|
|
||||||
|
// Aternc_Api_Legacyobject
|
||||||
|
|
||||||
|
|
|
@ -171,17 +171,8 @@ class Alternc_Api_Object_Account extends Alternc_Api_Legacyobject {
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
return $this->alterncLegacyErrorManager();
|
return $this->alterncLegacyErrorManager();
|
||||||
} else {
|
} else {
|
||||||
$offset = -1;
|
list($offset, $count) = $this->offsetAndCount($options, count($result));
|
||||||
$count = -1;
|
|
||||||
if (isset($options["count"]))
|
|
||||||
$count = intval($options["count"]);
|
|
||||||
if (isset($options["offset"]))
|
|
||||||
$offset = intval($options["offset"]);
|
|
||||||
if ($offset != -1 || $count != -1) {
|
if ($offset != -1 || $count != -1) {
|
||||||
if ($offset < 0 || $offset > count($result))
|
|
||||||
$offset = 0;
|
|
||||||
if ($count < 0 || $count > 1000)
|
|
||||||
$count = 1000;
|
|
||||||
$result = array_slice($result, $offset, $count);
|
$result = array_slice($result, $offset, $count);
|
||||||
}
|
}
|
||||||
return new Alternc_Api_Response(array("content" => $result));
|
return new Alternc_Api_Response(array("content" => $result));
|
||||||
|
|
|
@ -44,17 +44,8 @@ class Alternc_Api_Object_Domain extends Alternc_Api_Legacyobject {
|
||||||
while ($me = $stmt->fetch(PDO::FETCH_OBJ)) {
|
while ($me = $stmt->fetch(PDO::FETCH_OBJ)) {
|
||||||
$result[$me->domaine] = $me;
|
$result[$me->domaine] = $me;
|
||||||
}
|
}
|
||||||
$offset = -1;
|
list($offset,$count)=$this->offsetAndCount($options, count($result));
|
||||||
$count = -1;
|
|
||||||
if (isset($options["count"]))
|
|
||||||
$count = intval($options["count"]);
|
|
||||||
if (isset($options["offset"]))
|
|
||||||
$offset = intval($options["offset"]);
|
|
||||||
if ($offset != -1 || $count != -1) {
|
if ($offset != -1 || $count != -1) {
|
||||||
if ($offset < 0 || $offset > count($result))
|
|
||||||
$offset = 0;
|
|
||||||
if ($count < 0 || $count > 1000)
|
|
||||||
$count = 1000;
|
|
||||||
$result = array_slice($result, $offset, $count);
|
$result = array_slice($result, $offset, $count);
|
||||||
}
|
}
|
||||||
return new Alternc_Api_Response(array("content" => $result));
|
return new Alternc_Api_Response(array("content" => $result));
|
||||||
|
|
|
@ -96,17 +96,8 @@ class Alternc_Api_Object_Ftp extends Alternc_Api_Legacyobject {
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
return $this->alterncLegacyErrorManager();
|
return $this->alterncLegacyErrorManager();
|
||||||
} else {
|
} else {
|
||||||
$offset = -1;
|
list($offset, $count) = $this->offsetAndCount($options, count($result));
|
||||||
$count = -1;
|
|
||||||
if (isset($options["count"]))
|
|
||||||
$count = intval($options["count"]);
|
|
||||||
if (isset($options["offset"]))
|
|
||||||
$offset = intval($options["offset"]);
|
|
||||||
if ($offset != -1 || $count != -1) {
|
if ($offset != -1 || $count != -1) {
|
||||||
if ($offset < 0 || $offset > count($result))
|
|
||||||
$offset = 0;
|
|
||||||
if ($count < 0 || $count > 1000)
|
|
||||||
$count = 1000;
|
|
||||||
$result = array_slice($result, $offset, $count);
|
$result = array_slice($result, $offset, $count);
|
||||||
}
|
}
|
||||||
return new Alternc_Api_Response(array("content" => $result));
|
return new Alternc_Api_Response(array("content" => $result));
|
||||||
|
|
Loading…
Reference in New Issue