Skip to content

Commit

Permalink
refactor: replace Properties::all() with `Properties::jsonSerialize…
Browse files Browse the repository at this point in the history
…()`.

Now `Properties` are immutable.

BREAKING CHANGE: yes
  • Loading branch information
drupol committed May 16, 2023
1 parent 018e32f commit 7f58bb1
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 71 deletions.
2 changes: 1 addition & 1 deletion spec/EcPhp/CasLib/Cas.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static function getTestProperties(): CasProperties

public static function getTestPropertiesWithPgtUrl(): CasProperties
{
$properties = self::getTestProperties()->all();
$properties = self::getTestProperties()->jsonSerialize();

$properties['protocol']['serviceValidate']['default_parameters']['pgtUrl'] = 'https://from/proxyCallback.php';

Expand Down
38 changes: 6 additions & 32 deletions spec/EcPhp/CasLib/Configuration/PropertiesSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,32 @@
namespace spec\EcPhp\CasLib\Configuration;

use EcPhp\CasLib\Configuration\Properties;
use JsonSerializable;
use PhpSpec\ObjectBehavior;
use spec\EcPhp\CasLib\Cas;

class PropertiesSpec extends ObjectBehavior
{
public function it_can_be_used_as_an_array()
public function it_can_be_json_encoded()
{
$properties = Cas::getTestProperties();

$this->beConstructedWith($properties->all());

$this
->offsetGet('base_url')
->shouldReturn('http://local/cas');

$this
->offsetSet('foo', 'bar');

$this
->offsetUnset('base_url');
}

public function it_can_modify_the_configuration()
{
$properties = [
'foo' => 'bar',
'protocol' => [
'test' => [
],
],
];

$this->beConstructedWith($properties);
$this->beConstructedWith(['foo' => 'bar']);

$this
->all()
->jsonSerialize()
->shouldReturn([
'foo' => 'bar',
'protocol' => [
'test' => [
'default_parameters' => [],
],
],
]);
}

public function it_is_initializable()
{
$properties = (array) Cas::getTestProperties();
$properties = Cas::getTestProperties()->jsonSerialize();

$this->beConstructedWith($properties);

$this->shouldHaveType(Properties::class);
$this->shouldImplement(JsonSerializable::class);
}
}
25 changes: 1 addition & 24 deletions src/Configuration/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
namespace EcPhp\CasLib\Configuration;

use EcPhp\CasLib\Contract\Configuration\PropertiesInterface;
use Exception;

use function array_key_exists;

final class Properties implements PropertiesInterface
{
Expand All @@ -35,28 +32,8 @@ public function __construct(array $properties)
}
}

public function all(): array
public function jsonSerialize(): array
{
return $this->properties;
}

public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->properties);
}

public function offsetGet(mixed $offset): mixed
{
return $this->properties[$offset];
}

public function offsetSet(mixed $offset, mixed $value): void
{
throw new Exception('Read-only object, setters are disabled.');
}

public function offsetUnset(mixed $offset): void
{
throw new Exception('Read-only object, setters are disabled.');
}
}
11 changes: 2 additions & 9 deletions src/Contract/Configuration/PropertiesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@

namespace EcPhp\CasLib\Contract\Configuration;

use ArrayAccess;
use JsonSerializable;

/**
* @template-extends ArrayAccess<string, mixed>
*/
interface PropertiesInterface extends ArrayAccess
interface PropertiesInterface extends JsonSerializable
{
/**
* @return array<string, mixed>
*/
public function all(): array;
}
2 changes: 1 addition & 1 deletion src/Handler/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function buildUri(
string $type,
array $queryParams = []
): UriInterface {
$properties = $this->getProperties();
$properties = $this->getProperties()->jsonSerialize();

$queryParams += Uri::getParams($from);
$baseUrl = parse_url((string) $properties['base_url']);
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function handle(RequestInterface $request): ResponseInterface
{
$parameters = $this->buildParameters(
$this->getParameters(),
$this->getProperties()['protocol'][HandlerInterface::TYPE_LOGIN]['default_parameters'] ?? [],
$this->getProperties()->jsonSerialize()['protocol'][HandlerInterface::TYPE_LOGIN]['default_parameters'] ?? [],
['service' => (string) $request->getUri()],
);

Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function handle(RequestInterface $request): ResponseInterface
{
$parameters = $this->buildParameters(
$this->getParameters(),
$this->getProperties()['protocol'][HandlerInterface::TYPE_LOGOUT]['default_parameters'] ?? [],
$this->getProperties()->jsonSerialize()['protocol'][HandlerInterface::TYPE_LOGOUT]['default_parameters'] ?? [],
['service' => (string) $request->getUri()],
);

Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
$parameters = $this->buildParameters(
$this->getParameters(),
$this->getProperties()['protocol'][HandlerInterface::TYPE_PROXY]['default_parameters'] ?? [],
$this->getProperties()->jsonSerialize()['protocol'][HandlerInterface::TYPE_PROXY]['default_parameters'] ?? [],
['service' => (string) $request->getUri()],
);

Expand Down
2 changes: 1 addition & 1 deletion src/Handler/ServiceValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class ServiceValidate extends Handler implements ServiceValidateHandlerInt
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
$properties = $this->getProperties();
$properties = $this->getProperties()->jsonSerialize();

$type = isset($properties['protocol'][HandlerInterface::TYPE_SERVICE_VALIDATE]['default_parameters']['pgtUrl'])
? HandlerInterface::TYPE_PROXY_VALIDATE
Expand Down

0 comments on commit 7f58bb1

Please sign in to comment.