Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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