-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.php
More file actions
143 lines (115 loc) · 3.5 KB
/
Entity.php
File metadata and controls
143 lines (115 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace App\Model\Trait;
use App\Controller\DefaultController;
use App\Controller\EntityController;
use App\Repository\Interface\Searchable;
use App\Service\ModelExporter;
use App\Service\ModelNormalizer;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
trait Entity
{
protected object $entity;
public function getEntity(): object
{
return $this->entity;
}
public function update( EntityManagerInterface $entityManager, $flush = false ): void
{
if ( $this->entity->getId() ) {
$this->persist( $entityManager, $flush );
}
}
public function persist( EntityManagerInterface $entityManager, $flush = false ): void
{
// Create ref if not set yet.
if ( is_callable( [ $this, 'createRef' ] ) ) {
$this->createRef( false );
}
$entityManager->persist( $this->entity );
if ( $flush ) {
$entityManager->flush();
}
}
public function normalize( $relationships = false, $dependents = false ): array
{
return ( new ModelNormalizer() )->normalize( $this, $relationships, $dependents );
}
public function export(): array
{
return ( new ModelExporter() )->export( $this );
}
public function __call( string $name, array $arguments )
{
if ( ! is_callable( [ $this->entity, $name ] ) ) {
throw new \Exception( 'Method not found: ' . __CLASS__ . '::' . $name );
}
return call_user_func_array( [ $this->entity, $name ], $arguments );
}
public static function get( $entity ): ?static
{
if ( $entity instanceof static ) {
return $entity;
}
if ( empty( static::getEntityClass() ) ) {
return null;
}
if ( is_object( $entity ) && EntityController::getEntityClass( $entity ) === static::getEntityClass() ) {
return new static( $entity );
}
$repository = static::getRepository();
if ( is_numeric( $entity ) ) {
$entity = $repository->findOneBy( [ 'id' => $entity ] );
} elseif ( is_string( $entity ) ) {
$entity = $repository->findOneBy( [ 'ref' => $entity ] );
} else {
$entity = $repository->findOneBy( $entity );
}
return ( $entity ) ? new static( $entity ) : null;
}
public static function getAll( array $query = [] ): array
{
if ( empty( static::getEntityClass() ) ) {
return [];
}
$repository = static::getRepository();
if ( $query ) {
$order = $query['orderBy'] ?? $query['order'] ?? $query['sort'] ?? null;
$limit = $query['limit'] ?? $query['max'] ?? null;
$offset = $query['offset'] ?? null;
$where = $query['where'] ?? [];
if ( ! empty( $query['search'] ) && $repository instanceof Searchable ) {
$search = $query['search'];
if ( ! is_array( $search ) ) {
$search = array( 'name' => $search );
}
$entities = $repository->searchBy( $search, $where, $order, $limit, $offset );
} else {
$entities = $repository->findBy( $where, $order, $limit, $offset );
}
} else {
$entities = $repository->findAll();
}
$models = [];
foreach ( $entities as $entity ) {
$models[] = new static( $entity );
}
return $models;
}
public static function getTotalCount( array $query = [] ): int
{
if ( empty( static::getEntityClass() ) ) {
return 0;
}
$repository = static::getRepository();
return $repository->count( $query['where'] ?? [] );
}
public static function getRepository( EntityManagerInterface $entityManager = null ): EntityRepository
{
if ( ! $entityManager ) {
$entityManager = DefaultController::getEntityManager();
}
return $entityManager->getRepository( static::getEntityClass() );
}
abstract public static function getEntityClass();
}