A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.github.com/setnemo/asterisk-notation/commit/766c5b93b42d4715daefc48484aa78786b423912 below:

add tests for merge functions · setnemo/asterisk-notation@766c5b9 · GitHub

File tree Expand file treeCollapse file tree 11 files changed

+300

-34

lines changed

Filter options

Expand file treeCollapse file tree 11 files changed

+300

-34

lines changed Original file line number Diff line number Diff line change

@@ -19,7 +19,7 @@ Asterisk notation for array access in PHP. Update array access to the next level

19 19

```php

20 20

use Setnemo\Asterisk;

21 21 22 -

$items = new \Setnemo\Asterisk([

22 +

$items = new Asterisk([

23 23

'Europe' => [

24 24

'Ukraine' => [

25 25

'capital' => 'Kyiv',

@@ -65,15 +65,15 @@ Create a new \Setnemo\Asterisk object:

65 65

```php

66 66

use Adbar\Dot;

67 67

use Setnemo\Asterisk;

68 -

$asterisk = new \Setnemo\Asterisk;

68 +

$asterisk = new Asterisk;

69 69

$array = ['first_one' => ['second' => 'value'], 'first_two' => ['second' => 'value'],];

70 70

// With existing array

71 -

$asterisk = new \Setnemo\Asterisk($array);

71 +

$asterisk = new Asterisk($array);

72 72

// With existing \Adbar\Dot

73 73

$dot = new Dot($array);

74 -

$asterisk = new \Setnemo\Asterisk($dot);

74 +

$asterisk = new Asterisk($dot);

75 75

// or existing Asterisk

76 -

$newAsterisk = new \Setnemo\Asterisk($asterisk);

76 +

$newAsterisk = new Asterisk($asterisk);

77 77

