Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
✨ Add direct get/set class properties
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Aug 15, 2022
1 parent 20d67bb commit b88cbcc
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 27 deletions.
51 changes: 43 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ class MyCapsule implements Encapsulable {
// The action to perform after setted the property
echo 'Setted property...' . PHP_EOL;
}

protected mixed $myProperty = 'My default value';

public function gettingMyProperty(): void {
// The action to perform before getting the property
echo 'Getting my property...' . PHP_EOL;
}

public function gettedMyProperty(): void {
// The action to perform after getted the property
echo 'Getted my property...' . PHP_EOL;
}

public function settingMyProperty(mixed $value): void {
// The action to perform before setting the property
echo 'Setting my property...' . PHP_EOL;
}

public function settedMyProperty(mixed $value): void {
// The action to perform after setted the property
echo 'Setted my property...' . PHP_EOL;
}
}
```

Expand All @@ -74,9 +96,19 @@ Get/set a property:
<?php

$capsule = new MyCapsule();
echo "Property: $capsule->property" . PHP_EOL;
$capsule->property = 'new value';
echo "Property: $capsule->property" . PHP_EOL;
echo $capsule->property . PHP_EOL;
echo $capsule->myProperty . PHP_EOL;

unset($capsule->property);
$capsule->myProperty = 'my value';

if (isset($capsule->property)) {
echo "Property: $capsule->property" . PHP_EOL;
}

if (isset($capsule->myProperty)) {
echo "My property: $capsule->myProperty" . PHP_EOL;
}
```

The above code will output:
Expand All @@ -85,11 +117,14 @@ The above code will output:
Getting property...
Getted property...
default value
Setting property...
Setted property...
Getting property...
Getted property...
new value
Getting my property...
Getted my property...
My default value
Setting my property...
Setted my property...
Getting my property...
Getted my property...
My property: my value
```

## License
Expand Down
22 changes: 22 additions & 0 deletions app/Capsule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,26 @@ public function settedProperty(mixed $value): void {
// The action to perform after setted the property
echo 'Setted property...' . PHP_EOL;
}

protected mixed $myProperty = 'My default value';

public function gettingMyProperty(): void {
// The action to perform before getting the property
echo 'Getting my property...' . PHP_EOL;
}

public function gettedMyProperty(): void {
// The action to perform after getted the property
echo 'Getted my property...' . PHP_EOL;
}

public function settingMyProperty(mixed $value): void {
// The action to perform before setting the property
echo 'Setting my property...' . PHP_EOL;
}

public function settedMyProperty(mixed $value): void {
// The action to perform after setted the property
echo 'Setted my property...' . PHP_EOL;
}
}
15 changes: 13 additions & 2 deletions capsule
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ use App\Capsule;
require_once __DIR__ . '/vendor/autoload.php';

$capsule = new Capsule();

echo $capsule->property . PHP_EOL;
$capsule->property = 'new value';
echo $capsule->property . PHP_EOL;
echo $capsule->myProperty . PHP_EOL;

unset($capsule->property);
$capsule->myProperty = 'my value';

if (isset($capsule->property)) {
echo "Property: $capsule->property" . PHP_EOL;
}

if (isset($capsule->myProperty)) {
echo "My property: $capsule->myProperty" . PHP_EOL;
}
25 changes: 15 additions & 10 deletions src/Getter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@

trait Getter {
public function __get(string $name): mixed {
if (method_exists($this, $before_method = 'getting' . ucfirst($name))) {
$this->$before_method();
}
if (method_exists($this, $method = 'get' . ucfirst($name))) {
if (method_exists($this, $before_method = 'getting' . ucfirst($name))) {
$this->$before_method();
}
$result = $this->$method();
if (method_exists($this, $after_method = 'getted' . ucfirst($name))) {
$this->$after_method();
}
return $result;
}
throw new \RuntimeException('Property ' . $name . ' not found');
else if (property_exists($this, $name)) {
$result = $this->$name;
}
else {
throw new \RuntimeException('Property ' . $name . ' not found');
}
if (method_exists($this, $after_method = 'getted' . ucfirst($name))) {
$this->$after_method();
}
return $result;
}

public function __isset(string $name): bool {
if (method_exists($this, $method = 'get' . ucfirst($name))) {
if (method_exists($this, $method = 'get' . ucfirst($name)) && $this->$method() !== null) {
return true;
}
return false;
return property_exists($this, $name) && isset($this->$name);
}
}
21 changes: 14 additions & 7 deletions src/Setter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@

trait Setter {
public function __set(string $name, mixed $value): void {
if (method_exists($this, $before_method = 'setting' . ucfirst($name))) {
$this->$before_method($name, $value);
}
if (method_exists($this, $method = 'set' . ucfirst($name))) {
if (method_exists($this, $before_method = 'setting' . ucfirst($name))) {
$this->$before_method($name, $value);
}
$this->$method($value);
if (method_exists($this, $after_method = 'setted' . ucfirst($name))) {
$this->$after_method($name, $value);
}
} else {
}
elseif (property_exists($this, $name)) {
$this->$name = $value;
}
else {
throw new \RuntimeException('Property ' . $name . ' not found');
}
if (method_exists($this, $after_method = 'setted' . ucfirst($name))) {
$this->$after_method($name, $value);
}
}

public function __unset(string $name): void {
if (method_exists($this, $method = 'set' . ucfirst($name))) {
$this->$method(null);
}
elseif (property_exists($this, $name)) {
unset($this->$name);
}
}
}

0 comments on commit b88cbcc

Please sign in to comment.