Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0.0] Use Options objects instead of arrays. #64

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 285 additions & 0 deletions src/FreeDSx/Ldap/ClientOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
<?php

declare(strict_types=1);

/**
* This file is part of the FreeDSx LDAP package.
*
* (c) Chad Sikorra <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FreeDSx\Ldap;

final class ClientOptions
{
private int $version = 3;

private array $servers = [];

private int $port = 389;

private string $transport = 'tcp';

private ?string $baseDn = null;

private int $pageSize = 1000;

private bool $useSsl = false;

private bool $sslValidateCert = true;

private bool $sslAllowSelfSigned = false;

private ?string $sslCaCert = null;

private ?string $sslPeerName = null;

private int $timeoutConnect = 3;

private int $timeoutRead = 10;

private string $referral = 'throw';

private ?ReferralChaserInterface $referralChaser = null;

private int $referralLimit = 10;

/**
* A helper method designed to ease migration from array options to the new options object.
*
* @param array<string, mixed> $options
*/
public static function fromArray(array $options): self
{
$instance = new self();

return $instance;
}

public function getVersion(): int
{
return $this->version;
}

public function setVersion(int $version): self
{
$this->version = $version;

return $this;
}

/**
* @return string[]
*/
public function getServers(): array
{
return $this->servers;
}

/**
* @param string[] $servers
*/
public function setServers(array $servers): self
{
$this->servers = $servers;

return $this;
}

public function getPort(): int
{
return $this->port;
}

public function setPort(int $port): self
{
$this->port = $port;

return $this;
}

public function getTransport(): string
{
return $this->transport;
}

public function setTransport(string $transport): self
{
$this->transport = $transport;

return $this;
}

public function getBaseDn(): ?string
{
return $this->baseDn;
}

public function setBaseDn(?string $baseDn): self
{
$this->baseDn = $baseDn;

return $this;
}

public function getPageSize(): int
{
return $this->pageSize;
}

public function setPageSize(int $pageSize): self
{
$this->pageSize = $pageSize;

return $this;
}

public function isUseSsl(): bool
{
return $this->useSsl;
}

public function setUseSsl(bool $useSsl): self
{
$this->useSsl = $useSsl;

return $this;
}

public function isSslValidateCert(): bool
{
return $this->sslValidateCert;
}

public function setSslValidateCert(bool $sslValidateCert): self
{
$this->sslValidateCert = $sslValidateCert;

return $this;
}

public function isSslAllowSelfSigned(): bool
{
return $this->sslAllowSelfSigned;
}

public function setSslAllowSelfSigned(bool $sslAllowSelfSigned): self
{
$this->sslAllowSelfSigned = $sslAllowSelfSigned;

return $this;
}

public function getSslCaCert(): ?string
{
return $this->sslCaCert;
}


public function setSslCaCert(?string $sslCaCert): self
{
$this->sslCaCert = $sslCaCert;

return $this;
}

public function getSslPeerName(): ?string
{
return $this->sslPeerName;
}

public function setSslPeerName(?string $sslPeerName): self
{
$this->sslPeerName = $sslPeerName;

return $this;
}

public function getTimeoutConnect(): int
{
return $this->timeoutConnect;
}

public function setTimeoutConnect(int $timeoutConnect): self
{
$this->timeoutConnect = $timeoutConnect;

return $this;
}

public function getTimeoutRead(): int
{
return $this->timeoutRead;
}

public function setTimeoutRead(int $timeoutRead): self
{
$this->timeoutRead = $timeoutRead;

return $this;
}

public function getReferral(): string
{
return $this->referral;
}

public function setReferral(string $referral): self
{
$this->referral = $referral;

return $this;
}

public function getReferralChaser(): ?ReferralChaserInterface
{
return $this->referralChaser;
}

public function setReferralChaser(?ReferralChaserInterface $referralChaser): self
{
$this->referralChaser = $referralChaser;

return $this;
}

public function getReferralLimit(): int
{
return $this->referralLimit;
}

public function setReferralLimit(int $referralLimit): self
{
$this->referralLimit = $referralLimit;

return $this;
}

