Skip to content

Commit

Permalink
[4.4][RFC] Rename Event pre-processing methods from set/get to onSet/…
Browse files Browse the repository at this point in the history
…onGet (joomla#41722)

* Rename Event pre-processing events from set/get to onSet/onGet

* Apply suggestions from code review

Co-authored-by: Richard Fath <[email protected]>

---------

Co-authored-by: Richard Fath <[email protected]>
Co-authored-by: Allon Moritz <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2023
1 parent 60c0bb4 commit d173dfe
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions libraries/src/Event/AbstractEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ public function __construct(string $name, array $arguments = [])
}

/**
* Get an event argument value. It will use a getter method if one exists. The getters have the signature:
* Get an event argument value.
* It will use a pre-processing method if one exists. The method has the signature:
*
* get<ArgumentName>($value): mixed
* onGet<ArgumentName>($value): mixed
*
* where:
*
Expand Down Expand Up @@ -157,21 +158,37 @@ public function getArgument($name, $default = null)
);
}

$methodName = 'get' . ucfirst($name);
// Look for the method for the value pre-processing/validation
$ucfirst = ucfirst($name);
$methodName1 = 'onGet' . $ucfirst;
$methodName2 = 'get' . $ucfirst;

$value = parent::getArgument($name, $default);

if (method_exists($this, $methodName)) {
return $this->{$methodName}($value);
if (method_exists($this, $methodName1)) {
return $this->{$methodName1}($value);
} elseif (method_exists($this, $methodName2)) {
@trigger_error(
sprintf(
'Use method "%s" for value pre-processing is deprecated, and will not work in Joomla 6. Use "%s" instead. Event %s',
$methodName2,
$methodName1,
\get_class($this)
),
E_USER_DEPRECATED
);

return $this->{$methodName2}($value);
}

return $value;
}

/**
* Add argument to event. It will use a setter method if one exists. The setters have the signature:
* Add argument to event.
* It will use a pre-processing method if one exists. The method has the signature:
*
* set<ArgumentName>($value): mixed
* onSet<ArgumentName>($value): mixed
*
* where:
*
Expand Down Expand Up @@ -204,10 +221,25 @@ public function setArgument($name, $value)
);
}

$methodName = 'set' . ucfirst($name);
// Look for the method for the value pre-processing/validation
$ucfirst = ucfirst($name);
$methodName1 = 'onSet' . $ucfirst;
$methodName2 = 'set' . $ucfirst;

if (method_exists($this, $methodName1)) {
$value = $this->{$methodName1}($value);
} elseif (method_exists($this, $methodName2)) {
@trigger_error(
sprintf(
'Use method "%s" for value pre-processing is deprecated, and will not work in Joomla 6. Use "%s" instead. Event %s',
$methodName2,
$methodName1,
\get_class($this)
),
E_USER_DEPRECATED
);

if (method_exists($this, $methodName)) {
$value = $this->{$methodName}($value);
$value = $this->{$methodName2}($value);
}

return parent::setArgument($name, $value);
Expand Down

0 comments on commit d173dfe

Please sign in to comment.