* This class repreesnts a collection of documents, stored on the filesystem.
*/
class FilesCollection extends Collection {
///
/// Helper methods
///
/**
- * Gets the path to the folder where the specified collection id is stored.
+ * Gets the path to the folder where the specified collection documents are stored.
*
* @param string $collectionId The collection identifiant
* @return string The path to the specified collection folder
*/
public static function getCollectionPath ($collectionId) {
global $Config;
if (!array_key_exists('DocumentStorage', $Config) || !array_key_exists('Path', $Config['DocumentStorage'])) {
throw new Exception("Configuration parameter missing: \$Config['DocumentStorage']['Path']. Expected value for this parameter is the path to the collections folders.");
user_error("File $filename doesn't exist. The update operation has became an insert one.", E_USER_WARNING);
}
$data = json_encode($document);
file_put_contents($filename, $data);
}
/**
* Gets a document from the collection
*
* @param string $documentId The identifiant of the document to get
* @param CollectionDocument $document The document
*/
public function get ($documentId) {
$type = $this->documentType;
if (!class_exists($type)) {
- throw new Exception("Can't create an instance of $type. If the class exists, did you registered a SPL autoloader or updated includes/autoload.php?");
+ throw new Exception("Can't create an instance of $type. If the class exists, did you register a SPL autoloader or updated includes/autoload.php?");
* @param string $documentId The identifiant of the document to get
* @param CollectionDocument $document The document
*/
public function get ($documentId) {
$data = $this->mongoCollection->findOne(
[ '_id' => $documentId ]
);
+ return $this->getDocumentFromArray($data);
+ }
+
+ /**
+ * Gets a document of the relevant collection documents type from an array.
+ */
+ public function getDocumentFromArray($documentArray) {
$type = $this->documentType;
if (!class_exists($type)) {
- throw new Exception("Can't create an instance of $type. If the class exists, did you registered a SPL autoloader or updated includes/autoload.php?");
+ throw new Exception("Can't create an instance of $type. If the class exists, did you register a SPL autoloader or updated includes/autoload.php?");
}
- return $type::loadFromObject($data);
+ return $type::loadFromObject($documentArray);
+ }
+
+ /**
+ * Gets a count of the documents in the collection
+ *
+ * @return int The number of documents
+ */
+ public function count () {
+ return $this->mongoCollection->count();
+ }
+
+ /**
+ * Gets all the documents from the collection
+ *
+ * @return Iterator An iterator to the documents, each item an instance of CollectionDocument
$this->assertEquals('Iain M. Banks', $newBook->author);
$this->assertEquals('redBook', $newBook->id);
- //Set
+ //::set - an existing document as parameter
$previousId = $this->redBook->id;
$this->collection->set($this->redBook);
$this->assertEquals(
$previousId,
$this->redBook->id,
"The document ID has been modified during a set operation on an already added dcoument. It should stay the same. Old id: $previousId. New id: " . $this->redBook->id
$this->fail("An expected exception has not been raised. If you're logged as root, you can safely delete /root/obsidiancollections folder and ignore this test. By the way, are you sure to trust a tests sequence creating and deleting files to run them as root?");
}
///
/// CRUD tests
///
use CRUDTestTrait;
/**
* @covers FilesCollection::add
* @covers FilesCollection::update
*/
public function testFileContent () {
global $Config;
$Config = self::getConfig();
$book = new BookDocument('Iain Banks', 'Excession');