Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Testing\Fluent;
4
 
5
use Closure;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Traits\Macroable;
9
use Illuminate\Support\Traits\Tappable;
10
use Illuminate\Testing\AssertableJsonString;
11
use PHPUnit\Framework\Assert as PHPUnit;
12
 
13
class AssertableJson implements Arrayable
14
{
15
    use Concerns\Has,
16
        Concerns\Matching,
17
        Concerns\Debugging,
18
        Concerns\Interaction,
19
        Macroable,
20
        Tappable;
21
 
22
    /**
23
     * The properties in the current scope.
24
     *
25
     * @var array
26
     */
27
    private $props;
28
 
29
    /**
30
     * The "dot" path to the current scope.
31
     *
32
     * @var string|null
33
     */
34
    private $path;
35
 
36
    /**
37
     * Create a new fluent, assertable JSON data instance.
38
     *
39
     * @param  array  $props
40
     * @param  string|null  $path
41
     * @return void
42
     */
43
    protected function __construct(array $props, string $path = null)
44
    {
45
        $this->path = $path;
46
        $this->props = $props;
47
    }
48
 
49
    /**
50
     * Compose the absolute "dot" path to the given key.
51
     *
52
     * @param  string  $key
53
     * @return string
54
     */
55
    protected function dotPath(string $key = ''): string
56
    {
57
        if (is_null($this->path)) {
58
            return $key;
59
        }
60
 
61
        return rtrim(implode('.', [$this->path, $key]), '.');
62
    }
63
 
64
    /**
65
     * Retrieve a prop within the current scope using "dot" notation.
66
     *
67
     * @param  string|null  $key
68
     * @return mixed
69
     */
70
    protected function prop(string $key = null)
71
    {
72
        return Arr::get($this->props, $key);
73
    }
74
 
75
    /**
76
     * Instantiate a new "scope" at the path of the given key.
77
     *
78
     * @param  string  $key
79
     * @param  \Closure  $callback
80
     * @return $this
81
     */
82
    protected function scope(string $key, Closure $callback): self
83
    {
84
        $props = $this->prop($key);
85
        $path = $this->dotPath($key);
86
 
87
        PHPUnit::assertIsArray($props, sprintf('Property [%s] is not scopeable.', $path));
88
 
89
        $scope = new static($props, $path);
90
        $callback($scope);
91
        $scope->interacted();
92
 
93
        return $this;
94
    }
95
 
96
    /**
97
     * Instantiate a new "scope" on the first child element.
98
     *
99
     * @param  \Closure  $callback
100
     * @return $this
101
     */
102
    public function first(Closure $callback): self
103
    {
104
        $props = $this->prop();
105
 
106
        $path = $this->dotPath();
107
 
108
        PHPUnit::assertNotEmpty($props, $path === ''
109
            ? 'Cannot scope directly onto the first element of the root level because it is empty.'
110
            : sprintf('Cannot scope directly onto the first element of property [%s] because it is empty.', $path)
111
        );
112
 
113
        $key = array_keys($props)[0];
114
 
115
        $this->interactsWith($key);
116
 
117
        return $this->scope($key, $callback);
118
    }
119
 
120
    /**
121
     * Instantiate a new "scope" on each child element.
122
     *
123
     * @param  \Closure  $callback
124
     * @return $this
125
     */
126
    public function each(Closure $callback): self
127
    {
128
        $props = $this->prop();
129
 
130
        $path = $this->dotPath();
131
 
132
        PHPUnit::assertNotEmpty($props, $path === ''
133
            ? 'Cannot scope directly onto each element of the root level because it is empty.'
134
            : sprintf('Cannot scope directly onto each element of property [%s] because it is empty.', $path)
135
        );
136
 
137
        foreach (array_keys($props) as $key) {
138
            $this->interactsWith($key);
139
 
140
            $this->scope($key, $callback);
141
        }
142
 
143
        return $this;
144
    }
145
 
146
    /**
147
     * Create a new instance from an array.
148
     *
149
     * @param  array  $data
150
     * @return static
151
     */
152
    public static function fromArray(array $data): self
153
    {
154
        return new static($data);
155
    }
156
 
157
    /**
158
     * Create a new instance from an AssertableJsonString.
159
     *
160
     * @param  \Illuminate\Testing\AssertableJsonString  $json
161
     * @return static
162
     */
163
    public static function fromAssertableJsonString(AssertableJsonString $json): self
164
    {
165
        return static::fromArray($json->json());
166
    }
167
 
168
    /**
169
     * Get the instance as an array.
170
     *
171
     * @return array
172
     */
173
    public function toArray()
174
    {
175
        return $this->props;
176
    }
177
}