/**
* @return array{version: int, servers: string[], port: int, base_dn: ?string, page_size: int, use_ssl: bool, ssl_validate_cert: bool, ssl_allow_self_signed: bool, ssl_ca_cert: ?string, ssl_peer_name: ?string, timeout_connect: int, timeout_read: int, referral: string, referral_chaser: ?ReferralChaserInterface, referral_limit: int}
*/
public function toArray(): array
{
return [
'version' => $this->getVersion(),
'servers' => $this->getServers(),
'port' => $this->getPort(),
'transport' => $this->getTransport(),
'base_dn' => $this->getBaseDn(),
'page_size' => $this->getPageSize(),
'use_ssl' => $this->isUseSsl(),
'ssl_validate_cert' => $this->isSslValidateCert(),
'ssl_allow_self_signed' => $this->isSslAllowSelfSigned(),
'ssl_ca_cert' => $this->getSslCaCert(),
'ssl_peer_name' => $this->getSslPeerName(),
'timeout_connect' => $this->getTimeoutConnect(),
'timeout_read' => $this->getTimeoutRead(),
'referral' => $this->getReferral(),
'referral_chaser' => $this->getReferralChaser(),
'referral_limit' => $this->getReferralLimit(),
];
}
}
50 changes: 9 additions & 41 deletions src/FreeDSx/Ldap/LdapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,13 @@ class LdapClient

public const REFERRAL_THROW = 'throw';

/**
* @var array<string, mixed>
*/
private array $options = [
'version' => 3,
'servers' => [],
'port' => 389,
'transport' => 'tcp',
'base_dn' => null,
'page_size' => 1000,
'use_ssl' => false,
'ssl_validate_cert' => true,
'ssl_allow_self_signed' => null,
'ssl_ca_cert' => null,
'ssl_peer_name' => null,
'timeout_connect' => 3,
'timeout_read' => 10,
'referral' => 'throw',
'referral_chaser' => null,
'referral_limit' => 10,
];
private ClientOptions $options;

private ?ClientProtocolHandler $handler = null;

/**
* @param array<string, mixed> $options
*/
public function __construct(array $options = [])
public function __construct(ClientOptions $options = new ClientOptions())
{
$this->options = array_merge(
$this->options,
$options,
);
$this->options = $options;
}

/**
Expand All @@ -98,7 +72,7 @@ public function bind(
): LdapMessageResponse {
return $this->sendAndReceive(
Operations::bind($username, $password)
->setVersion($this->options['version'])
->setVersion($this->options->getVersion())
);
}

Expand All @@ -117,7 +91,7 @@ public function bindSasl(
): LdapMessageResponse {
return $this->sendAndReceive(
Operations::bindSasl($options, $mechanism)
->setVersion($this->options['version'])
->setVersion($this->options->getVersion())
);
}

Expand Down Expand Up @@ -312,7 +286,7 @@ public function paging(
return new Paging(
client: $this,
search: $search,
size: $size ?? (int) $this->options['page_size']
size: $size ?? $this->options->getPageSize()
);
}

Expand Down Expand Up @@ -446,10 +420,8 @@ public function controls(): ControlBag

/**
* Get the options currently set.
*
* @return array<string, mixed>
*/
public function getOptions(): array
public function getOptions(): ClientOptions
{
return $this->options;
}
Expand All @@ -458,18 +430,14 @@ public function getOptions(): array
* Merge a set of options. Depending on what you are changing, you many want to set the $forceDisconnect param to
* true, which forces the client to disconnect. After which you would have to manually bind again.
*
* @param array<string, mixed> $options The set of options to merge in.
* @param bool $forceDisconnect Whether the client should disconnect; forcing a manual re-connect / bind. This is
* false by default.
*/
public function setOptions(
array $options,
ClientOptions $options,
bool $forceDisconnect = false
): self {
$this->options = array_merge(
$this->options,
$options
);
$this->options = $options;
if ($forceDisconnect) {
$this->unbindIfConnected();
}
Expand Down
Loading