-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for properties hooks [Closes #171]
- Loading branch information
Showing
4 changed files
with
244 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\PhpGenerator; | ||
|
||
class PropertyHook | ||
{ | ||
use Traits\AttributeAware; | ||
use Traits\CommentAware; | ||
|
||
private ?string $body = null; | ||
private bool $shortcut = false; // Whether to use the '=>' syntax | ||
private ?Parameter $parameter = null; | ||
private bool $returnReference = false; | ||
|
||
|
||
public function setBody(string $body, bool $shortcut = false): self | ||
{ | ||
$this->body = $body; | ||
$this->shortcut = $shortcut; | ||
return $this; | ||
} | ||
|
||
|
||
public function getBody(): ?string | ||
{ | ||
return $this->body; | ||
} | ||
|
||
|
||
public function isShortcut(): bool | ||
{ | ||
return $this->shortcut; | ||
} | ||
|
||
|
||
/** | ||
* Adds a parameter. If it already exists, it overwrites it. | ||
* @param string $name without $ | ||
*/ | ||
public function setParameter(string $name = 'value'): Parameter | ||
{ | ||
$param = new Parameter($name); | ||
return $this->parameter = $param; | ||
} | ||
|
||
|
||
public function getParameter(): ?Parameter | ||
{ | ||
return $this->parameter; | ||
} | ||
|
||
|
||
/** @internal */ | ||
public function getParameters(): array | ||
{ | ||
return $this->parameter ? [$this->parameter] : []; | ||
} | ||
|
||
|
||
public function setReturnReference(bool $state = true): static | ||
{ | ||
$this->returnReference = $state; | ||
return $this; | ||
} | ||
|
||
|
||
public function getReturnReference(): bool | ||
{ | ||
return $this->returnReference; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\PhpGenerator - PHP 8.4 property hooks for classes | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\PhpGenerator\ClassType; | ||
use Nette\PhpGenerator\PhpFile; | ||
use Nette\PhpGenerator\PropertyHook; | ||
use Nette\PhpGenerator\PsrPrinter; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
$file = new PhpFile; | ||
$file->setStrictTypes(); | ||
|
||
$class = new ClassType('Locale'); | ||
|
||
$class->addProperty('languageCode') | ||
->setType('string') | ||
->setPublic(); | ||
|
||
$countryCodeSetHookClosure = (new PropertyHook) | ||
->setBody('$this->countryCode = strtoupper($countryCode);'); | ||
|
||
$countryCodeSetHookClosure->setParameter('countryCode')->setType('string'); | ||
|
||
$class->addProperty('countryCode') | ||
->setType('string') | ||
->setValue('AA') | ||
->setPublic() | ||
->setSetHook($countryCodeSetHookClosure); | ||
|
||
$combinedCodeGetHookClosure = (new PropertyHook) | ||
->setBody('return \sprintf("%s_%s", $this->languageCode, $this->countryCode);'); | ||
|
||
$combinedCodeSetHookClosure = (new PropertyHook) | ||
->setBody('[$this->languageCode, $this->countryCode] = explode(\'_\', $value, 2);'); | ||
|
||
$combinedCodeSetHookClosure->setParameter('value')->setType('string'); | ||
|
||
$class->addProperty('combinedCode') | ||
->setType('string') | ||
->setPublic() | ||
->setGetHook($combinedCodeGetHookClosure) | ||
->setSetHook($combinedCodeSetHookClosure); | ||
|
||
$expected = <<<'PHP' | ||
class Locale | ||
{ | ||
public string $languageCode; | ||
public string $countryCode = 'AA' { | ||
set (string $countryCode) { | ||
$this->countryCode = strtoupper($countryCode); | ||
} | ||
} | ||
public string $combinedCode { | ||
set (string $value) { | ||
[$this->languageCode, $this->countryCode] = explode('_', $value, 2); | ||
} | ||
get { | ||
return \sprintf("%s_%s", $this->languageCode, $this->countryCode); | ||
} | ||
} | ||
} | ||
PHP; | ||
|
||
same(rtrim($expected), rtrim((new PsrPrinter)->printClass($class))); |