public function getProperty($property)
{
// If property exists and initialised then return it
if (array_key_exists($property, $this->_data)) {
return $this->_data[$property];
}
// Fetch clean data for this property
if (array_key_exists($property, $this->_cleanData)) {
$data = $this->_cleanData[$property];
} else {
$data = array();
}
// If data is not an array then we can do nothing else with it
if (!is_array($data)) {
$this->_data[$property] = $data;
return $this->_data[$property];
}
// If property is supposed to be an array then initialise an array
if ($this->hasRequirement($property, 'Array')) {
return $this->_data[$property] = $data;
}
// If property is a reference to another document then fetch the reference document
$db = $this->getConfigAttribute('db');
if (MongoDBRef::isRef($data)) {
$collection = $data['$ref'];
$data = MongoDBRef::get($this->_getMongoDB(false), $data);
// If this is a broken reference then no point keeping it for later
if (!$data) {
$this->_data[$property] = null;
return $this->_data[$property];
}
$reference = true;
} else {
$collection = $this->getConfigAttribute('collection');
$reference = false;
}
// Find out the class name of the document or document set we are loaded
if ($className = $this->hasRequirement($property, 'DocumentSet')) {
$docType = 'Shanty_Mongo_DocumentSet';
} else {
$className = $this->hasRequirement($property, 'Document');
// Load a document anyway so long as $data is not empty
if (!$className && !empty($data)) {
$className = 'Shanty_Mongo_Document';
}
if ($className) {
$docType = 'Shanty_Mongo_Document';
}
}
// Nothing else to do
if (!$className) {
return null;
}
// Configure property for document/documentSet usage
$config = array();
$config['new'] = empty($data);
$config['connectionGroup'] = $this->getConfigAttribute('connectionGroup');
$config['db'] = $this->getConfigAttribute('db');
$config['collection'] = $collection;
$config['requirementModifiers'] = $this->getRequirements($property . '.');
$config['hasId'] = $this->hasRequirement($property, 'hasId');
if (!$reference) {
$config['pathToDocument'] = $this->getPathToProperty($property);
$config['criteria'] = $this->getCriteria();
}
// Initialise document
$document = new $className($data, $config);
// if this document was a reference then remember that
if ($reference) {
$this->_references->attach($document);
}
$this->_data[$property] = $document;
return $this->_data[$property];
}