Added object results in search, scope in search and other minor items

This commit is contained in:
Deon George 2014-02-17 11:30:59 +11:00
parent 732ea8b978
commit 06bf21f1aa
4 changed files with 75 additions and 27 deletions

View File

@ -23,31 +23,33 @@ abstract class Kohana_Database_LDAP_Result extends Database_Result {
if (! $this->_current_entry) if (! $this->_current_entry)
return array(); return array();
// Return an array of the row
// @todo We could probably cache this for optimisation
$attrs = $vals = array();
array_push($attrs,ldap_first_attribute($this->_result['l'],$this->_current_entry));
while ($x = ldap_next_attribute($this->_result['l'],$this->_current_entry))
array_push($attrs,$x);
foreach ($attrs as $attr) {
$vals[strtolower($attr)] = ldap_get_values($this->_result['l'],$this->_current_entry,$attr);
// We dont need the count entry
unset($vals[strtolower($attr)]['count']);
}
$vals['dn'] = $this->key();
if ($this->_as_object === TRUE) { if ($this->_as_object === TRUE) {
// Return an stdClass // Return an stdClass
throw HTTP_Exception::factory(501,'Not implemented'); throw HTTP_Exception::factory(501,'Not implemented',array(':method'=>__METHOD__));
} elseif (is_string($this->_as_object)) { } elseif (is_string($this->_as_object)) {
// Return an object of given class name $x = new $this->_as_object;
throw HTTP_Exception::factory(501,'Not implemented');
return $x->result_load_values($vals);
} else { } else {
// Return an array of the row
// @todo We could probably cache this for optimisation
$attrs = $vals = array();
array_push($attrs,ldap_first_attribute($this->_result['l'],$this->_current_entry));
while ($x = ldap_next_attribute($this->_result['l'],$this->_current_entry))
array_push($attrs,$x);
foreach ($attrs as $attr) {
$vals[strtolower($attr)] = ldap_get_values($this->_result['l'],$this->_current_entry,$attr);
// We dont need the count entry
unset($vals[strtolower($attr)]['count']);
}
$vals['dn'] = $this->key();
return $vals; return $vals;
} }
} }
@ -165,17 +167,17 @@ abstract class Kohana_Database_LDAP_Result extends Database_Result {
$result[$dn] = $row; $result[$dn] = $row;
} elseif ($key === NULL) { } elseif ($key === NULL) {
throw HTTP_Exception::factory(501,'Not implemented'); throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__));
// Indexed columns // Indexed columns
} elseif ($value === NULL) { } elseif ($value === NULL) {
throw HTTP_Exception::factory(501,'Not implemented'); throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__));
// Associative rows // Associative rows
} else { } else {
throw HTTP_Exception::factory(501,'Not implemented'); throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__));
// Associative columns // Associative columns
} }

View File

@ -30,6 +30,9 @@ abstract class Kohana_Database_LDAP_Search {
// Parameters for __construct when using object results // Parameters for __construct when using object results
protected $_object_params = array(); protected $_object_params = array();
// Return results as associative arrays or objects
protected $_as_object = FALSE;
/** /**
* Callable database methods * Callable database methods
* @var array * @var array
@ -37,7 +40,7 @@ abstract class Kohana_Database_LDAP_Search {
protected static $_db_methods = array( protected static $_db_methods = array(
'where', 'and_where', 'or_where', 'where_open', 'and_where_open', 'or_where_open', 'where_close', 'where', 'and_where', 'or_where', 'where_open', 'and_where_open', 'or_where_open', 'where_close',
'and_where_close', 'or_where_close', 'and_where_close', 'or_where_close',
'limit', 'limit','scope',
); );
/** /**
@ -132,6 +135,23 @@ abstract class Kohana_Database_LDAP_Search {
return $this; return $this;
} }
/**
* Returns results as objects
*
* @param string $class classname or TRUE for stdClass
* @param array $params
* @return $this
*/
public function as_object($class = TRUE, array $params = NULL) {
$this->_as_object = $class;
// Add object parameters
if ($params)
$this->_object_params = $params;
return $this;
}
/** /**
* Figure out the bases * Figure out the bases
*/ */
@ -188,7 +208,7 @@ abstract class Kohana_Database_LDAP_Search {
/** /**
* Search the LDAP database * Search the LDAP database
*/ */
public function execute($db=NULL,$as_object=FALSE,$object_params=NULL) { public function execute($db=NULL,$as_object=NULL,$object_params=NULL) {
$query = array(); $query = array();
if (! is_object($this->_db)) if (! is_object($this->_db))

View File

@ -25,6 +25,8 @@ abstract class Kohana_ORM_LDAP extends ORM {
foreach ($id as $column => $value) foreach ($id as $column => $value)
$this->where($column, '=', $value); $this->where($column, '=', $value);
$this->limit(1);
$this->scope('sub');
$this->find(); $this->find();
} else { } else {
@ -220,9 +222,33 @@ abstract class Kohana_ORM_LDAP extends ORM {
} }
else else
{ {
throw new Kohana_Exception('The :property property does not exist in the :class class', // The item doesnt exist.
array(':property' => $column, ':class' => get_class($this))); return NULL;
} }
} }
public function result_load_values(array $values) {
$this->clear();
$this->_load_values($values);
return $this;
}
// @todo Taken into account the schema definition
public function resolve($key) {
eval("\$x = \$this->get_first('$key');");
return $x;
}
public function scope($scope) {
// Add pending database call which is executed after query type is determined
$this->_db_pending[] = array(
'name' => 'scope',
'args' => array($scope),
);
return $this;
}
} }
?> ?>

View File

@ -13,7 +13,7 @@ class Model_LDAP_Schema extends ORM_LDAP {
public function attribute($column) { public function attribute($column) {
$k = 'attributetypes'; $k = 'attributetypes';
return new Schema_Attribute(($x = $this->_attr_search($k,$column) ? $this->_object[$k][$x] : ''); return new Schema_Attribute(($x = $this->_attr_search($k,$column)) ? $this->_object[$k][$x] : '');
} }
private function _attr_search($key,$column) { private function _attr_search($key,$column) {