Skip to content

Commit afb7e87

Browse files
committed
+ hydrator by construct
1 parent 5f7343f commit afb7e87

File tree

4 files changed

+97
-21
lines changed

4 files changed

+97
-21
lines changed

.gitignore

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1 @@
1-
# Build and Release Folders
2-
bin/
3-
bin-debug/
4-
bin-release/
5-
[Oo]bj/ # FlashDevelop obj
6-
[Bb]in/ # FlashDevelop bin
7-
8-
# Other files and folders
9-
.settings/
10-
11-
# Executables
12-
*.swf
13-
*.air
14-
*.ipa
15-
*.apk
16-
17-
# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
18-
# should NOT be excluded as they contain compiler settings and other important
19-
# information for Eclipse / Flash Builder.
1+
.idea/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# helpers
2-
Base daily helpers
1+
# Common helpers
2+
Base daily helpers for fast reusing

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "shubinmi/helpers",
3+
"description": "Base daily helpers for fast reusing.",
4+
"keywords": [
5+
"helpers",
6+
"base",
7+
"common"
8+
],
9+
"homepage": "https://github.com/shubinmi/helpers",
10+
"type": "library",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Maxim Shubin",
15+
"email": "[email protected]",
16+
"homepage": "https://github.com/shubinmi",
17+
"role": "Founder and cyclist"
18+
}
19+
],
20+
"support": {
21+
"wiki": "https://github.com/shubinmi/helpers",
22+
"source": "https://github.com/shubinmi/helpers"
23+
},
24+
"require": {
25+
"php": ">=5.4"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"BaseHelpers\\": "src/"
30+
}
31+
}
32+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace BaseHelpers\hydrators;
4+
5+
abstract class ConstructFromArrayOrJson
6+
{
7+
/**
8+
* @param string(json)|array|null $params
9+
*/
10+
public function __construct($params = null)
11+
{
12+
if (is_string($params)) {
13+
$params = json_decode($params, true);
14+
}
15+
if (!is_array($params)) {
16+
return;
17+
}
18+
foreach ($params as $property => $value) {
19+
$setter = 'set' . str_replace('_', '', $property);
20+
if (method_exists($this, $setter)) {
21+
$this->{$setter}($value);
22+
} elseif (property_exists($this, $property)) {
23+
$this->{$property} = $value;
24+
}
25+
}
26+
}
27+
28+
/**
29+
* @param bool $withNull
30+
*
31+
* @return array
32+
*/
33+
public function toArray($withNull = false)
34+
{
35+
$result = [];
36+
foreach (get_object_vars($this) as $property => $value) {
37+
$getter = 'get' . str_replace('_', '', $property);
38+
if (method_exists($this, $getter)) {
39+
$result[$property] = $this->{$getter}();
40+
} else {
41+
$result[$property] = $this->{$property};
42+
}
43+
}
44+
if ($withNull) {
45+
return $result;
46+
}
47+
48+
return array_filter(
49+
$result, function ($value) {
50+
return isset($value);
51+
}
52+
);
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function toJson()
59+
{
60+
return json_encode($this->toArray());
61+
}
62+
}

0 commit comments

Comments
 (0)