-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEntityMapFactory.php
73 lines (61 loc) · 1.63 KB
/
EntityMapFactory.php
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
<?php
namespace Darya\ORM;
use Darya\ORM\EntityMap\Strategy\PropertyStrategy;
use InvalidArgumentException;
/**
* Darya's entity map factory.
*
* Provides simple entity map instantiation with sensible defaults.
*
* @author Chris Andrew <[email protected]>
*/
class EntityMapFactory
{
/**
* Create an entity map without a specific entity class.
*
* @param string $name
* @param array $mapping
* @param string|null $resource
* @return EntityMap
*/
public function create(string $name, array $mapping, string $resource = null): EntityMap
{
if ($resource === null) {
$resource = $name;
}
$entityMap = new EntityMap(
Entity::class, $resource, $mapping, new PropertyStrategy()
);
$entityMap->setName($name);
return $entityMap;
}
/**
* Create a mapping for an entity class.
*
* @param string $class
* @param array|null $mapping
* @param string|null $resource
* @return EntityMap
*/
public function createForClass(string $class, array $mapping = null, string $resource = null): EntityMap
{
if (!class_exists($class)) {
throw new InvalidArgumentException("Undefined class '$class'");
}
if ($resource === null) {
// TODO: Extract snake_case resource name from class base name
$resource = $class;
}
if (empty($mapping)) {
// TODO: Extract class properties using PHP's Reflection API for a one-to-one mapping
// One step further would be to use Doctrine-style or PHP 8 annotations... ooo...
$mapping = [];
}
$entityMap = new EntityMap(
$class, $resource, $mapping, new PropertyStrategy()
);
$entityMap->setName($class);
return $entityMap;
}
}