Stub.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Cloner;
  11. /**
  12. * Represents the main properties of a PHP variable.
  13. *
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class Stub
  17. {
  18. public const TYPE_REF = 1;
  19. public const TYPE_STRING = 2;
  20. public const TYPE_ARRAY = 3;
  21. public const TYPE_OBJECT = 4;
  22. public const TYPE_RESOURCE = 5;
  23. public const TYPE_SCALAR = 6;
  24. public const STRING_BINARY = 1;
  25. public const STRING_UTF8 = 2;
  26. public const ARRAY_ASSOC = 1;
  27. public const ARRAY_INDEXED = 2;
  28. public int $type = self::TYPE_REF;
  29. public string|int|null $class = '';
  30. public mixed $value = null;
  31. public int $cut = 0;
  32. public int $handle = 0;
  33. public int $refCount = 0;
  34. public int $position = 0;
  35. public array $attr = [];
  36. /**
  37. * @internal
  38. */
  39. protected static array $propertyDefaults = [];
  40. public function __serialize(): array
  41. {
  42. static $noDefault = new \stdClass();
  43. if (self::class === static::class) {
  44. $data = [];
  45. foreach ($this as $k => $v) {
  46. $default = self::$propertyDefaults[$this::class][$k] ??= ($p = new \ReflectionProperty($this, $k))->hasDefaultValue() ? $p->getDefaultValue() : ($p->hasType() ? $noDefault : null);
  47. if ($noDefault === $default || $default !== $v) {
  48. $data[$k] = $v;
  49. }
  50. }
  51. return $data;
  52. }
  53. return \Closure::bind(function () use ($noDefault) {
  54. $data = [];
  55. foreach ($this as $k => $v) {
  56. $default = self::$propertyDefaults[$this::class][$k] ??= ($p = new \ReflectionProperty($this, $k))->hasDefaultValue() ? $p->getDefaultValue() : ($p->hasType() ? $noDefault : null);
  57. if ($noDefault === $default || $default !== $v) {
  58. $data[$k] = $v;
  59. }
  60. }
  61. return $data;
  62. }, $this, $this::class)();
  63. }
  64. }