160 lines
4.8 KiB
PHP
160 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Ldap;
|
|
|
|
use Ulmus\Ldap\Annotation\Classes\{ ObjectClass, ObjectType };
|
|
use Ulmus\{EntityCollection, Ulmus, SearchRequest, Query};
|
|
use Ulmus\Annotation\Property\{ Where, Having, Relation, Join, WithJoin, Relation\Ignore as RelationIgnore };
|
|
use Ulmus\Common\EntityResolver;
|
|
|
|
class Repository extends \Ulmus\Repository
|
|
{
|
|
use Repository\ConditionTrait;
|
|
|
|
const DEFAULT_ALIAS = "";
|
|
|
|
public array $events = [];
|
|
|
|
public function __construct(string $entity, string $alias = self::DEFAULT_ALIAS, ConnectionAdapter $adapter = null) {
|
|
parent::__construct($entity, $alias, $adapter);
|
|
|
|
$this->queryBuilder = new QueryBuilder();
|
|
}
|
|
|
|
protected function selectSqlQuery() : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
|
|
$this->select(array_keys($this->entityResolver->fieldList(EntityResolver::KEY_COLUMN_NAME)));
|
|
}
|
|
|
|
if ( null !== $objectClass = $this->entityResolver->getAnnotationFromClassname( ObjectClass::class, false ) ) {
|
|
$this->where('objectclass', $objectClass->type);
|
|
}
|
|
|
|
if ( null !== $objectClass = $this->entityResolver->getAnnotationFromClassname( ObjectType::class, false ) ) {
|
|
$this->where('objecttype', $objectClass->type);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function insertSqlQuery(array $dataset, bool $replace = false) : self
|
|
{
|
|
if ( null === $insert = $this->queryBuilder->getFragment(Query\Insert::class) ) {
|
|
$fields = array_map([ $this, 'escapeField' ] , array_keys($dataset));
|
|
|
|
$this->insert($fields, "", "", null);
|
|
}
|
|
|
|
$this->values($dataset);
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function updateSqlQuery(array $dataset) : self
|
|
{
|
|
$condition = array_pop($this->queryBuilder->where->conditionList);
|
|
|
|
if ($condition[0] === 'dn') {
|
|
if ( null === $this->queryBuilder->getFragment(Query\Update::class) ) {
|
|
$this->update($condition[1], "", null);
|
|
}
|
|
}
|
|
else {
|
|
array_push($this->queryBuilder->where->conditionList, $condition);
|
|
}
|
|
|
|
$this->set($dataset);
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function deleteSqlQuery() : self
|
|
{
|
|
/*$condition = array_pop($this->queryBuilder->where->conditionList);
|
|
|
|
if ($condition[0] === 'dn') {
|
|
if ( null === $this->queryBuilder->getFragment(Query\Delete::class) ) {
|
|
$this->delete($condition[1], "", null);
|
|
}
|
|
}
|
|
else {
|
|
array_push($this->queryBuilder->where->conditionList, $condition);
|
|
}
|
|
*/
|
|
return $this;
|
|
}
|
|
|
|
public function destroy(object $entity) : bool
|
|
{
|
|
if ( ! $this->matchEntity($entity) ) {
|
|
throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`");
|
|
}
|
|
|
|
$this->delete($entity->dn)->deleteAll()->rowCount();
|
|
|
|
return false;
|
|
}
|
|
|
|
public function loadAllFromOU(string $ou) : EntityCollection
|
|
{
|
|
$dn = $this->adapter->connector()->dn;
|
|
|
|
$this->adapter->connector()->dn = $ou;
|
|
|
|
$collection = $this->collectionFromQuery();
|
|
|
|
$this->adapter->connector()->dn = $dn;
|
|
|
|
return $collection;
|
|
}
|
|
|
|
public function insert(array $dataset, string $table, string $alias, ? string $schema, bool $replace = false) : self
|
|
{
|
|
$this->queryBuilder->insert($dataset);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function update(string $dn, string $alias, ? string $schema) : self
|
|
{
|
|
$this->queryBuilder->update($dn);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function delete(...$args) : self
|
|
{
|
|
$this->queryBuilder->delete($args[0]);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function escapeValue($identifier) : string
|
|
{
|
|
return is_object($identifier) ? $identifier : $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\Ldap::IDENTIFIER_FILTER);
|
|
}
|
|
|
|
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest, bool $count = true) : self
|
|
{
|
|
if ($count) {
|
|
$this->eventRegister(new class($searchRequest) implements \Ulmus\Event\Repository\CollectionFromQueryInterface {
|
|
|
|
protected SearchRequest\SearchRequestInterface $searchRequest;
|
|
|
|
public function __construct(SearchRequest\SearchRequestInterface $searchRequest) {
|
|
$this->searchRequest = $searchRequest;
|
|
}
|
|
|
|
public function execute(EntityCollection $collection) : EntityCollection
|
|
{
|
|
$this->searchRequest->count = $collection->count();
|
|
|
|
return $collection;
|
|
}
|
|
});
|
|
}
|
|
|
|
return parent::filterServerRequest($searchRequest, false);
|
|
}
|
|
} |