Collection.php
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Lackoxygen\GzCbec\Utils;
use Illuminate\Support\Collection as BaseCollection;
use Lackoxygen\GzCbec\Traits\XML;
class Collection extends BaseCollection
{
use XML;
/**
* @param $method
* @param $value
* @param bool $lcfirst
*/
public function mSet($method, $value, $lcfirst = true)
{
parent::offsetSet($this->subKey($method, 3, $lcfirst), $value);
}
/**
* @param $method
* @param $value
*/
public function mPush($method, $value)
{
$key = $this->subKey($method, 4, false);
$listCollection = $this->get($key);
if (!$listCollection instanceof BaseCollection) {
$listCollection = BaseCollection::make();
}
$listCollection->push($value);
$this->offsetSet($key, $listCollection);
}
/**
* @param $method
* @param null $default
*
* @return mixed
*/
public function mGet($method, $default = null)
{
return parent::get($this->subKey($method, 3), $default);
}
/**
* @param string $key
* @param int $offset
* @param bool $lcfirst
*
* @return string
*/
public function subKey(string $key, int $offset = 0, $lcfirst = true): string
{
$afterKey = substr($key, $offset);
if ($lcfirst) {
return lcfirst($afterKey);
}
return ucfirst($afterKey);
}
}