Skip to content

Commit

Permalink
[TASK] cleaner resource handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kaystrobach committed Mar 30, 2015
1 parent 70b5acf commit 55c6e28
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Classes/KayStrobach/Ldap/Domain/Model/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

namespace KayStrobach\Ldap\Domain\Model;
use KayStrobach\Ldap\Service\Exception\OperationException;

/**
* Class LdapEntry
Expand Down Expand Up @@ -61,10 +62,14 @@ public function getAsArray() {
* @throws \KayStrobach\Ldap\Service\Exception\OperationException
*/
public function getDn() {
if($this->entryAsResource !== FALSE) {
if(is_resource($this->entryAsResource)) {
$dn = ldap_get_dn($this->ldapConnection->getResource(), $this->entryAsResource);
$this->ldapConnection->checkError('getDn');
return $dn;
} elseif(array_key_exists('dn', $this->getAsArray())) {
return $this->getAsArray()['dn'];
} else {
throw new OperationException('Can´t detect DN of object');
}
}

Expand Down
23 changes: 20 additions & 3 deletions Classes/KayStrobach/Ldap/Persistence/LdapQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace KayStrobach\Ldap\Persistence;

use KayStrobach\Ldap\Utility\CleanResultUtility;
use TYPO3\Flow\Persistence\QueryResultInterface;
use KayStrobach\Ldap\Domain\Model\Entry;
use TYPO3\Flow\Annotations as Flow;

class LdapQueryResult implements QueryResultInterface {
Expand Down Expand Up @@ -59,6 +61,20 @@ protected function initialize() {
}
}

/**
* @param $data
* @return Entry
*/
protected function toEntry($data) {
if(is_array($data)) {
$cleanedData = CleanResultUtility::stripCountFromArray($data);
$entry = new Entry($this->ldapConnection, NULL, $data);
} else {
$entry = NULL;
}
return $entry;
}

/**
* Returns a clone of the query object
*
Expand All @@ -71,12 +87,12 @@ public function getQuery() {
/**
* Returns the first object in the result set
*
* @return object
* @return Entry
*/
public function getFirst() {
$this->initialize();
reset($this->resultArray);
return current($this->resultArray);
return $this->current();
}

/**
Expand Down Expand Up @@ -152,7 +168,8 @@ public function offsetUnset($offset) {
*/
public function current() {
$this->initialize();
return current($this->resultArray);
$data = current($this->resultArray);
return $this->toEntry($data);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions Classes/KayStrobach/Ldap/Utility/CleanResultUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Created by PhpStorm.
* User: kay
* Date: 30.03.15
* Time: 11:33
*/

namespace KayStrobach\Ldap\Utility;


class CleanResultUtility {
/**
* @param $array
* @return array
*/
public static function stripCountFromArray($array) {
if(array_key_exists('count', $array)) {
unset($array['count']);
}
foreach($array as $key => $value) {
if(is_array($value)) {
$array['key'] = self::stripCountFromArray($value);
}
}
return $array;
}
}

0 comments on commit 55c6e28

Please sign in to comment.