Skip to content

Commit a467853

Browse files
committed
add setMetaAttribute
1 parent 512331b commit a467853

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## v4.2.0
8+
9+
### Added
10+
11+
- Add method to trait `Metable`: `setMetaAttribute`
12+
713
## v4.1.0
814

915
### Added

src/Traits/Metable.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,32 @@ public function meta(): array
3535
*
3636
* @return mixed
3737
*/
38-
public function metaAttribute(string $key, $default = null)
38+
public function metaAttribute(string $key, mixed $default = null)
3939
{
4040
return Arr::get($this->meta, $key, $default);
4141
}
4242

43+
/**
44+
* @param string $key
45+
* @param mixed $value
46+
*
47+
* @return $this
48+
*/
49+
public function setMetaAttribute(string $key, mixed $value): static
50+
{
51+
Arr::set($this->meta, $key, $value);
52+
53+
return $this;
54+
}
55+
4356
/**
4457
* Set additional meta information for the element.
4558
*
4659
* @param array $meta
4760
*
4861
* @return $this
4962
*/
50-
public function withMeta(array $meta): self
63+
public function withMeta(array $meta): static
5164
{
5265
$this->meta = Arr::merge($this->meta, $meta);
5366

tests/traits/MetableTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,43 @@ public function testRecursive(): void
8080
]
8181
);
8282
}
83+
84+
public function testSetMetaAttribute(): void
85+
{
86+
$instance = new MetableClassTest();
87+
88+
$instance->setMetaAttribute('test', 123);
89+
static::assertEquals($instance->metaAttribute('test'), 123);
90+
91+
$instance->setMetaAttribute('params.id', 1);
92+
$instance->setMetaAttribute('params.isBool', true);
93+
$instance->setMetaAttribute('params.string', 'test');
94+
static::assertEquals(1, $instance->metaAttribute('params.id'));
95+
static::assertEquals(true, $instance->metaAttribute('params.isBool'));
96+
static::assertEquals('test', $instance->metaAttribute('params.string'));
97+
98+
static::assertEquals(
99+
[
100+
'id' => 1,
101+
'isBool' => true,
102+
'string' => 'test',
103+
],
104+
$instance->metaAttribute('params')
105+
);
106+
107+
static::assertEquals(
108+
[
109+
'test' => 123,
110+
'params' =>
111+
[
112+
'id' => 1,
113+
'isBool' => true,
114+
'string' => 'test',
115+
],
116+
],
117+
$instance->meta()
118+
);
119+
}
83120
}
84121

85122
/**

0 commit comments

Comments
 (0)