Skip to content

Commit f44163a

Browse files
authored
Merge pull request #6 from adrorocker/develop
Add Entities
2 parents a8bfda2 + 628f9dd commit f44163a

30 files changed

+968
-189
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
build
1+
build
2+
html
3+
vendor
4+
composer.lock

.travis.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@ sudo: required
1010

1111
install: composer install
1212

13+
matrix:
14+
allow_failures:
15+
- php: hhvm
16+
1317
before_script:
1418
# navigate out of module directory to prevent blown stack by recursive module lookup
15-
- wget https://phar.phpunit.de/phpunit.phar
16-
- chmod +x phpunit.phar
17-
- sudo mv phpunit.phar /usr/local/bin/phpunit
19+
- wget https://phar.phpunit.de/phpunit-5.6.phar
20+
- chmod +x phpunit-5.6.phar
21+
- sudo mv phpunit-5.6.phar /usr/local/bin/phpunit
1822
- phpunit --version
1923

2024
- wget http://getcomposer.org/composer.phar
2125
- php composer.phar install
22-
- php composer.phar require satooshi/php-coveralls
26+
- php composer.phar require php-coveralls/php-coveralls
2327

2428
script:
2529
- mkdir -p build/logs
2630
- phpunit -c phpunit.xml.dist
31+
- phpunit --coverage-clover build/logs/clover.xml
2732

2833
after_script:
29-
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then travis_retry php vendor/bin/coveralls -v; fi
30-
- if [[ "$TRAVIS_PHP_VERSION" == '7.0' ]]; then travis_retry php vendor/bin/coveralls -v; fi
34+
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then travis_retry php vendor/bin/php-coveralls -v; fi
35+
- if [[ "$TRAVIS_PHP_VERSION" == '7.0' ]]; then travis_retry php vendor/bin/php-coveralls -v; fi
36+
- if [[ "$TRAVIS_PHP_VERSION" == '7.1' ]]; then travis_retry php vendor/bin/php-coveralls -v; fi

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2016, Alejandro Morelos.
1+
Copyright (c) 2016 - 2018, Alejandro Morelos.
22

