Skip to content

Commit 34b2ff3

Browse files
committed
Create Base Classes for PHP 7.4
1 parent e1a5e4a commit 34b2ff3

File tree

10 files changed

+609
-0
lines changed

10 files changed

+609
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"require": {
1313
"php": "^7.0|^8.0",
1414
"ext-pdo": "*",
15+
"ext-json": "*",
1516
"laravel/helpers": "^1.5",
1617
"illuminate/console": "~5.8.0|^6.0|^7.0|^8.0|^9.0",
1718
"illuminate/support": "^5.5|^6.0|^7.0|^8.0|^9.0"
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Models\Entity;
4+
5+
use Illuminate\Contracts\Support\Arrayable;
6+
use JsonSerializable;
7+
8+
abstract class Entity implements JsonSerializable, Arrayable
9+
{
10+
// contain originals value of attributes
11+
private array $originals = [];
12+
13+
/**
14+
* @return int
15+
*/
16+
abstract public function getId();
17+
18+
public function __construct()
19+
{
20+
21+
}
22+
23+
public function __set($name, $value)
24+
{
25+
if (property_exists($this, $name)) {
26+
$function = camel_case('set_' . snake_case($name));
27+
$this->$function($value);
28+
}
29+
}
30+
31+
public function __get($name)
32+
{
33+
if (property_exists($this, $name)) {
34+
$function = camel_case('get_' . snake_case($name));
35+
return $this->$function();
36+
}
37+
}
38+
39+
public function __isset($name)
40+
{
41+
return property_exists($this, $name);
42+
}
43+
44+
/**
45+
* Make all variables of the object as null
46+
* @return $this
47+
*/
48+
public function clearVariables()
49+
{
50+
$attributes = get_object_vars($this);
51+
foreach ($attributes as $attributeName => $attributeValue) {
52+
$this->$attributeName = null;
53+
}
54+
return $this;
55+
}
56+
57+
/**
58+
* @return int
59+
*/
60+
public function getPrimaryKey()
61+
{
62+
return $this->getId();
63+
}
64+
65+
/**
66+
* Fill the model
67+
*/
68+
public function fill()
69+
{
70+
71+
}
72+
73+
/**
74+
* get an Array of current Attributes value
75+
*/
76+
public function toArray(): array
77+
{
78+
return get_object_vars($this);
79+
}
80+
81+
/**
82+
* store an array of attributes original value
83+
*/
84+
public function storeOriginals()
85+
{
86+
$this->originals = $this->toArray();
87+
}
88+
89+
/**
90+
* get an Array of Changed Attributes
91+
* @return array
92+
*/
93+
public function changedAttributesName()
94+
{
95+
$changedAttributes = [];
96+
$attributes = $this->toArray();
97+
foreach ($attributes as $key => $value) {
98+
if (isset($this->originals[$key])) {
99+
if ($value != $this->originals[$key] && !((is_array($this->originals[$key]) || is_object($this->originals[$key])))) {
100+
$changedAttributes[] = $key;
101+
}
102+
}
103+
}
104+
return $changedAttributes;
105+
}
106+
107+
/**
108+
* get an Array of Changed Attributes with new values
109+
* @return array
110+
*/
111+
public function getDirty()
112+
{
113+
$dirty = [];
114+
$attributes = $this->toArray();
115+
116+
foreach ($this->changedAttributesName() as $key) {
117+
$dirty[$key] = $attributes[$key];
118+
}
119+
120+
return $dirty;
121+
}
122+
123+
/**
124+
* get an Array of Changed Attributes with original values
125+
* @return array
126+
*/
127+
public function getChanges()
128+
{
129+
$changes = [];
130+
131+
foreach ($this->changedAttributesName() as $key) {
132+
$changes[$key] = $this->originals[$key];
133+
}
134+
135+
return $changes;
136+
}
137+
138+
/**
139+
* is any attribute changed?
140+
* @return bool
141+
*/
142+
public function isDirty()
143+
{
144+
if (count($this->changedAttributesName()) > 0) return true;
145+
146+
return false;
147+
}
148+
149+
public function jsonSerialize()
150+
{
151+
return $this->toArray();
152+
}
153+
}

src/Models/PHP7.4/Enums/Enum.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Models\Enums;
4+
5+
use ReflectionClass;
6+
7+
abstract class Enum
8+
{
9+
public function getList(): array
10+
{
11+
return (new ReflectionClass($this))->getConstants();
12+
}
13+
14+
/**
15+
* @param int|string $key
16+
* @return string|null
17+
*/
18+
public function getValue($key): ?string
19+
{
20+
$list = $this->getList();
21+
$keys = array_keys($list);
22+
$key = is_numeric($key) ? (int)$key : $key;
23+
24+
if (is_int($key) && $key < count($keys)) {
25+
$value = $list[$keys[$key]];
26+
} else {
27+
$value = $list[strtoupper($key)];
28+
}
29+
30+
return $value;
31+
}
32+
33+
/**
34+
* @param int|string $value
35+
* @return false|int|string
36+
*/
37+
public function indexOf($value)
38+
{
39+
$list = $this->getList();
40+
$values = array_values($list);
41+
42+
return array_search($value, $values, true);
43+
}
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Models\Enums;
4+
5+
class GriewFilterOperator extends Enum
6+
{
7+
public const IS_EQUAL_TO = 'is_equal_to';
8+
public const IS_EQUAL_TO_OR_NULL = 'is_equal_to_or_null';
9+
public const IS_NOT_EQUAL_TO = 'is_not_equal_to';
10+
public const IS_NULL = 'is_null';
11+
public const IS_NOT_NULL = 'is_not_null';
12+
public const START_WITH = 'start_with';
13+
public const DOES_NOT_CONTAINS = 'does_not_contains';
14+
public const CONTAINS = 'contains';
15+
public const ENDS_WITH = 'ends_with';
16+
public const IN = 'in';
17+
public const NOT_IN = 'not_In';
18+
public const BETWEEN = 'between';
19+
public const IS_GREATER_THAN_OR_EQUAL_TO = 'is_greater_than_or_equal_to';
20+
public const IS_GREATER_THAN = 'is_greater_than';
21+
public const IS_LESS_THAN_OR_EQUAL_TO = 'is_less_than_or_equal_to';
22+
public const IS_LESS_THAN = 'is_less_than';
23+
public const IS_AFTER_THAN_OR_EQUAL_TO = 'is_after_than_or_equal_to';
24+
public const IS_AFTER_THAN = 'is_after_than';
25+
public const IS_BEFORE_THAN_OR_EQUAL_TO = 'is_Before_than_or_equal_to';
26+
public const IS_BEFORE_THAN = 'is_before_than';
27+
// public const IS_INSIDE_POLYGON = 'is_inside_polygon';
28+
// public const IS_NEAR_TO_POINT = 'is_near_to_point';
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Models\Factory;
4+
5+
use Nanvaie\DatabaseRepository\Models\Entity\Entity;
6+
use Illuminate\Support\Collection;
7+
use stdClass;
8+
9+
abstract class Factory implements IFactory
10+
{
11+
abstract public function makeEntityFromStdClass(stdClass $entity): Entity;
12+
13+
/**
14+
* @param Collection|array $entities
15+
* @return Collection
16+
*/
17+
public function makeCollectionOfEntities($entities): Collection
18+
{
19+
$entityCollection = collect();
20+
21+
foreach ($entities as $_entity) {
22+
if (is_array($_entity)) {
23+
$_entity = (object)$_entity;
24+
}
25+
$entityCollection->push($this->makeEntityFromStdClass($_entity));
26+
}
27+
28+
return $entityCollection;
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Models\Factory;
4+
5+
use Nanvaie\DatabaseRepository\Models\Entity\Entity;
6+
use Illuminate\Support\Collection;
7+
use stdClass;
8+
9+
interface IFactory
10+
{
11+
/**
12+
* @param stdClass $entity
13+
* @return Entity
14+
*/
15+
public function makeEntityFromStdClass(stdClass $entity): Entity;
16+
17+
/**
18+
* @param Collection|array $entities
19+
* @return Collection
20+
*/
21+
public function makeCollectionOfEntities($entities): Collection;
22+
}

0 commit comments

Comments
 (0)