Skip to content

Commit f09949e

Browse files
committed
Return base collections for some methods
1 parent 3e39ae9 commit f09949e

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

src/Collection.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,122 @@ public function __construct($items = [])
3636

3737
parent::__construct($items);
3838
}
39+
40+
/**
41+
* Run a map over each of the items.
42+
*
43+
* @param callable $callback
44+
* @return \Illuminate\Support\Collection|static
45+
*/
46+
public function map(callable $callback)
47+
{
48+
$result = $this->toBase()->map($callback);
49+
50+
$class = $this->getTypeClass();
51+
52+
return $result->contains(function ($item) use ($class) {
53+
return ! $item instanceof $class;
54+
}) ? $result : new static($result->all());
55+
}
56+
57+
/**
58+
* Run an associative map over each of the items.
59+
*
60+
* The callback should return an associative array with a single key / value pair.
61+
*
62+
* @param callable $callback
63+
* @return \Illuminate\Support\Collection|static
64+
*/
65+
public function mapWithKeys(callable $callback)
66+
{
67+
$result = $this->toBase()->mapWithKeys($callback);
68+
69+
$class = $this->getTypeClass();
70+
71+
return $result->contains(function ($item) use ($class) {
72+
return ! $item instanceof $class;
73+
}) ? $result : new static($result->all());
74+
}
75+
76+
/**
77+
* The following methods are intercepted to always return base collections.
78+
*/
79+
80+
/**
81+
* Get an array with the values of a given key.
82+
*
83+
* @param string|array $value
84+
* @param string|null $key
85+
* @return \Illuminate\Support\Collection
86+
*/
87+
public function pluck($value, $key = null)
88+
{
89+
return $this->toBase()->pluck($value, $key);
90+
}
91+
92+
/**
93+
* Get the keys of the collection items.
94+
*
95+
* @return \Illuminate\Support\Collection
96+
*/
97+
public function keys()
98+
{
99+
return $this->toBase()->keys();
100+
}
101+
102+
/**
103+
* Zip the collection together with one or more arrays.
104+
*
105+
* @param mixed ...$items
106+
* @return \Illuminate\Support\Collection
107+
*/
108+
public function zip($items)
109+
{
110+
return $this->toBase()->zip(...func_get_args());
111+
}
112+
113+
/**
114+
* Collapse the collection of items into a single array.
115+
*
116+
* @return \Illuminate\Support\Collection
117+
*/
118+
public function collapse()
119+
{
120+
return $this->toBase()->collapse();
121+
}
122+
123+
/**
124+
* Get a flattened array of the items in the collection.
125+
*
126+
* @param int $depth
127+
* @return \Illuminate\Support\Collection
128+
*/
129+
public function flatten($depth = INF)
130+
{
131+
return $this->toBase()->flatten($depth);
132+
}
133+
134+
/**
135+
* Flip the items in the collection.
136+
*
137+
* @return \Illuminate\Support\Collection
138+
*/
139+
public function flip()
140+
{
141+
return $this->toBase()->flip();
142+
}
143+
144+
/**
145+
* Pad collection to the specified length with a value.
146+
*
147+
* @param int $size
148+
* @param mixed $value
149+
* @return \Illuminate\Support\Collection
150+
*/
151+
public function pad($size, $value)
152+
{
153+
return $this->toBase()->pad($size, $value);
154+
}
39155

40156
/**
41157
* Get the class name of the items.

0 commit comments

Comments
 (0)