| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Schema; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Nette\Utils\Reflection; |
| 12 |
/** |
| 13 |
* @internal |
| 14 |
*/ |
| 15 |
final class Helpers |
| 16 |
{ |
| 17 |
use \Packetery\Nette\StaticClass; |
| 18 |
public const PREVENT_MERGING = '_prevent_merging'; |
| 19 |
/** |
| 20 |
* Merges dataset. Left has higher priority than right one. |
| 21 |
* @return array|string |
| 22 |
*/ |
| 23 |
public static function merge($value, $base) |
| 24 |
{ |
| 25 |
if (\is_array($value) && isset($value[self::PREVENT_MERGING])) { |
| 26 |
unset($value[self::PREVENT_MERGING]); |
| 27 |
return $value; |
| 28 |
} |
| 29 |
if (\is_array($value) && \is_array($base)) { |
| 30 |
$index = 0; |
| 31 |
foreach ($value as $key => $val) { |
| 32 |
if ($key === $index) { |
| 33 |
$base[] = $val; |
| 34 |
$index++; |
| 35 |
} else { |
| 36 |
$base[$key] = static::merge($val, $base[$key] ?? null); |
| 37 |
} |
| 38 |
} |
| 39 |
return $base; |
| 40 |
} elseif ($value === null && \is_array($base)) { |
| 41 |
return $base; |
| 42 |
} else { |
| 43 |
return $value; |
| 44 |
} |
| 45 |
} |
| 46 |
public static function getPropertyType(\ReflectionProperty $prop) : ?string |
| 47 |
{ |
| 48 |
if (!\class_exists(\Packetery\Nette\Utils\Type::class)) { |
| 49 |
throw new \Packetery\Nette\NotSupportedException('Expect::from() requires nette/utils 3.x'); |
| 50 |
} elseif ($type = \Packetery\Nette\Utils\Type::fromReflection($prop)) { |
| 51 |
return (string) $type; |
| 52 |
} elseif ($type = \preg_replace('#\\s.*#', '', (string) self::parseAnnotation($prop, 'var'))) { |
| 53 |
$class = Reflection::getPropertyDeclaringClass($prop); |
| 54 |
return \preg_replace_callback('#[\\w\\\\]+#', function ($m) use($class) { |
| 55 |
return Reflection::expandClassName($m[0], $class); |
| 56 |
}, $type); |
| 57 |
} |
| 58 |
return null; |
| 59 |
} |
| 60 |
/** |
| 61 |
* Returns an annotation value. |
| 62 |
* @param \ReflectionProperty $ref |
| 63 |
*/ |
| 64 |
public static function parseAnnotation(\Reflector $ref, string $name) : ?string |
| 65 |
{ |
| 66 |
if (!Reflection::areCommentsAvailable()) { |
| 67 |
throw new \Packetery\Nette\InvalidStateException('You have to enable phpDoc comments in opcode cache.'); |
| 68 |
} |
| 69 |
$re = '#[\\s*]@' . \preg_quote($name, '#') . '(?=\\s|$)(?:[ \\t]+([^@\\s]\\S*))?#'; |
| 70 |
if ($ref->getDocComment() && \preg_match($re, \trim($ref->getDocComment(), '/*'), $m)) { |
| 71 |
return $m[1] ?? ''; |
| 72 |
} |
| 73 |
return null; |
| 74 |
} |
| 75 |
/** |
| 76 |
* @param mixed $value |
| 77 |
*/ |
| 78 |
public static function formatValue($value) : string |
| 79 |
{ |
| 80 |
if (\is_object($value)) { |
| 81 |
return 'object ' . \get_class($value); |
| 82 |
} elseif (\is_string($value)) { |
| 83 |
return "'" . \Packetery\Nette\Utils\Strings::truncate($value, 15, '...') . "'"; |
| 84 |
} elseif (\is_scalar($value)) { |
| 85 |
return \var_export($value, \true); |
| 86 |
} else { |
| 87 |
return \strtolower(\gettype($value)); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|