Closure 함수 예제.

1$message = 'hello';
2 
3// No "use"
4$example = function () {
5 var_dump($message);
6};
7 
8echo $example(); //undefined

use 사용

1$message = 'hello';
2 
3// Inherit $message
4$example = function () use ($message) {
5 var_dump($message);
6};
7 
8echo $example(); //hello
1// Inherited variable's value is from when the function
2// is defined, not when called
3$message = 'world';
4 
5echo $example(); //hello

&message 참조사용

1// Reset message
2$message = 'hello';
3 
4// Inherit by-reference
5$example = function () use (&$message) {
6 var_dump($message);
7};
8echo $example(); //hello
9 
10// The changed value in the parent scope
11// is reflected inside the function call
12$message = 'world';
13 
14echo $example(); //world

Closure 를 이용한 예제

1 
2 // class Cart.php
3 
4 * @param \Closure $search
5 * @return \Illuminate\Support\Collection
6 */
7public function search(Closure $search)
8{
9 $content = $this->getContent();
10 return $content->filter($search);
11}
12 
13// livewire item render() 함수에서
14// app('cart') 클래스 불러서 search() 함수를 실행시킨다.
15 
16public function render()
17{
18 $item = $this->item;
19 
20 $cartItem = app('cart')->search(function ($cartItem) use ($item) {
21 return $cartItem->id === $item->id;
22 })->first();
23 
24 if (isset($cartItem)) {
25 $this->rowId = $cartItem->rowId;
26 } else {
27 $this->rowId = null;
28 }
29 
30 return view('livewire.item');
31}

Collection Filter 사용법

1$collection = collect([1, 2, 3, 4]);
2 
3$filtered = $collection->filter(function (int $value, int $key) {
4return $value > 2;
5});
6 
7$filtered->all();
8 
9// [3, 4]

getContent()

1/**
2* Get the carts content, if there is no cart content set yet, return a new empty Collection
3*
4* @return \Illuminate\Support\Collection
5*/
6protected function getContent()
7{
8 $content = $this->session->has($this->instance)
9 ? $this->session->get($this->instance)
10 : new Collection;
11 return $content;
12}

__construct()

1 
2/**
3* Cart constructor.
4*
5* @param \Illuminate\Session\SessionManager $session
6* @param \Illuminate\Contracts\Events\Dispatcher $events
7*/
8public function __construct(SessionManager $session, Dispatcher $events)
9{
10 $this->session = $session;
11 $this->events = $events;
12 $this->instance(self::DEFAULT_INSTANCE);
13}

instance()

1/**
2* Set the current cart instance.
3*
4* @param string|null $instance
5* @return \App\Cart
6*/
7public function instance($instance = null)
8{
9 $instance = $instance ?: self::DEFAULT_INSTANCE;
10 $this->instance = sprintf('%s.%s', 'cart', $instance);
11return $this;
12}

add() array_map()

1 
2public function add($id, $name = null, $qty = null, $price = null, array $options = [])
3{
4 // keyID 여러개 넘어온경우..배열처리
5 // keyID 가 하나만 넘어온 경우.
6 
7 if ($this->isMulti($id)) {
8 return array_map(function ($item) {
9 return $this->add($item);
10 }, $id);
11 }
12 
13 $cartItem = $this->createCartItem($id, $name, $qty, $price, $options);
14 $content = $this->getContent();
15 
16 if ($content->has($cartItem->rowId)) {
17 $cartItem->qty += $content->get($cartItem->rowId)->qty;
18 }
19 
20 $content->put($cartItem->rowId, $cartItem);
21 
22 $this->events->dispatch('cart.added', $cartItem);
23 $this->session->put($this->instance, $content);
24 
25 return $cartItem;
26}

add() array_map()

1 
2private function createCartItem($id, $name, $qty, $price, array $options)
3{
4 if (is_array($id)) {
5 $cartItem = CartItem::fromArray($id);
6 $cartItem->setQuantity($id['qty']);
7 } else {
8 $cartItem = CartItem::fromAttributes($id, $name, $price, $options);
9 $cartItem->setQuantity($qty);
10}
11$cartItem->setTaxRate(config('cart.tax'));
12return $cartItem;
13}

static 을 이용한 생성 방법

1 
2// 배열로 넘어온 경우.
3public static function fromArray(array $attributes)
4{
5 $options = array_get($attributes, 'options', []);
6 return new self($attributes['id'], $attributes['name'], $attributes['price'], $options);
7}
8 
9// 속성으로 넘어온 경우
10public static function fromAttributes($id, $name, $price, array $options = [])
11{
12 return new self($id, $name, $price, $options);
13}

고유키 만들기

1/**
2* Generate a unique id for the cart item.
3*
4* @param string $id
5* @param array $options
6* @return string
7*/
8protected function generateRowId($id, array $options)
9{
10 ksort($options);
11 return md5($id . serialize($options));
12}
13 
14function generateUniqueKey()
15{
16 return uniqid(); // Using the built-in uniqid() function to generate a unique identifier
17}