Skip to content

Commit 14a112f

Browse files
committed
Add Entities
1 parent 4034ce8 commit 14a112f

24 files changed

+794
-26
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

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

extra/Entities/Call.php

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

extra/Entities/Entity.php

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

extra/Entities/EntityInterface.php

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

0 commit comments

Comments
 (0)