Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/ArrayAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,39 @@ public function count(): int

/**
* This method is required by the interface {@see \ArrayAccess}.
* @param int $offset the offset to check on
* @param mixed $offset the offset to check on
* @return bool
*/
public function offsetExists(int $offset): bool
public function offsetExists($offset): bool
{
return isset($this->data[$offset]);
}

/**
* This method is required by the interface {@see \ArrayAccess}.
* @param int $offset the offset to retrieve element.
* @param mixed $offset the offset to retrieve element.
* @return mixed the element at the offset, null if no element is found at the offset
*/
public function offsetGet(int $offset)
public function offsetGet($offset)
{
return $this->data[$offset] ?? null;
}

/**
* This method is required by the interface {@see \ArrayAccess}.
* @param int $offset the offset to set element
* @param mixed $item the element value
* @param mixed $offset the offset to set element
* @param mixed $value the element value
*/
public function offsetSet(int $offset, $item): void
public function offsetSet($offset, $value): void
{
$this->data[$offset] = $item;
$this->data[$offset] = $value;
}

/**
* This method is required by the interface {@see \ArrayAccess}.
* @param int $offset the offset to unset element
* @param mixed $offset the offset to unset element
*/
public function offsetUnset(int $offset): void
public function offsetUnset($offset): void
{
unset($this->data[$offset]);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/ArrayAccessObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Arrays\Tests;

use ArrayAccess;
use Countable;
use IteratorAggregate;
use Yiisoft\Arrays\ArrayAccessTrait;

class ArrayAccessObject implements IteratorAggregate, ArrayAccess, Countable
{
use ArrayAccessTrait;

public array $data = [
'a' => 1,
'b' => 2,
'c' => 3,
];
}
63 changes: 63 additions & 0 deletions tests/ArrayAccessTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Arrays\Tests;

use ArrayIterator;
use PHPUnit\Framework\TestCase;

class ArrayAccessTraitTest extends TestCase
{

public function testIterator()
{
$object = new ArrayAccessObject();
$iterator = $object->getIterator();
$this->assertInstanceOf(ArrayIterator::class, $iterator);
$this->assertSame($object->data, $iterator->getArrayCopy());
}

public function testCount()
{
$object = new ArrayAccessObject();
$this->assertSame(3, $object->count());
}

public function testOffsetExists()
{
$object = new ArrayAccessObject();
$this->assertTrue($object->offsetExists('a'));
$this->assertFalse($object->offsetExists('x'));
}

public function testOffsetGet()
{
$object = new ArrayAccessObject();
$this->assertSame(1, $object->offsetGet('a'));
$this->assertNull($object->offsetGet('x'));
}

public function testOffsetSet()
{
$object = new ArrayAccessObject();
$object->offsetSet('a', 4);
$object->offsetSet('x', 5);
$this->assertSame([
'a' => 4,
'b' => 2,
'c' => 3,
'x' => 5,
], $object->data);
}

public function testOffsetUnset()
{
$object = new ArrayAccessObject();
$object->offsetUnset('b');
$this->assertSame([
'a' => 1,
'c' => 3,
], $object->data);
}
}