Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: magic methods on component not being called #151

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function refresh() {}
## Properties
Assign properties including a lifecycle ```updating``` and ```updated``` method.
```php
public $foo = 'bar'
public $foo = 'bar';

/**
* OPTIONAL METHOD: Gets executed right before the property gets assigned.
Expand Down
78 changes: 66 additions & 12 deletions src/Model/Action/Type/Magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Magewirephp\Magewire\Model\Action\Type;

use Exception;
use Magewirephp\Magewire\Component;
use Magewirephp\Magewire\Exception\ComponentException;
use Magewirephp\Magewire\Helper\Property as PropertyHelper;
Expand Down Expand Up @@ -43,22 +44,49 @@ public function toggle(string $property, $component): void
*/
public function set(string $property, $value, Component $component): void
{
if ($this->propertyHelper->containsDots($property)) {
$transform = $this->propertyHelper->transformDots($property, $value, $component);

$property = $transform['property'];
$value = $transform['data'];
}

// Transform a magic property value.
if (is_string($value)
try {
// Transform a magic property value.
if (is_string($value)
&& strrpos($value, '$') === 0
&& ($value = ltrim($value, '$'))
&& array_key_exists($value, $component->getPublicProperties())) {
$value = $component->{$value};
}
$value = $component->{$value};
}

$nested = $this->propertyHelper->containsDots($property);
$transform = [];

if ($nested) {
$transform = $this->propertyHelper->transformDots($property, $value, $component);
$property = $transform['property'];
$value = $transform['data'];
}

$component->{$property} = $value;
// Try to run existing pre-assignment methods if they exist.
$value = $this->updating($component, $property, $value);

if ($nested) {
$component->{ $transform['property'] } = $this->propertyHelper->assignViaDots($transform['path'], $value, $component->{ $transform['property'] });
} else {
$component->{ $property } = $value;
}

// Try to run post-assignment methods if they exist.
$value = $this->updated($component, $property, $value);

if ($nested) {
$component->{ $transform['property'] } = $this->propertyHelper->assignViaDots($transform['path'], $value, $component->{ $transform['property'] });
} else {
$component->{ $property } = $value;
}
} catch (Exception $exception) {
$this->logger->critical(
sprintf('Magewire: Something went wrong while syncing property "%s" onto component "%s"', $property, $component->name),
['exception' => $exception]
);

return;
}
}

/**
Expand All @@ -67,4 +95,30 @@ public function set(string $property, $value, Component $component): void
public function refresh(): void //phpcs:ignore
{
}

private function updating(Component $component, string $property, $value)
{
$methods = ['updating' . str_replace(' ', '', ucwords(str_replace(['-', '_', '.'], ' ', $property))), 'updating'];

foreach ($methods as $method) {
if (method_exists($component, $method)) {
$value = $component->{$method}($value, $property);
}
}

return $value;
}

private function updated(Component $component, string $property, $value)
{
$methods = ['updated', 'updated' . str_replace(' ', '', ucwords(str_replace(['-', '_', '.'], ' ', $property)))];

foreach ($methods as $method) {
if (method_exists($component, $method)) {
$value = $component->{$method}($value, $property);
}
}

return $value;
}
}