Skip to content

Commit 05c6faa

Browse files
committed
Add ArrayAccess support
1 parent 877692c commit 05c6faa

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/Brief.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace AlwaysBlank\Brief;
22

3-
class Brief
3+
class Brief implements \ArrayAccess
44
{
55
/**
66
* A limited list of terms that cannot be used as argument keys.
@@ -922,4 +922,66 @@ public function delete($key) {
922922
}
923923
unset($this->store[$internalKey]);
924924
}
925+
926+
/**
927+
* Whether a offset exists
928+
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
929+
*
930+
* @param mixed $offset <p>
931+
* An offset to check for.
932+
* </p>
933+
*
934+
* @return bool true on success or false on failure.
935+
* </p>
936+
* <p>
937+
* The return value will be casted to boolean if non-boolean was returned.
938+
*/
939+
public function offsetExists( $offset ) {
940+
return $this->get($offset) !== null;
941+
}
942+
943+
/**
944+
* Offset to retrieve
945+
* @link https://php.net/manual/en/arrayaccess.offsetget.php
946+
*
947+
* @param mixed $offset <p>
948+
* The offset to retrieve.
949+
* </p>
950+
*
951+
* @return TValue Can return all value types.
952+
*/
953+
public function offsetGet( $offset ) {
954+
return $this->get($offset);
955+
}
956+
957+
/**
958+
* Offset to set
959+
* @link https://php.net/manual/en/arrayaccess.offsetset.php
960+
*
961+
* @param TKey $offset <p>
962+
* The offset to assign the value to.
963+
* </p>
964+
* @param TValue $value <p>
965+
* The value to set.
966+
* </p>
967+
*
968+
* @return void
969+
*/
970+
public function offsetSet( $offset, $value ) {
971+
$this->set($offset, $value);
972+
}
973+
974+
/**
975+
* Offset to unset
976+
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
977+
*
978+
* @param TKey $offset <p>
979+
* The offset to unset.
980+
* </p>
981+
*
982+
* @return void
983+
*/
984+
public function offsetUnset($offset){
985+
$this->delete($oddffset);
986+
}
925987
}

tests/BriefTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,15 @@ public function testCanDeleteDataFromBrief(): void {
492492
$this->assertNull($NumericBrief->_0);
493493

494494
}
495+
496+
public function testAccessAsArray(): void {
497+
$Brief = Brief::make(['key' => 'value']);
498+
$this->assertEquals('value', $Brief['key']);
499+
$NumericBrief = Brief::make(['value', 'not-value']);
500+
$this->assertEquals('value', $NumericBrief[0]);
501+
$NumericBrief[1] = 'another-value';
502+
$this->assertEquals('another-value', $NumericBrief[1]);
503+
}
495504
}
496505

497506
/**

0 commit comments

Comments
 (0)