```

78 78 79 79

## Methods

@@ -89,7 +89,7 @@ Asterisk has the following methods:

89 89

- [get()](#get)

90 90

- [has()](#has)

91 91

- [isEmpty()](#isempty)

92 -

- [merge()](#merge) // use Dot::merge(), **need write tests**, because used get(), set()

92 +

- [merge()](#merge)

93 93

- [mergeRecursive()](#mergerecursive)

94 94

- [mergeRecursiveDistinct()](#mergerecursivedistinct)

95 95

- [pull()](#pull)

@@ -326,11 +326,13 @@ $asterisk->isEmpty('*.spouse'); // true

326 326

### merge()

327 327 328 328

> It works like [Adbar\Dot::merge()](https://github.com/adbario/php-dot-notation#merge)

329 +

> Asterisk in path Asterisk::merge('key', 'value') work like Asterisk::set('key', 'value', false)

329 330 330 331

<a name="mergerecursive"></a>

331 332

### mergeRecursive()

332 333 333 334

> It works like [Adbar\Dot::mergeRecursive()](https://github.com/adbario/php-dot-notation#mergeRecursive)

335 +

> Asterisk in path Asterisk::mergeRecursive('key', 'value') work like Asterisk::set('key', 'value', false)

334 336 335 337

<a name="mergerecursivedistinct"></a>

336 338

### mergeRecursiveDistinct()

Original file line number Diff line number Diff line change

@@ -120,6 +120,75 @@ public function has($keys, $value = null, bool $strict = true): bool

120 120

return (bool) ((int) !empty($affectedKeys) | (int) parent::has($keys));

121 121

}

122 122 123 +

/**

124 +

* Merge a given array or Asterisk object with the given key

125 +

* or with the whole Asterisk object

126 +

* Also ignored * in keys, worked like Dot::merge()

127 +

*

128 +

* @param array|string|self $key

129 +

* @param array|self $value

130 +

*/

131 +

public function merge($key, $value = [])

132 +

{

133 +

if (is_array($key)) {

134 +

$this->items = array_merge($this->items, $key);

135 +

} elseif (is_string($key)) {

136 +

$items = (array) $this->get($key, null, false);

137 +

$value = array_merge($items, $this->getArrayItems($value));

138 + 139 +

$this->set($key, $value, false);

140 +

} elseif ($key instanceof self) {

141 +

$this->items = array_merge($this->items, $key->all());

142 +

}

143 +

}

144 + 145 +

/**

146 +

* Recursively merge a given array or a Dot object with the given key

147 +

* or with the whole Dot object.

148 +

*

149 +

* Duplicate keys are converted to arrays.

150 +

*

151 +

* @param array|string|self $key

152 +

* @param array|self $value

153 +

*/

154 +

public function mergeRecursive($key, $value = [])

155 +

{

156 +

if (is_array($key)) {

157 +

$this->items = array_merge_recursive($this->items, $key);

158 +

} elseif (is_string($key)) {

159 +

$items = (array) $this->get($key, null, false);

160 +

$value = array_merge_recursive($items, $this->getArrayItems($value));

161 + 162 +

$this->set($key, $value, false);

163 +

} elseif ($key instanceof self) {

164 +

$this->items = array_merge_recursive($this->items, $key->all());

165 +

}

166 +

}

167 + 168 +

/**

169 +

* Recursively merge a given array or a Dot object with the given key

170 +

* or with the whole Dot object.

171 +

*

172 +

* Instead of converting duplicate keys to arrays, the value from

173 +

* given array will replace the value in Dot object.

174 +

*

175 +

* @param array|string|self $key

176 +

* @param array|self $value

177 +

*/

178 +

public function mergeRecursiveDistinct($key, $value = [])

179 +

{

180 +

if (is_array($key)) {

181 +

$this->items = $this->arrayMergeRecursiveDistinct($this->items, $key);

182 +

} elseif (is_string($key)) {

183 +

$items = (array) $this->get($key, null, false);

184 +

$value = $this->arrayMergeRecursiveDistinct($items, $this->getArrayItems($value));

185 + 186 +

$this->set($key, $value, false);

187 +

} elseif ($key instanceof self) {

188 +

$this->items = $this->arrayMergeRecursiveDistinct($this->items, $key->all());

189 +

}

190 +

}

191 + 123 192

/**

124 193

* Set a given key / value pair or pairs

125 194

* @param array|int|string $keys

Original file line number Diff line number Diff line change

@@ -38,7 +38,7 @@ public function addWithAsterisk(): array

38 38

],

39 39

'key' => '*.second',

40 40

'value' => 'VALUE'

41 -

]

41 +

],

42 42

],

43 43

/* #1 */ [

44 44

[

@@ -48,7 +48,7 @@ public function addWithAsterisk(): array

48 48

],

49 49

'key' => '*.second',

50 50

'value' => 'value'

51 -

]

51 +

],

52 52

],

53 53

/* #2 */ [

54 54

[

@@ -58,7 +58,7 @@ public function addWithAsterisk(): array

58 58

],

59 59

'key' => '*.*',

60 60

'value' => '1'

61 -

]

61 +

],

62 62

],

63 63

/* #3 */ [

64 64

[

@@ -69,7 +69,7 @@ public function addWithAsterisk(): array

69 69

],

70 70

'key' => '*.*',

71 71

'value' => '1'

72 -

]

72 +

],

73 73

],

74 74

/* #4 */ [

75 75

[

@@ -85,7 +85,7 @@ public function addWithAsterisk(): array

85 85

],

86 86

'key' => '*.*',

87 87

'value' => '1'

88 -

]

88 +

],

89 89

],

90 90

/* #5 */ [

91 91

[

@@ -97,7 +97,7 @@ public function addWithAsterisk(): array

97 97

],

98 98

'key' => '*.second',

99 99

'value' => '1'

100 -

]

100 +

],

101 101

],

102 102

/* #6 */ [

103 103

[

Original file line number Diff line number Diff line change

@@ -38,7 +38,7 @@ public function clearWithAsterisk(): array

38 38

'first_two' => ['second' => []]

39 39

],

40 40

'key' => '*.second'

41 -

]

41 +

],

42 42

],

43 43

[

44 44

[

@@ -51,7 +51,7 @@ public function clearWithAsterisk(): array

51 51

'first_two' => ['second' => []]

52 52

],

53 53

'key' => '*.second'

54 -

]

54 +

],

55 55

],

56 56

[

57 57

[

@@ -64,7 +64,7 @@ public function clearWithAsterisk(): array

64 64

'first_two' => ['second' => []]

65 65

],

66 66

'key' => '*.*'

67 -

]

67 +

],

68 68

],

69 69

[

70 70

[

@@ -77,7 +77,7 @@ public function clearWithAsterisk(): array

77 77

'first_two' => ['second' => 'value',]

78 78

],

79 79

'key' => '*.second2'

80 -

]

80 +

],

81 81

],

82 82

[

83 83

[

Original file line number Diff line number Diff line change

@@ -36,7 +36,7 @@ public function deleteWithAsterisk(): array

36 36

'constructor' => [],

37 37

'expected' => [],

38 38

'key' => '*.second'

39 -

]

39 +

],

40 40

],

41 41

/* #1 */

42 42

[

Original file line number Diff line number Diff line change

@@ -126,7 +126,7 @@ public function hasWithAsteriskAndValue(): array

126 126

'expected' => true,

127 127

'key' => '*.second',

128 128

'value' => 'VALUE'

129 -

]

129 +

],

130 130

],

131 131

/* #2 */

132 132

[

@@ -135,7 +135,7 @@ public function hasWithAsteriskAndValue(): array

135 135

'expected' => false,

136 136

'key' => '*.second',

137 137

'value' => 'value'

138 -

]

138 +

],

139 139

],

140 140

/* #3 */

141 141

[['constructor' => ['*' => ['second' => 'value']], 'expected' => false, 'key' => '*.*', 'value' => '1']],

@@ -146,7 +146,7 @@ public function hasWithAsteriskAndValue(): array

146 146

'expected' => true,

147 147

'key' => '*.*',

148 148

'value' => 'value'

149 -

]

149 +

],

150 150

],

151 151

/* #5 */

152 152

[

@@ -155,7 +155,7 @@ public function hasWithAsteriskAndValue(): array

155 155

'expected' => false,

156 156

'key' => '*.*',

157 157

'value' => 'value'

158 -

]

158 +

],

159 159

],

160 160

/* #6 */

161 161

[

@@ -164,7 +164,7 @@ public function hasWithAsteriskAndValue(): array

164 164

'expected' => true,

165 165

'key' => '*.*',

166 166

'value' => 'value'

167 -

]

167 +

],

168 168

],

169 169

/* #7 */

170 170

[

@@ -191,7 +191,7 @@ public function hasWithAsteriskAndValue(): array

191 191

'expected' => false,

192 192

'key' => '*.second',

193 193

'value' => 'value'

194 -

]

194 +

],

195 195

],

196 196

/* #10*/

197 197

[

@@ -200,7 +200,7 @@ public function hasWithAsteriskAndValue(): array

200 200

'expected' => true,

201 201

'key' => '*.second',

202 202

'value' => 'value'

203 -

]

203 +

],

204 204

],

205 205

];

206 206

}

@@ -248,7 +248,7 @@ public function hasWithAsteriskAndValueNonStrict(): array

248 248

'expected' => false,

249 249

'key' => 'locations.Europe.*.currency',

250 250

'value' => 'ZAR'

251 -

]

251 +

],

252 252

],

253 253

/* #3 */

254 254

[

@@ -257,7 +257,7 @@ public function hasWithAsteriskAndValueNonStrict(): array

257 257

'expected' => true,

258 258

'key' => ['locations.Africa.South Africa.currency', 'locations.Africa.*.currency',],

259 259

'value' => 'ZAR'

260 -

]

260 +

],

261 261

],

262 262

];

263 263

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,65 @@

1 +

<?php

2 + 3 +

declare(strict_types=1);

4 + 5 +

namespace Tests\Unit;

6 + 7 +

use Codeception\Test\Unit;

8 +

use Setnemo\Asterisk;

9 + 10 +

/**

11 +

* Class MergeRecursiveDistinctTest

12 +

* @package Tests\Unit

13 +

*/

14 +

class MergeRecursiveDistinctTest extends Unit

15 +

{

16 +

/**

17 +

* @dataProvider setWithAsterisk()

18 +

*/

19 +

public function testMerge(array $example): void

20 +

{

21 +

$asterisk = new Asterisk($example['constructor']);

22 +

$asterisk->mergeRecursiveDistinct($example['key'], $example['value']);

23 + 24 +

$this->assertEquals($example['expected'], $asterisk->all());

25 +

}

26 + 27 +

/**

28 +

* @return \array[][]

29 +

*/

30 +

public function setWithAsterisk(): array

31 +

{

32 +

return [

33 +

/* #0 */ [

34 +

[

35 +

'constructor' => [],

36 +

'expected' => [

37 +

'*' => ['second' => ['value']],

38 +

],

39 +

'key' => '*.second',

40 +

'value' => 'value'

41 +

],

42 +

],

43 +

/* #1 */ [

44 +

[

45 +

'constructor' => ['example' => 'example1'],

46 +

'expected' => [

47 +

'example' => 'example2',

48 +

],

49 +

'key' => new Asterisk(['example' => 'example2']),

50 +

'value' => null,

51 +

],

52 +

],

53 +

/* #2 */ [

54 +

[

55 +

'constructor' => [],

56 +

'expected' => [

57 +

'test' => 'test',

58 +

],

59 +

'key' => ['test' => 'test'],

60 +

'value' => null,

61 +

],

62 +

],

63 +

];

64 +

}

65 +

}

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4