-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToStringTrait.php
More file actions
56 lines (48 loc) · 1.63 KB
/
Copy pathToStringTrait.php
File metadata and controls
56 lines (48 loc) · 1.63 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
<?php
declare(strict_types=1);
/*
* This file is part of ezkoding
*
* (c) 2025 Oliver Glowa, coding.glowa.com
*
* This source file is subject to the Apache-2.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace ollily\Tools\String;
trait ToStringTrait
{
use ImplodeTrait;
/**
* @SuppressWarnings("PHPMD.CamelCaseMethodName")
*/
abstract protected function __toStringValues(): mixed; // NOSONAR: php:S100
public function __toString(): string
{
$value = $this->__toStringValues();
if (is_string($value)) {
$toString = sprintf('%s:\'%s\'', get_class($this), $value);
} elseif (is_scalar($value)) {
$toString = sprintf('%s:%s', get_class($this), (string)$value);
} elseif (is_array($value)) {
foreach (array_keys($value) as $arrayKey) {
if (is_object($value[$arrayKey]) && $this == $value[$arrayKey]) {
$value[$arrayKey] = get_class($value[$arrayKey]);
}
}
$toString = sprintf('%s:[%s]', get_class($this), self::implode_recursive(',', $value));
} elseif (is_object($value)) {
if ($this == $value) {
$toString = sprintf('{%s}', print_r($value, true));
} else {
$toString = sprintf('%s:{%s}', get_class($this), print_r($value, true));
}
} else {
$toString = sprintf('%s', get_class($this));
}
return $toString;
}
public function __wakeup(): void
{
throw new \BadMethodCallException("Cannot unserialize singleton");
}
}