-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Context class to increase the developer experience when adding cr…
…on jobs
- Loading branch information
Showing
4 changed files
with
103 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\CronBuilder; | ||
|
||
/** | ||
* @implements \ArrayAccess<string, mixed> | ||
* @implements \IteratorAggregate<string, mixed> | ||
*/ | ||
final class Context implements \ArrayAccess, \IteratorAggregate, \Countable | ||
{ | ||
public function __construct( | ||
/** @var array<string, mixed> $context */ | ||
private array $context = [], | ||
) { | ||
} | ||
|
||
public function has(string $key): bool | ||
{ | ||
return array_key_exists($key, $this->context); | ||
} | ||
|
||
public function get(string $key, mixed $default = null): mixed | ||
{ | ||
if (!$this->has($key)) { | ||
return $default; | ||
} | ||
|
||
return $this->context[$key]; | ||
} | ||
|
||
public function set(string $key, mixed $value): void | ||
{ | ||
$this->context[$key] = $value; | ||
} | ||
|
||
public function offsetExists($offset): bool | ||
{ | ||
return $this->has($offset); | ||
} | ||
|
||
public function offsetGet($offset): mixed | ||
{ | ||
return $this->get($offset); | ||
} | ||
|
||
public function offsetSet($offset, $value): void | ||
{ | ||
if (null === $offset) { | ||
throw new \InvalidArgumentException('The offset cannot be null'); | ||
} | ||
|
||
$this->set($offset, $value); | ||
} | ||
|
||
public function offsetUnset($offset): void | ||
{ | ||
unset($this->context[$offset]); | ||
} | ||
|
||
public function getIterator(): \ArrayIterator | ||
{ | ||
return new \ArrayIterator($this->context); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->context); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->context; | ||
} | ||
|
||
public function isEmpty(): bool | ||
{ | ||
return [] === $this->context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters