https://github.com/phoenixrvd/oda
API-Package for simplify access from Data-Block of an object, without implementing from getters and setters.
https://github.com/phoenixrvd/oda
clean-code composer-package decorator-pattern magic-method mit open-source php-library php56 php70 php71 php72
Last synced: 7 months ago
JSON representation
API-Package for simplify access from Data-Block of an object, without implementing from getters and setters.
- Host: GitHub
- URL: https://github.com/phoenixrvd/oda
- Owner: phoenixrvd
- License: mit
- Created: 2017-04-10T20:15:50.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-02-28T10:33:18.000Z (almost 8 years ago)
- Last Synced: 2025-06-06T23:35:43.418Z (8 months ago)
- Topics: clean-code, composer-package, decorator-pattern, magic-method, mit, open-source, php-library, php56, php70, php71, php72
- Language: PHP
- Size: 39.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Object Data Accessor (ODA)
[](https://php.net/)
[](https://packagist.org/packages/phoenixrvd/oda)
[](https://packagist.org/packages/phoenixrvd/oda)
[](https://packagist.org/packages/phoenixrvd/oda)
[](https://travis-ci.org/phoenixrvd/oda)
[](https://codeclimate.com/github/phoenixrvd/oda)
[](https://styleci.io/repos/87853601)
[](https://codeclimate.com/github/phoenixrvd/oda/coverage)
[](https://bettercodehub.com/results/phoenixrvd/oda)
[](https://packagist.org/packages/phoenixrvd/oda)
- [Features](#features)
- [Installation](#installation)
- [Basics](#basics)
- [IDE-Helper](#ide-helper)
- [Standard-Accessoren](#standard-accessoren)
- [Eigene-Accessoren](#eigene-accessoren)
- [Beispiel](#beispiel)
- [Hinweis](#hinweis)
- [Testing](#testing)
- [Copyright and license](#copyright-and-license)
## Features
* Vereinfacht das Nutzen von Datenhaltungsobjekten.
* Erhöht die Lesbarkeit von Quellcode, durch das Minimieren von LOC.
* Standardisiert DAO-Schicht mit einem kleinem Package, ohne große Frameworks.
## Installation
Bei der Installation ist [Composer](https://getcomposer.org/download/) vorausgesetzt.
```bash
composer require phoenixrvd/oda
```
## Basics
Angenommen, braucht man ein ein Objekt, welches genau 2 Datenfelder hat (foo und bar).
Deklariert man Standard-Methoden, wird das Objekt wie Folgt aussehen:
```php
data['foo'];
}
public function setFoo($value){
$this->data['foo'] = $value;
return $this;
}
public function hasFoo(){
return isset($this->data['foo']);
}
public function isFoo($value){
return $this->hasFoo() && $this->data['foo'] === $value;
}
public function getBar(){
return $this->data['bar'];
}
public function setBar($value){
$this->data['bar'] = $value;
return $this;
}
public function hasBar(){
return isset($this->data['bar']);
}
public function isBar($value){
return $this->hasBar() && $this->data['bar'] === $value;
}
}
```
Hätte man nicht 2 sondern 10 Properties, wird das Object gleich um 300 LOC länger.
Nutzt man das Packet, bekommt man gleiche Funktionalität wesentlich kompakter. Auch wenn anzahl von Datenfelder wächst,
vergrößert man nicht den Code-Basis.
Im Beispiel Unten ist vorheriges Objekt umgeschrieben:
```php
data['created_at']``` ist Unix-Timestamp, man braucht es aber als RFC822-Datum.
Im [Bespiel](#beispiel) ist beschrieben, wie man der API ein **date** - Accessor beibringt. Damit wird voll automatisch
Unix-Timestamp nach Datum konvertieren und man es mit ```$this->dateCreatedAt()``` überall verwenden kann.
### Beispiel
1. Accessor-Klasse anlegen
```php
getData();
return date(DATE_RFC822, $data[ $this->propertyName ]);
}
}
```
2. Trait für Wiederverwendbarkeit anlegen
```php
setAccessor(new DateRFC822) // Eigenes Accessor bei der Factory registrieren
->makeMethod($name)
->execute($this, $arguments);
}
}
```
3. Eigenes Trait an Stelle von Standard-Trait im Object verwenden.
```php
setCreatedAt(1491945734)->dateCreatedAt();
```
## Hinweis
Projekte, die auf sehr hohe Leistung ausgelegt sind, sollten auf die Methodik generell verzichten. Diskussion dazu findet man
[hier](http://stackoverflow.com/questions/3330852/get-set-call-performance-questions-with-php).
## Testing
```bash
composer oda:test
```
## Copyright and license
Code released under the [MIT License](LICENSE).