Skip to content

Commit

Permalink
Array notation too
Browse files Browse the repository at this point in the history
  • Loading branch information
darrencoutts118 committed Jan 13, 2024
1 parent 6e3b426 commit 687d7f1
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 3 deletions.
61 changes: 59 additions & 2 deletions src/ComponentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AppKit\UI;

use ArrayAccess;
use BadMethodCallException;
use Closure;
use Illuminate\Support\Arr;
Expand All @@ -12,7 +13,7 @@
use ReflectionMethod;
use RuntimeException;

class ComponentBuilder
class ComponentBuilder implements ArrayAccess
{
use ForwardsCalls;

Expand Down Expand Up @@ -304,6 +305,17 @@ public function getAttributes()
return $this->element()->getAttributes();
}

/**
* Get an attribute value on the default element via magic parameter
*
* @param string $name
* @return mixed
*/
public function __get(string $name): mixed
{
return $this->element()->__get($name);
}

/**
* Set an attribute value on the default element via magic parameter
*
Expand All @@ -317,7 +329,7 @@ public function __set(string $name, $value): void
}

/**
* Unset an attribute value via a magic parameter
* Unset an attribute value on the default element via a magic parameter
*
* @param string $name
* @return void
Expand All @@ -327,6 +339,51 @@ public function __unset(string $name): void
$this->element()->__unset($name);
}

/**
* Set an attribute on the default element via array notation
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->element()->offsetSet($offset, $value);
}

/**
* Get an attribute on the default element via array notation
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet(mixed $offset): mixed
{
return $this->element()->offsetGet($offset);
}

/**
* Check if an attribute exists via array notation
*
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return $this->element()->offsetExists($offset);
}

/**
* Remove an attribute on the default element via array notation
*
* @param mixed $offset
* @return void
*/
public function offsetUnset(mixed $offset): void
{
$this->element()->offsetUnset($offset);
}

private function generateMagicMethodRegexCapture(string $captureGroup, array $values, array $triggers = [])
{
// we need to build up the regex that we are going to use to parse the method
Expand Down
63 changes: 62 additions & 1 deletion src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace AppKit\UI;

use ArrayAccess;
use BadMethodCallException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Illuminate\View\ComponentAttributeBag;

class Element
class Element implements ArrayAccess
{
use ForwardsCalls;

Expand Down Expand Up @@ -183,6 +184,21 @@ public function getAttributeBag()
return $this->attributeBag;
}

/**
* Get an attribute value via magic parameter
*
* @param string $name
* @return mixed
*/
public function __get(string $name): mixed
{
// turn the name of the attribute into kebab case
$name = Str::of($name)->kebab()->__toString();

// get the value
return $this->getAttribute($name);
}

/**
* Set an attribute value via magic parameter
*
Expand Down Expand Up @@ -220,6 +236,51 @@ public function __unset(string $name): void
$this->removeAttribute($name);
}

/**
* Set an attribute via array notation
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->setAttribute($offset, $value);
}

/**
* Get an attribute via array notation
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet(mixed $offset): mixed
{
return $this->getAttribute($offset);
}

/**
* Check if an attribute exists via array notation
*
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return $this->getAttribute($offset) != '';
}

/**
* Remove an attribute via array notation
*
* @param mixed $offset
* @return void
*/
public function offsetUnset(mixed $offset): void
{
$this->removeAttribute($offset);
}

/**
* Forward calls to the underlying attribute bag
*
Expand Down
46 changes: 46 additions & 0 deletions tests/AttributeBagAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,49 @@
// check that the classes are correct
expect($componentBuilder->getAttributes())->not()->toHaveKey('aria-label');
});

it('can access an attribute value via parameter', function () {
// create a new component builder
$componentBuilder = createComponentBuilder(['foo' => 'bar']);

// check that the classes are correct
expect($componentBuilder->foo)->toBe('bar');
});

it('can access a kebab attribute value via parameter', function () {
// create a new component builder
$componentBuilder = createComponentBuilder(['aria-label' => 'bar']);

// check that the classes are correct
expect($componentBuilder->ariaLabel)->toBe('bar');
});

it('can add an attribute via array notation', function () {
// create a new component builder
$componentBuilder = createComponentBuilder();

// add the class to the component builder
$componentBuilder['foo'] = 'bar';

// check that the classes are correct
expect($componentBuilder->getAttributes())->toHaveKey('foo', 'bar');
});

it('can remove an attribute via array notation', function () {
// create a new component builder
$componentBuilder = createComponentBuilder(['foo' => 'bar']);

// add the class to the component builder
unset($componentBuilder['foo']);

// check that the classes are correct
expect($componentBuilder->getAttributes())->not()->toHaveKey('foo');
});

it('can access an attribute value via array notation', function () {
// create a new component builder
$componentBuilder = createComponentBuilder(['foo' => 'bar']);

// check that the classes are correct
expect($componentBuilder['foo'])->toBe('bar');
});

0 comments on commit 687d7f1

Please sign in to comment.