forked from Respect/Stringifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectHelper.php
More file actions
44 lines (35 loc) · 990 Bytes
/
ObjectHelper.php
File metadata and controls
44 lines (35 loc) · 990 Bytes
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
<?php
/*
* This file is part of Respect/Stringifier.
* Copyright (c) Henrique Moody <henriquemoody@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Stringifier\Helpers;
use function array_filter;
use function count;
use function implode;
use function sprintf;
use function strstr;
trait ObjectHelper
{
private function format(object $object, string|null ...$pieces): string
{
$filteredPieces = array_filter($pieces);
if (count($filteredPieces) === 0) {
return $this->getName($object) . ' {}';
}
return sprintf('%s { %s }', $this->getName($object), implode(' ', $filteredPieces));
}
private function getName(object $object): string
{
$name = strstr($object::class, "\000", true);
if ($name === 'class@anonymous') {
return 'class';
}
if ($name !== false) {
return $name;
}
return $object::class;
}
}