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

Commit

Permalink
🎉 Begin the Capsule library
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Aug 15, 2022
0 parents commit 20d67bb
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
21 changes: 21 additions & 0 deletions LICENSE
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.
102 changes: 102 additions & 0 deletions README.md
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!
46 changes: 46 additions & 0 deletions app/Capsule.php
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;
}
}
11 changes: 11 additions & 0 deletions capsule
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;
41 changes: 41 additions & 0 deletions composer.json
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"
}
5 changes: 5 additions & 0 deletions src/Encapsulable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Sikessem\Capsule;

interface Encapsulable extends Settable, Gettable {}
7 changes: 7 additions & 0 deletions src/Encapsuler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Sikessem\Capsule;

trait Encapsuler {
use Setter, Getter;
}
8 changes: 8 additions & 0 deletions src/Gettable.php
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;
}
26 changes: 26 additions & 0 deletions src/Getter.php
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;
}
}
8 changes: 8 additions & 0 deletions src/Settable.php
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;
}
25 changes: 25 additions & 0 deletions src/Setter.php
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);
}
}
}

0 comments on commit 20d67bb

Please sign in to comment.