Compare commits
5 Commits
ee2ddf6e38
...
5fa1832928
| Author | SHA1 | Date | |
|---|---|---|---|
| 5fa1832928 | |||
| 67255faf77 | |||
| 1a0c5bdc30 | |||
| 4dd0e11339 | |||
| 287395aba5 |
@@ -2,7 +2,8 @@
|
||||
"name": "potter/aware",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"php": "^8.4"
|
||||
"php": "^8.4",
|
||||
"potter/cloneable": "dev-main"
|
||||
},
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user