Skip to content

Commit

Permalink
feat: Added traits subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed May 26, 2024
0 parents commit b99bcb6
Show file tree
Hide file tree
Showing 11 changed files with 749 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.releaserc export-ignore
/composer.lock export-ignore
Empty file added .github/renovate.json
Empty file.
40 changes: 40 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI
on:
push:
branches:
- master
- develop
- alpha
- beta

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.OBLAKBOT_PAT }}
- name: Import GPG key
uses: crazy-max/ghaction-import-gpg@v6
id: gpg
with:
gpg_private_key: ${{ secrets.OBLAKBOT_GPG_KEY }}
passphrase: ${{ secrets.OBLAKBOT_GPG_PASS }}
git_config_global: true
git_user_signingkey: true
git_commit_gpgsign: true
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v4
with:
extra_plugins: |
@semantic-release/github
@semantic-release/exec
env:
GIT_AUTHOR_NAME: ${{ steps.gpg.outputs.name}}
GIT_AUTHOR_EMAIL: ${{ steps.gpg.outputs.email}}
GIT_COMMITTER_NAME: ${{ steps.gpg.outputs.name}}
GIT_COMMITTER_EMAIL: ${{ steps.gpg.outputs.email}}
GITHUB_TOKEN: ${{ secrets.OBLAKBOT_PAT }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
vendor
27 changes: 27 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"branches": [
"master"
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
[
"@semantic-release/exec",
{
"prepareCmd": "zip -r '/tmp/release.zip' *.php README.md"
}
],
[
"@semantic-release/github",
{
"assets": [
{
"path": "/tmp/release.zip",
"name": "xwp-helper-traits-${nextRelease.version}.zip",
"label": "xWP Helper Traits v${nextRelease.version}"
}
]
}
]
]
}
222 changes: 222 additions & 0 deletions Array_Access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php //phpcs:disable WordPress.NamingConventions
namespace XWP\Helper\Traits;

/**
* Allows a class to be accessed as an array.
*
* @template TKey
* @template TValue
* @template-covariant TValue
* @template-implements \ArrayAccess<TKey, TValue>, \Iterator<TKey, TValue>, \Countable, \JsonSerializable
* @phpstan-require-implements \ArrayAccess<TKey, TValue>, \Iterator<TKey, TValue>, \Countable, \JsonSerializable
*/
trait Array_Access {
/**
* Traversible data array.
*
* @var array<int|string, mixed>
*/
protected array $arr_data = array();

/**
* Array of keys for the data array.
*
* @var array<string|int>
*/
protected array $arr_data_keys = array();

/**
* Current iterator position.
*
* @var int
*/
protected int $arr_position = 0;

/**
* Counts the number of items in the data array.
*
* Used by the Countable interface.
*
* @return int<0, max>
*/
public function count(): int {
return \count( $this->arr_data_keys );
}

/**
* Returns the current item in the data array.
*
* Used by the Iterator interface.
*
* @return TValue Can return any type.
*/
public function current(): mixed {
return $this->arr_data[ $this->arr_data_keys[ $this->arr_position ] ];
}

/**
* Returns the key of the current item in the data array.
*
* Used by the Iterator interface.
*
* @return TKey|null TKey on success, or null on failure.
*/
public function key(): mixed {
return $this->arr_data_keys[ $this->arr_position ];
}

/**
* Moves the iterator to the next item in the data array.
*
* Used by the Iterator interface.
*
* @return void
*/
public function next(): void {
++$this->arr_position;
}

/**
* Resets the iterator to the first item in the data array.
*
* Used by the Iterator interface.
*
* @return void
*/
public function rewind(): void {
$this->arr_position = 0;
}

/**
* Checks if the current iterator position is valid.
*
* Used by the Iterator interface.
*
* @return bool
*/
public function valid(): bool {
return isset( $this->arr_data_keys[ $this->arr_position ] );
}

/**
* Assigns a value to the specified offset.
*
* Used by the ArrayAccess interface.
*
* @param TKey $offset The offset to assign the value to.
* @param TValue $value The value to set.
* @return void
*/
public function offsetSet( $offset, $value ): void {
if ( \is_null( $offset ) ) {
$this->arr_data[] = $value;
$this->arr_data_keys[] = \array_key_last( $this->arr_data );

return;
}

$this->arr_data[ $offset ] = $value;

if ( \in_array( $offset, $this->arr_data_keys, true ) ) {
return;
}

$this->arr_data_keys[] = $offset;
}

/**
* Returns the value at the specified offset.
*
* Used by the ArrayAccess interface.
*
* @param TKey $offset The offset to retrieve.
* @return TValue Can return any type.
*/
public function &offsetGet( $offset ): mixed {
$this->arr_data[ $offset ] ??= array();

return $this->arr_data[ $offset ];
}

/**
* Checks if the specified offset exists.
*
* Used by the ArrayAccess interface.
*
* @param TKey $offset The offset to check.
* @return bool
*/
public function offsetExists( $offset ): bool {
return isset( $this->arr_data[ $offset ] );
}

/**
* Unsets the value at the specified offset.
*
* Used by the ArrayAccess interface.
*
* @param TKey $offset The offset to unset.
* @return void
*/
public function offsetUnset( $offset ): void {
unset( $this->arr_data[ $offset ] );
unset( $this->arr_data_keys[ \array_search( $offset, $this->arr_data_keys, true ) ] );

$this->arr_data_keys = \array_values( $this->arr_data_keys );
}

/**
* Returns the data array as an array.
*
* @return array<TKey, TValue>
*/
public function __debugInfo() {
return $this->arr_data;
}

/**
* Serialize the data array.
*
* @return array<TKey, TValue>
*/
public function __serialize() {
return $this->arr_data;
}

/**
* Unserialize the data array.
*
* @param array<TKey, TValue> $data The data to unserialize.
* @return void
*/
public function __unserialize($data) {
foreach ($data as $k => $v) {
$this[$k] = $v;
}
}

/**
* Json Serialize the data array.
*
* @return array<TKey, TValue>
*/
public function jsonSerialize(): mixed {
return $this->arr_data;
}

/**
* Set the state of the object.
*
* @param array<TKey, TValue> $data The data to set.
* @return static
*/
public static function __set_state( array $data): static {
$obj = new static();

foreach ($data['arr_data'] as $k => $v) {
$obj[$k] = $v;
}

return $obj;
}
}
Loading

0 comments on commit b99bcb6

Please sign in to comment.