Skip to content

Commit

Permalink
Added generation of custom IDS
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriele120987 committed Feb 15, 2021
1 parent 3cee526 commit fd59446
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/Sag.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@
/**
* The Sag class provides the core functionality for talking to CouchDB.
*
* @version %VERSION%
* @version 0.9.1
* @package Core
*/
class Sag {

/**
* @var string Used by login() to use HTTP Basic Authentication.
* @static
*/
public static $VERSION = "0.9.1";

/**
* @var string Used by login() to use HTTP Basic Authentication.
* @static
Expand Down Expand Up @@ -379,6 +386,7 @@ public function put($id, $data)

$toSend = (is_string($data)) ? $data : json_encode($data);
$id = urlencode($id);
$id = str_replace("%2F", "/", $id); /** Url Encoding a / confuses Couch **/

$url = "/{$this->db}/$id";
$response = $this->procPacket('PUT', $url, $toSend);
Expand Down Expand Up @@ -714,6 +722,41 @@ public function generateIDs($num = 10) {
}

/**
* Uses CouchDB to generate IDs with custom prefix ad separator.
*
* @param int $num The number of IDs to generate (>= 0). Defaults to 10.
* @param string $prefix The prefix for the ID
* @param string $separator the separatore between prefix and ID
* @return array
*/
public function generateIDsCustom($num = 10, $prefix = "", $separator = ":") {
$ids = array();

$docs = $this->generateIDs($num);
if ($docs->status == 200) {
if (isset($docs->body->uuids) && is_array($docs->body->uuids)) {
foreach ($docs->body->uuids as $uuid) {
$ids[] = $prefix . $separator . $uuid;
}
}
}

return $ids;
}

/**
* Uses CouchDB to generate only one ID with custom prefix ad separator.
*
* @param string $prefix The prefix for the ID
* @param string $separator the separatore between prefix and ID
* @return string
*/
public function generateIDCustom($prefix, $separator = ":") {
$ids = $this->generateIDsCustom(1, $prefix, $separator);
return $ids[0];
}

/**
* Creates a database with the specified name.
*
* @param string $name The name of the database you want to create.
Expand Down Expand Up @@ -1128,7 +1171,7 @@ private function procPacket($method, $url, $data = null, $headers = array()) {

// Build the request packet.
$headers["Host"] = "{$this->host}:{$this->port}";
$headers["User-Agent"] = "Sag/%VERSION%";
$headers["User-Agent"] = "Sag/" . self::$VERSION;

/*
* This prevents some unRESTful requests, such as inline attachments in
Expand Down

0 comments on commit fd59446

Please sign in to comment.