Compare commits

...

5 Commits

Author SHA1 Message Date
5fa1832928 Extend Cloneable 2025-10-25 12:08:09 -04:00
67255faf77 Add without() Method 2025-10-25 12:06:55 -04:00
1a0c5bdc30 Add with() Method 2025-10-25 12:05:46 -04:00
4dd0e11339 Add unset() Method 2025-10-25 12:02:18 -04:00
287395aba5 Require Cloneable 2025-10-25 12:00:39 -04:00
4 changed files with 34 additions and 3 deletions

View File

@@ -2,7 +2,8 @@
"name": "potter/aware",
"type": "library",
"require": {
"php": "^8.4"
"php": "^8.4",
"potter/cloneable": "dev-main"
},
"license": "MIT",
"autoload": {

View File

@@ -4,9 +4,14 @@ declare(strict_types=1);
namespace Potter\Aware;
abstract class AbstractAware implements AwareInterface
use \Potter\Cloneable\Cloneable;
abstract class AbstractAware extends Cloneable implements AwareInterface
{
abstract public function get(string $key): mixed;
abstract public function has(string $key): bool;
abstract protected function set(string $key, mixed $value): mixed;
abstract protected function unset(string $key): void;
abstract public function with(string $key, mixed $value): AwareInterface;
abstract public function without(string $key): AwareInterface;
}

View File

@@ -4,8 +4,12 @@ declare(strict_types=1);
namespace Potter\Aware;
interface AwareInterface
use \Potter\Cloneable\CloneableInterface;
interface AwareInterface extends CloneableInterface
{
public function get(string $key): mixed;
public function has(string $key): bool;
public function with(string $key, mixed $value): self;
public function without(string $key): self;
}

View File

@@ -4,10 +4,14 @@ declare(strict_types=1);
namespace Potter\Aware;
use \Potter\Cloneable\CloneableInterface;
trait AwareTrait
{
private array $aware = [];
abstract public function clone(): CloneableInterface;
final public function get(string $key): mixed
{
return $this->aware[$key];
@@ -22,4 +26,21 @@ trait AwareTrait
{
return $this->aware[$key] = $value;
}
final protected function unset(string $key): void
{
unset($this->aware[$key]);
}
final public function with(string $key, mixed $value): AwareInterface
{
($clone = $this->clone())->set($key, $value);
return $clone;
}
final public function without(string $key): AwareInterface
{
($clone = $this->clone())->unset($key);
return $clone;
}
}