-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Statement.php
65 lines (51 loc) 路 1.71 KB
/
Statement.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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\DI\Definitions;
use Nette;
/**
* Assignment or calling statement.
*
* @property string|array|Definition|Reference|null $entity
*/
final class Statement implements Nette\Schema\DynamicParameter
{
use Nette\SmartObject;
public array $arguments;
private string|array|Definition|Reference|null $entity;
public function __construct(string|array|Definition|Reference|null $entity, array $arguments = [])
{
if (
$entity !== null
&& !is_string($entity) // Class, @service, not, tags, types, PHP literal, entity::member
&& !$entity instanceof Definition
&& !$entity instanceof Reference
&& !(is_array($entity)
&& array_keys($entity) === [0, 1]
&& (is_string($entity[0])
|| $entity[0] instanceof self
|| $entity[0] instanceof Reference
|| $entity[0] instanceof Definition)
)) {
throw new Nette\InvalidArgumentException('Argument is not valid Statement entity.');
}
// normalize Class::method to [Class, method]
if (is_string($entity) && str_contains($entity, '::') && !str_contains($entity, '?')) {
$entity = explode('::', $entity, 2);
}
if (is_string($entity) && str_starts_with($entity, '@')) { // normalize @service to Reference
$entity = new Reference(substr($entity, 1));
} elseif (is_array($entity) && is_string($entity[0]) && str_starts_with($entity[0], '@')) {
$entity[0] = new Reference(substr($entity[0], 1));
}
$this->entity = $entity;
$this->arguments = $arguments;
}
public function getEntity(): string|array|Definition|Reference|null
{
return $this->entity;
}
}