Skip to content

Commit 8b6d678

Browse files
committed
Generate attribute accessors
1 parent 8080709 commit 8b6d678

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

packages/framework/src/Framework/Features/Navigation/NavigationItem.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,51 @@ public function getExtraAttributes(): array
127127
{
128128
return $this->attributes;
129129
}
130+
131+
/**
132+
* Set an extra attribute for the navigation item.
133+
*
134+
* @param string $key
135+
* @param scalar $value
136+
* @return $this
137+
*/
138+
public function setAttribute(string $key, $value): self
139+
{
140+
$this->attributes[$key] = $value;
141+
return $this;
142+
}
143+
144+
/**
145+
* Set multiple extra attributes for the navigation item.
146+
*
147+
* @param array<string, scalar> $attributes
148+
* @return $this
149+
*/
150+
public function setAttributes(array $attributes): self
151+
{
152+
$this->attributes = array_merge($this->attributes, $attributes);
153+
return $this;
154+
}
155+
156+
/**
157+
* Get an extra attribute of the navigation item.
158+
*
159+
* @param string $key
160+
* @param scalar|null $default
161+
* @return scalar|null
162+
*/
163+
public function getAttribute(string $key, $default = null)
164+
{
165+
return $this->attributes[$key] ?? $default;
166+
}
167+
168+
/**
169+
* Get all extra attributes of the navigation item.
170+
*
171+
* @return array<string, scalar>
172+
*/
173+
public function getAttributes(): array
174+
{
175+
return $this->attributes;
176+
}
130177
}

packages/framework/tests/Unit/NavigationItemTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,33 @@ public function testCreateWithAttributes()
337337
$item = NavigationItem::create('foo', 'Test', 500, ['class' => 'active']);
338338
$this->assertSame(['class' => 'active'], $item->getExtraAttributes());
339339
}
340+
341+
public function testSetAttribute()
342+
{
343+
$item = new NavigationItem('foo', 'Test');
344+
$item->setAttribute('class', 'active');
345+
$this->assertSame('active', $item->getAttribute('class'));
346+
}
347+
348+
public function testSetAttributes()
349+
{
350+
$item = new NavigationItem('foo', 'Test');
351+
$item->setAttributes(['class' => 'active', 'id' => 'nav-item']);
352+
$this->assertSame(['class' => 'active', 'id' => 'nav-item'], $item->getAttributes());
353+
}
354+
355+
public function testGetAttribute()
356+
{
357+
$item = new NavigationItem('foo', 'Test', 500, ['class' => 'active']);
358+
$this->assertSame('active', $item->getAttribute('class'));
359+
$this->assertNull($item->getAttribute('id'));
360+
$this->assertSame('default', $item->getAttribute('id', 'default'));
361+
}
362+
363+
public function testGetAttributes()
364+
{
365+
$attributes = ['class' => 'active', 'id' => 'nav-item'];
366+
$item = new NavigationItem('foo', 'Test', 500, $attributes);
367+
$this->assertSame($attributes, $item->getAttributes());
368+
}
340369
}

0 commit comments

Comments
 (0)