33
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:
44

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,86 @@ $put = $firebase->put('/logs/'.$id, $data);
4545
$get = $firebase->get('/logs');
4646
```
4747

48+
49+
## Extras
50+
51+
Now [PHP-Firebase](https://github.com/adrorocker/php-firebase) include a simple way to save and retrieve _Entities_ using repositories.
52+
53+
You can use them like this:
54+
55+
* Create an _entity_ class
56+
57+
```php
58+
// app/Model/User/User.php
59+
<?php
60+
61+
namespace App\Model\User;
62+
63+
use PhpFirebase\Entities\Entity;
64+
65+
class User extends Entity
66+
{
67+
protected $id;
68+
69+
public $firstName;
70+
71+
public $lastName;
72+
}
73+
74+
```
75+
76+
* Create a _repository_ class
77+
78+
```php
79+
// app/Model/User/UserRepository.php
80+
<?php
81+
82+
namespace App\Model\User;
83+
84+
use PhpFirebase\Entities\Repository\Repository;
85+
86+
class UserRepository extends Repository
87+
{
88+
public function __construct()
89+
{
90+
// Base endpoint
91+
$base = 'https://hey-123.firebaseio.com/somesubendpoint';
92+
// Auth token
93+
$token = 'a1b2c3d4e5f6g7h8i9';
94+
95+
$this->class = User::class;
96+
97+
parent::__construct($base, $token, '/users');
98+
}
99+
}
100+
101+
```
102+
103+
* Usage
104+
105+
```php
106+
require '../vendor/autoload.php';
107+
108+
$repo = new UserRepository();
109+
// Create user
110+
$user = new User([
111+
'id' => 1,
112+
'firstName' => 'Adro',
113+
'lastName' => 'Rocker',
114+
]);
115+
$user = $repo->store($user); // $user will be an instance of App\Model\User
116+
117+
// Update user
118+
// You can get or assign values to an entity property using a method named as the property name.
119+
$user->lastName('Rocks'); // setting $lastName to be 'Rocks'.
120+
$lastName = $user->lastName(); // getting $lastName, $lastName has the value 'Rocks'.
121+
$user = $repo->store($user);
122+
123+
// Find user
124+
$user = $repo->find(1); // $user will be an instance of App\Model\User
125+
126+
```
127+
48128
## Authors:
49129

50130
[Alejandro Morelos](https://github.com/adrorocker).

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"authors": [
99
{
1010
"name": "Adro Rocker",
11-
"email": "alejandro.morelos@jarwebdev.com",
11+
"email": "me@adro.rocks",
1212
"homepage": "https://github.com/adrorocker"
1313
}
1414
],
@@ -21,9 +21,10 @@
2121
},
2222
"autoload": {
2323
"psr-4": {
24-
"PhpFirebase\\": ["src/","tests/"]
24+
"PhpFirebase\\": ["src/", "extra/", "tests/"]
2525
},
2626
"files": [
27+
"extra/Entities/functions.php"
2728
]
2829
},
2930
"extra": {

extra/Entities/Bridge.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* PHP-Firebase.
4+
*
5+
* @link https://github.com/adrorocker/php-firebase
6+
*
7+
* @copyright Copyright (c) 2018 Adro Rocker
8+
* @author Adro Rocker <mes@adro.rocks>
9+
*/
10+
11+
namespace PhpFirebase\Entities;
12+
13+
use ReflectionObject;
14+
use ReflectionProperty;
15+
16+
class Bridge
17+
{
18+
/**
19+
* @var object
20+
*/
21+
protected $object;
22+
23+
/**
24+
* @var array
25+
*/
26+
protected $properties = [];
27+
28+
/**
29+
* Make non-public members of the given object accessible.
30+
*
31+
* @param object $object.- Object which members we'll make accessible
32+
*/
33+
public function __construct($object)
34+
{
35+
$this->object = $object;
36+
$reflected = new ReflectionObject($this->object);
37+
$this->properties = [];
38+
39+
$properties = $reflected->getProperties(
40+
ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC
41+
);
42+
43+
foreach ($properties as $property) {
44+
$property->setAccessible(true);
45+
$this->properties[$property->getName()] = $property;
46+
}
47+
}
48+
49+
public function getProperties()
50+
{
51+
return $this->properties;
52+
}
53+
54+
/**
55+
* Returns a property of $this->object.
56+
*
57+
* @param string $name
58+
*
59+
* @return mixed
60+
*/
61+
public function __get($name)
62+
{
63+
// If the property is exposed (with reflection) then we use getValue()
64+
// to access it, else we access it directly
65+
if (isset($this->properties[$name])) {
66+
return $this->properties[$name]->getValue($this->object);
67+
}
68+
}
69+
}

extra/Entities/Call.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* PHP-Firebase.
4+
*
5+
* @link https://github.com/adrorocker/php-firebase
6+
*
7+
* @copyright Copyright (c) 2018 Adro Rocker
8+
* @author Adro Rocker <mes@adro.rocks>
9+
*/
10+
11+
namespace PhpFirebase\Entities;
12+
13+
use BadMethodCallException;
14+
use ReflectionClass;
15+
use ReflectionProperty;
16+
17+
trait Call
18+
{
19+
/**
20+
* Get or Set property.
21+
*
22+
* @param string $name Name of the method
23+
* @param array $arguments Arguments
24+
*
25+
* @throws BadMethodCallException If the $name is not a property
26+
*/
27+
public function __call($name, $arguments)
28+
{
29+
if (true !== property_exists($this, $name)) {
30+
throw new BadMethodCallException(sprintf(
31+
'The metod "%s" does not exist',
32+
$name
33+
));
34+
}
35+
if ($arguments && count($arguments) == 1) {
36+
$reflect = new ReflectionClass($this);
37+
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
38+
foreach ($props as $prop) {
39+
if ($prop->getName() == $name) {
40+
$this->{$name} = $arguments[0];
41+
}
42+
}
43+
}
44+
45+
return $this->{$name};
46+
}
47+
}

extra/Entities/Entity.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* PHP-Firebase.
4+
*
5+
* @link https://github.com/adrorocker/php-firebase
6+
*
7+
* @copyright Copyright (c) 2018 Adro Rocker
8+
* @author Adro Rocker <mes@adro.rocks>
9+
*/
10+
11+
namespace PhpFirebase\Entities;
12+
13+
class Entity implements EntityInterface
14+
{
15+
use Call;
16+
17+
public function __construct(array $properties)
18+
{
19+
$this->fill($properties);
20+
}
21+
22+
public function toArray()
23+
{
24+
$bridge = new Bridge($this);
25+
$array = [];
26+
foreach ($bridge->getProperties() as $key => $value) {
27+
$array[$key] = $this->$key;
28+
}
29+
30+
return json_decode(json_encode($array), true);
31+
}
32+
33+
public function toJson()
34+
{
35+
return json_encode($this->toArray());
36+
}
37+
38+
public static function fromJson($json)
39+
{
40+
$array = json_decode($json, true);
41+
$class = get_called_class();
42+
43+
return new $class($array);
44+
}
45+
46+
protected function fill(array $properties)
47+
{
48+
foreach ($properties as $key => $value) {
49+
if (true == property_exists($this, $key)) {
50+
$this->$key = $value;
51+
}
52+
}
53+
}
54+
}

extra/Entities/EntityInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* PHP-Firebase.
4+
*
5+
* @link https://github.com/adrorocker/php-firebase
6+
*
7+
* @copyright Copyright (c) 2018 Adro Rocker
8+
* @author Adro Rocker <mes@adro.rocks>
9+
*/
10+
11+
namespace PhpFirebase\Entities;
12+
13+
interface EntityInterface
14+
{
15+
public function toArray();
16+
17+
public function toJson();
18+
19+
public static function fromJson($string);
20+
}

0 commit comments

Comments
 (0)