Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Validation;
4
 
5
use Illuminate\Support\Arr;
6
 
7
class ValidationData
8
{
9
    /**
10
     * Initialize and gather data for the given attribute.
11
     *
12
     * @param  string  $attribute
13
     * @param  array  $masterData
14
     * @return array
15
     */
16
    public static function initializeAndGatherData($attribute, $masterData)
17
    {
18
        $data = Arr::dot(static::initializeAttributeOnData($attribute, $masterData));
19
 
20
        return array_merge($data, static::extractValuesForWildcards(
21
            $masterData, $data, $attribute
22
        ));
23
    }
24
 
25
    /**
26
     * Gather a copy of the attribute data filled with any missing attributes.
27
     *
28
     * @param  string  $attribute
29
     * @param  array  $masterData
30
     * @return array
31
     */
32
    protected static function initializeAttributeOnData($attribute, $masterData)
33
    {
34
        $explicitPath = static::getLeadingExplicitAttributePath($attribute);
35
 
36
        $data = static::extractDataFromPath($explicitPath, $masterData);
37
 
38
        if (! str_contains($attribute, '*') || str_ends_with($attribute, '*')) {
39
            return $data;
40
        }
41
 
42
        return data_set($data, $attribute, null, true);
43
    }
44
 
45
    /**
46
     * Get all of the exact attribute values for a given wildcard attribute.
47
     *
48
     * @param  array  $masterData
49
     * @param  array  $data
50
     * @param  string  $attribute
51
     * @return array
52
     */
53
    protected static function extractValuesForWildcards($masterData, $data, $attribute)
54
    {
55
        $keys = [];
56
 
991 lars 57
        $pattern = str_replace('\*', '[^\.]+', preg_quote($attribute, '/'));
148 lars 58
 
59
        foreach ($data as $key => $value) {
60
            if ((bool) preg_match('/^'.$pattern.'/', $key, $matches)) {
61
                $keys[] = $matches[0];
62
            }
63
        }
64
 
65
        $keys = array_unique($keys);
66
 
67
        $data = [];
68
 
69
        foreach ($keys as $key) {
70
            $data[$key] = Arr::get($masterData, $key);
71
        }
72
 
73
        return $data;
74
    }
75
 
76
    /**
77
     * Extract data based on the given dot-notated path.
78
     *
79
     * Used to extract a sub-section of the data for faster iteration.
80
     *
81
     * @param  string  $attribute
82
     * @param  array  $masterData
83
     * @return array
84
     */
85
    public static function extractDataFromPath($attribute, $masterData)
86
    {
87
        $results = [];
88
 
89
        $value = Arr::get($masterData, $attribute, '__missing__');
90
 
91
        if ($value !== '__missing__') {
92
            Arr::set($results, $attribute, $value);
93
        }
94
 
95
        return $results;
96
    }
97
 
98
    /**
99
     * Get the explicit part of the attribute name.
100
     *
101
     * E.g. 'foo.bar.*.baz' -> 'foo.bar'
102
     *
103
     * Allows us to not spin through all of the flattened data for some operations.
104
     *
105
     * @param  string  $attribute
106
     * @return string
107
     */
108
    public static function getLeadingExplicitAttributePath($attribute)
109
    {
110
        return rtrim(explode('*', $attribute)[0], '.') ?: null;
111
    }
112
}