-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractModel.php
More file actions
75 lines (63 loc) · 1.86 KB
/
AbstractModel.php
File metadata and controls
75 lines (63 loc) · 1.86 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
<?php
namespace SyncEngine\Model\Abstract;
use Psr\Container\ContainerInterface;
use SyncEngine\Controller\DefaultController;
use SyncEngine\Model\Interface\Normalizable;
abstract class AbstractModel implements Normalizable
{
private ContainerInterface $_container;
public function __construct()
{
$this->setContainer( DefaultController::getContainer() );
}
public function setContainer( ContainerInterface $container ): void
{
$this->_container = $container;
}
public function getContainer(): ?ContainerInterface
{
return $this->_container ?? null;
}
public function getParameter( string $name, string $prefix = 'syncengine' ): mixed
{
if ( $prefix ) {
$name = $prefix . '.' . $name;
}
return $this->getContainer()->get('parameter_bag')->get( $name );
}
protected function trans( ?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null ): string {
return $this->getContainer()->get('translator')->trans( $id, $parameters, $domain, $locale );
}
public static function getClassName(): string
{
return ( new \ReflectionClass( static::class ) )->getShortName();
}
public static function getModelName( string $name = '' ): string
{
if ( ! $name ) {
$name = static::getClassName();
}
if ( ! str_ends_with( $name, 'Model' ) ) {
return $name;
}
// Remove "Model" from class short name.
return substr( $name, 0, -5 );
}
public static function getModelClassName( string $name = '' ): string
{
if ( ! $name ) {
$name = static::getModelName();
}
return ucfirst( $name ) . 'Model';
}
public static function getModelClass( string $name ): ?string
{
// @todo Cache models within a container instead of hardcoding the namespace.
$namespace = substr( __NAMESPACE__, 0, -8 );
$name = $namespace . self::getModelClassName( $name );
if ( class_exists( $name ) ) {
return $name;
}
return null;
}
}