This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 20d67bb
Showing
12 changed files
with
302 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
/composer.lock |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 SIKessEm | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,102 @@ | ||
<div align="center"><a href="https://sikessem.com/" title="SIKessEm"><img src="https://github.com/sikessem/sikessem/blob/main/SIKessEm-logo.png" alt="SIKessEm logo"/></a></div> | ||
|
||
*** | ||
|
||
# Get/set PHP class properties dynamically | ||
|
||
Capsule allows to get/set a property thanks to the magic methods of PHP by defining the getter/setter of this property. | ||
Capsule also allows you to perform actions before getting/setting and/or after getted/setted a property. | ||
|
||
|
||
## Installation | ||
|
||
Using [composer](https://getcomposer.org/), you can install Capsule with the following command: | ||
|
||
```bash | ||
composer require sikessem/capsule | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
Encapsulate a property: | ||
|
||
```php | ||
<?php | ||
|
||
use Sikessem\Capsule\{Encapsulable, Encapsuler}; | ||
|
||
class MyCapsule implements Encapsulable { | ||
use Encapsuler; | ||
|
||
// The property to encapsulate must be defined if it has no getter/setter method. | ||
private mixed $property = 'default value'; | ||
|
||
// No need to define this method if the property to get is defined. | ||
public function getProperty(): mixed { | ||
return $this->property; | ||
} | ||
|
||
// No need to define this method if it has no action to perform before getting the property | ||
public function gettingProperty(): void { | ||
// The action to perform before getting the property | ||
echo 'Getting property...' . PHP_EOL; | ||
} | ||
|
||
// No need to define this method if it has no action to perform after getted the property | ||
public function gettedProperty(): void { | ||
// The action to perform after getted the property | ||
echo 'Getted property...' . PHP_EOL; | ||
} | ||
|
||
// No need to define this method if the property to set is defined. | ||
public function setProperty(mixed $value) { | ||
$this->property = $value; | ||
} | ||
|
||
// No need to define this method if it has no action to perform before setting the property | ||
public function settingProperty(mixed $value): void { | ||
// The action to perform before setting the property | ||
echo 'Setting property...' . PHP_EOL; | ||
} | ||
|
||
// No need to define this method if it has no action to perform after setted the property | ||
public function settedProperty(mixed $value): void { | ||
// The action to perform after setted the property | ||
echo 'Setted property...' . PHP_EOL; | ||
} | ||
} | ||
``` | ||
|
||
Get/set a property: | ||
|
||
```php | ||
<?php | ||
|
||
$capsule = new MyCapsule(); | ||
echo "Property: $capsule->property" . PHP_EOL; | ||
$capsule->property = 'new value'; | ||
echo "Property: $capsule->property" . PHP_EOL; | ||
``` | ||
|
||
The above code will output: | ||
|
||
```bash | ||
Getting property... | ||
Getted property... | ||
default value | ||
Setting property... | ||
Setted property... | ||
Getting property... | ||
Getted property... | ||
new value | ||
``` | ||
|
||
## License | ||
|
||
This library is distributed under the [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT) | ||
|
||
|
||
## Security Reports | ||
|
||
Please send any sensitive issue to [[email protected]](mailto:[email protected]). Thanks! |
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,46 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Sikessem\Capsule\{Encapsulable, Encapsuler}; | ||
|
||
class Capsule implements Encapsulable { | ||
use Encapsuler; | ||
|
||
// The property to encapsulate must be defined if it has no getter/setter method. | ||
private mixed $property = 'default value'; | ||
|
||
// No need to define this method if the property to get is defined. | ||
public function getProperty(): mixed { | ||
return $this->property; | ||
} | ||
|
||
// No need to define this method if it has no action to perform before getting the property | ||
public function gettingProperty(): void { | ||
// The action to perform before getting the property | ||
echo 'Getting property...' . PHP_EOL; | ||
} | ||
|
||
// No need to define this method if it has no action to perform after getted the property | ||
public function gettedProperty(): void { | ||
// The action to perform after getted the property | ||
echo 'Getted property...' . PHP_EOL; | ||
} | ||
|
||
// No need to define this method if the property to set is defined. | ||
public function setProperty(mixed $value) { | ||
$this->property = $value; | ||
} | ||
|
||
// No need to define this method if it has no action to perform before setting the property | ||
public function settingProperty(mixed $value): void { | ||
// The action to perform before setting the property | ||
echo 'Setting property...' . PHP_EOL; | ||
} | ||
|
||
// No need to define this method if it has no action to perform after setted the property | ||
public function settedProperty(mixed $value): void { | ||
// The action to perform after setted the property | ||
echo 'Setted property...' . PHP_EOL; | ||
} | ||
} |
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,11 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
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; |
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,41 @@ | ||
{ | ||
"name": "sikessem/capsule", | ||
"description": "Encapsulate properties and use accessor/mutator methods", | ||
"homepage": "https://github.com/sikessem/capsule#readme", | ||
"keywords": [ | ||
"capsule", | ||
"encapsulation", | ||
"accessor", | ||
"mutator", | ||
"property", | ||
"getter", | ||
"setter", | ||
"method", | ||
"event" | ||
], | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "SIGUI Kessé Emmanuel", | ||
"email": "[email protected]", | ||
"homepage": "http://sikessem.com" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Sikessem\\Capsule\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"App\\": "app/" | ||
} | ||
}, | ||
"require": { | ||
"php": "^8.0" | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"bin": "./capsule" | ||
} |
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,5 @@ | ||
<?php | ||
|
||
namespace Sikessem\Capsule; | ||
|
||
interface Encapsulable extends Settable, Gettable {} |
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,7 @@ | ||
<?php | ||
|
||
namespace Sikessem\Capsule; | ||
|
||
trait Encapsuler { | ||
use Setter, Getter; | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Sikessem\Capsule; | ||
|
||
interface Gettable { | ||
public function __get(string $name): mixed; | ||
public function __isset(string $name): bool; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Sikessem\Capsule; | ||
|
||
trait Getter { | ||
public function __get(string $name): mixed { | ||
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'); | ||
} | ||
|
||
public function __isset(string $name): bool { | ||
if (method_exists($this, $method = 'get' . ucfirst($name))) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Sikessem\Capsule; | ||
|
||
interface Settable { | ||
public function __set(string $name, mixed $value): void; | ||
public function __unset(string $name): void; | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Sikessem\Capsule; | ||
|
||
trait Setter { | ||
public function __set(string $name, mixed $value): void { | ||
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 { | ||
throw new \RuntimeException('Property ' . $name . ' not found'); | ||
} | ||
} | ||
|
||
public function __unset(string $name): void { | ||
if (method_exists($this, $method = 'set' . ucfirst($name))) { | ||
$this->$method(null); | ||
} | ||
} | ||
} |