From b7d5a79c3a41cfa49d08028c675a8940ac988cdc Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 23 Dec 2024 11:35:44 +0900 Subject: [PATCH] Allow method chaining in Session and encryption class For session set/unset/auto write close flag In the encryption classes for setting keys --- src/Create/Session.php | 17 ++++++++++------- src/Security/AsymmetricAnonymousEncryption.php | 10 ++++++---- src/Security/SymmetricEncryption.php | 5 +++-- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Create/Session.php b/src/Create/Session.php index b3f9f77..4440b47 100644 --- a/src/Create/Session.php +++ b/src/Create/Session.php @@ -363,11 +363,12 @@ class Session * set the auto write close flag * * @param bool $flag - * @return void + * @return Session */ - public function setAutoWriteClose(bool $flag): void + public function setAutoWriteClose(bool $flag): Session { $this->auto_write_close = $flag; + return $this; } /** @@ -513,14 +514,15 @@ class Session * * @param string $name array name in _SESSION * @param mixed $value value to set (can be anything) - * @return void + * @return Session */ - public function set(string $name, mixed $value): void + public function set(string $name, mixed $value): Session { $this->checkValidSessionEntryKey($name); $this->restartSession(); $_SESSION[$name] = $value; $this->closeSessionCall(); + return $this; } /** @@ -577,16 +579,17 @@ class Session * unset one _SESSION entry 'name' if exists * * @param string $name _SESSION key name to remove - * @return void + * @return Session */ - public function unset(string $name): void + public function unset(string $name): Session { if (!isset($_SESSION[$name])) { - return; + return $this; } $this->restartSession(); unset($_SESSION[$name]); $this->closeSessionCall(); + return $this; } /** diff --git a/src/Security/AsymmetricAnonymousEncryption.php b/src/Security/AsymmetricAnonymousEncryption.php index 47c357d..46b01e7 100644 --- a/src/Security/AsymmetricAnonymousEncryption.php +++ b/src/Security/AsymmetricAnonymousEncryption.php @@ -256,13 +256,13 @@ class AsymmetricAnonymousEncryption * sets the private key for encryption * * @param string $key_pair Key pair in hex - * @return void + * @return AsymmetricAnonymousEncryption * @throws \UnexpectedValueException key pair empty */ public function setKeyPair( #[\SensitiveParameter] string $key_pair - ) { + ): AsymmetricAnonymousEncryption { if (empty($key_pair)) { throw new \UnexpectedValueException('Key pair cannot be empty'); } @@ -277,6 +277,7 @@ class AsymmetricAnonymousEncryption // check if valid $this->createPublicKey($this->public_key); } + return $this; } /** @@ -308,10 +309,10 @@ class AsymmetricAnonymousEncryption * extract the public key from the key pair * * @param string $public_key Public Key in hex - * @return void + * @return AsymmetricAnonymousEncryption * @throws \UnexpectedValueException public key empty */ - public function setPublicKey(string $public_key) + public function setPublicKey(string $public_key): AsymmetricAnonymousEncryption { if (empty($public_key)) { throw new \UnexpectedValueException('Public key cannot be empty'); @@ -320,6 +321,7 @@ class AsymmetricAnonymousEncryption $this->createPublicKey($public_key); $this->public_key = $public_key; sodium_memzero($public_key); + return $this; } /** diff --git a/src/Security/SymmetricEncryption.php b/src/Security/SymmetricEncryption.php index 91d8c2c..0882621 100644 --- a/src/Security/SymmetricEncryption.php +++ b/src/Security/SymmetricEncryption.php @@ -209,13 +209,13 @@ class SymmetricEncryption * set a new key for encryption * * @param string $key - * @return void + * @return SymmetricEncryption * @throws \UnexpectedValueException key cannot be empty */ public function setKey( #[\SensitiveParameter] string $key - ) { + ): SymmetricEncryption { if (empty($key)) { throw new \UnexpectedValueException('Key cannot be empty'); } @@ -224,6 +224,7 @@ class SymmetricEncryption // set key $this->key = $key; sodium_memzero($key); + return $this; } /**