Skip to content

Commit 3ecd1bd

Browse files
committed
feat: enhance Collection class with addArray method and update add method return type
1 parent 57abaed commit 3ecd1bd

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

debian/changelog

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
php-vitexsoftware-ease-core (1.50.0) UNRELEASED; urgency=medium
1+
php-vitexsoftware-ease-core (1.50.1) UNRELEASED; urgency=medium
2+
3+
* enhance Collection class with addArray method
4+
5+
-- vitex <info@vitexsoftware.cz> Tue, 17 Feb 2026 18:39:40 +0100
6+
7+
php-vitexsoftware-ease-core (1.50.0) unstable; urgency=medium
28

39
* EaseURI functions added
410

5-
-- vitex <info@vitexsoftware.cz> Fri, 30 Jan 2026 22:46:09 +0100
11+
-- vitex <info@vitexsoftware.cz> Tue, 17 Feb 2026 18:39:21 +0100
612

713
php-vitexsoftware-ease-core (1.49.1) unstable; urgency=medium
814

src/Ease/Collection.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ public function __construct(string $class)
5454

5555
/**
5656
* @param T $item
57+
*
58+
* @return self
5759
*/
58-
public function add(object $item): void
60+
public function add(object $item): self
5961
{
6062
if (!$item instanceof $this->class) {
6163
throw new \InvalidArgumentException(
@@ -64,6 +66,19 @@ public function add(object $item): void
6466
}
6567

6668
$this->items[] = $item;
69+
return $this;
70+
}
71+
72+
/**
73+
* Add New item defined by its pure data
74+
*
75+
* @param array $data
76+
*
77+
* @return self
78+
*/
79+
public function addArray(array $data): self {
80+
$this->add(new $this->class($data));
81+
return $this;
6782
}
6883

6984
/**

tests/src/Ease/AtomTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#[CoversClass(Atom::class)]
3636
class AtomTest extends \PHPUnit\Framework\TestCase
3737
{
38-
protected \Ease\Atom $object;
38+
protected $object;
3939

4040
/**
4141
* Sets up the fixture, for example, opens a network connection.

tests/src/Ease/CollectionTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testTypeValidation(): void
6767

6868
// Invalid type should throw exception
6969
$this->expectException(\InvalidArgumentException::class);
70-
$this->expectExceptionMessage('Item must be an instance of stdClass');
70+
$this->expectExceptionMessage('Item must be instance of stdClass, Exception given');
7171

7272
$bus->add(new \Exception());
7373
}
@@ -102,4 +102,28 @@ public function testGetItems(): void
102102
$this->assertSame($item1, $items[0]);
103103
$this->assertSame($item2, $items[1]);
104104
}
105+
106+
public function testAddArray(): void
107+
{
108+
// Anonymous class that accepts an array in constructor
109+
$proto = new class(['a' => 1]) {
110+
public array $data;
111+
112+
public function __construct(array $data)
113+
{
114+
$this->data = $data;
115+
}
116+
};
117+
118+
$className = \get_class($proto);
119+
120+
$collection = new Collection($className);
121+
122+
$collection->addArray(['a' => 1, 'b' => 2]);
123+
124+
$items = $collection->getItems();
125+
126+
$this->assertCount(1, $items);
127+
$this->assertSame(['a' => 1, 'b' => 2], $items[0]->data);
128+
}
105129
}

0 commit comments

Comments
 (0)