Skip to content

Commit

Permalink
Add ArrayAccess support
Browse files Browse the repository at this point in the history
  • Loading branch information
alwaysblank committed Oct 28, 2022
1 parent 877692c commit 05c6faa
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/Brief.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace AlwaysBlank\Brief;

class Brief
class Brief implements \ArrayAccess
{
/**
* A limited list of terms that cannot be used as argument keys.
Expand Down Expand Up @@ -922,4 +922,66 @@ public function delete($key) {
}
unset($this->store[$internalKey]);
}

/**
* Whether a offset exists
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param mixed $offset <p>
* An offset to check for.
* </p>
*
* @return bool true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
*/
public function offsetExists( $offset ) {
return $this->get($offset) !== null;
}

/**
* Offset to retrieve
* @link https://php.net/manual/en/arrayaccess.offsetget.php
*
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
*
* @return TValue Can return all value types.
*/
public function offsetGet( $offset ) {
return $this->get($offset);
}

/**
* Offset to set
* @link https://php.net/manual/en/arrayaccess.offsetset.php
*
* @param TKey $offset <p>
* The offset to assign the value to.
* </p>
* @param TValue $value <p>
* The value to set.
* </p>
*
* @return void
*/
public function offsetSet( $offset, $value ) {
$this->set($offset, $value);
}

/**
* Offset to unset
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
*
* @param TKey $offset <p>
* The offset to unset.
* </p>
*
* @return void
*/
public function offsetUnset($offset){
$this->delete($oddffset);
}
}
9 changes: 9 additions & 0 deletions tests/BriefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,15 @@ public function testCanDeleteDataFromBrief(): void {
$this->assertNull($NumericBrief->_0);

}

public function testAccessAsArray(): void {
$Brief = Brief::make(['key' => 'value']);
$this->assertEquals('value', $Brief['key']);
$NumericBrief = Brief::make(['value', 'not-value']);
$this->assertEquals('value', $NumericBrief[0]);
$NumericBrief[1] = 'another-value';
$this->assertEquals('another-value', $NumericBrief[1]);
}
}

/**
Expand Down

0 comments on commit 05c6faa

Please sign in to comment.