1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace AlecRabbit\Accessories\Caller; |
5
|
|
|
|
6
|
|
|
use AlecRabbit\Accessories\Caller\Contracts\CallerConstants; |
7
|
|
|
use AlecRabbit\Formatters\Core\AbstractFormatter; |
8
|
|
|
use AlecRabbit\Reports\Core\Formattable; |
9
|
|
|
|
10
|
|
|
class CallerDataFormatter extends AbstractFormatter implements CallerConstants |
11
|
|
|
{ |
12
|
|
|
/** {@inheritDoc} */ |
13
|
9 |
|
public function __construct(?int $options = null) |
14
|
|
|
{ |
15
|
9 |
|
parent::__construct($options); |
16
|
9 |
|
$this->options = $options ?? static::SHOW_LINE_AND_FILE; |
17
|
9 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
8 |
|
public function format(Formattable $data): string |
23
|
|
|
{ |
24
|
8 |
|
if ($data instanceof CallerData) { |
25
|
7 |
|
if (null !== $class = $data->getClass()) { |
26
|
|
|
return |
27
|
5 |
|
sprintf( |
28
|
5 |
|
'%s%s%s%s', |
29
|
5 |
|
$this->getLineAndFile($data), |
30
|
5 |
|
$class, |
31
|
5 |
|
(string)$data->getType(), |
32
|
5 |
|
$this->getFunction($data) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
return |
36
|
2 |
|
sprintf( |
37
|
2 |
|
'%s%s', |
38
|
2 |
|
$this->getLineAndFile($data), |
39
|
2 |
|
$this->getFunction($data) |
40
|
|
|
); |
41
|
|
|
} |
42
|
1 |
|
return $this->errorMessage($data, CallerData::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param CallerData $caller |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
7 |
|
protected function getLineAndFile(CallerData $caller): string |
50
|
|
|
{ |
51
|
7 |
|
if (($this->options & static::SHOW_LINE_AND_FILE) && self::STR_UNDEFINED !== $caller->getFunction()) { |
52
|
|
|
return |
53
|
5 |
|
sprintf( |
54
|
5 |
|
'[%s:"%s"] ', |
55
|
5 |
|
(string)$caller->getLine(), |
56
|
5 |
|
(string)$caller->getFile() |
57
|
|
|
); |
58
|
|
|
} |
59
|
2 |
|
return ''; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param CallerData $caller |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
7 |
|
protected function getFunction(CallerData $caller): string |
67
|
|
|
{ |
68
|
7 |
|
$function = $caller->getFunction(); |
69
|
7 |
|
if ($function === self::STR_UNDEFINED) { |
70
|
1 |
|
return ucfirst($function); |
71
|
|
|
} |
72
|
6 |
|
return $function . '()'; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|