-
Notifications
You must be signed in to change notification settings - Fork 1
/
Map.php
70 lines (62 loc) · 1.41 KB
/
Map.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
<?php
namespace Railken\EloquentMapper;
use Railken\EloquentMapper\Contracts\Map as MapContract;
use Railken\EloquentMapper\Relations\RelationFinder;
use Illuminate\Database\Eloquent\Model;
class Map implements MapContract
{
/**
* Return an array of all models you want to map
*
* @return array
*/
public function models(): array
{
return [];
}
/**
* Given an instance of the model, retrieve all the relations
*
* @return array
*/
public function relations(Model $model): array
{
$finder = new RelationFinder();
return $finder->getModelRelations($model)->toArray();
}
/**
* Given an instance of the model, retrieve all the attributes
*
* @return array
*/
public function attributes(Model $model): array
{
return array_unique(array_merge(
array_keys($model->getCasts()),
$model->getFillable(),
$model->getDates()
));
}
/**
* Convert a model to a unique key
*
* @param Model $model
*
* @return string
*/
public function modelToKey(Model $model): string
{
return get_class($model);
}
/**
* Convert key to a new instance of a model
*
* @param string $key
*
* @return Model
*/
public function keyToModel(string $key): Model
{
return new $key();
}
}