يحتوي هذا المستودع على بعض وحدات ماكرو المجموعة المفيدة.
Spatie هي وكالة تصميم مواقع الإنترنت مقرها في أنتويرب، بلجيكا. ستجد نظرة عامة على جميع مشاريعنا مفتوحة المصدر على موقعنا.
نحن نستثمر الكثير من الموارد في إنشاء أفضل الحزم مفتوحة المصدر في فئتها. يمكنك دعمنا عن طريق شراء أحد منتجاتنا المدفوعة.
نحن نقدر بشدة إرسالك لنا بطاقة بريدية من مسقط رأسك، مع ذكر الحزمة (الحزم) التي تستخدمها. ستجد عنواننا على صفحة الاتصال لدينا. ننشر جميع البطاقات البريدية المستلمة على جدار البطاقات البريدية الافتراضية لدينا.
يمكنك سحب الحزمة عبر الملحن:
composer require spatie/laravel-collection-macrosستقوم الحزمة بتسجيل نفسها تلقائيًا.
afteratsecondthirdfourthfifthsixthseventheighthninthtenthgetNthbeforecatchchunkBycollectBycontainsAnycontainsAlleachConsextractfilterMapfirstOrFailfirstOrPushfromPairsgetCaseInsensitiveglobgroupByModelhasCaseInsensitiveheadififAnyifEmptyinsertAfterinsertAfterKeyinsertAtinsertBeforeinsertBeforeKeynonepaginatepathpluckManypluckManyValuespluckToArrayprioritizerecursiverotatesectionBysimplePaginatesliceBeforetailtrytoPairstransposevalidateweightedRandomwithSizeafterاحصل على العنصر التالي من المجموعة.
$ collection = collect ([ 1 , 2 , 3 ]);
$ currentItem = 2 ;
$ currentItem = $ collection -> after ( $ currentItem ); // return 3;
$ collection -> after ( $ currentItem ); // return null;
$ currentItem = $ collection -> after ( function ( $ item ) {
return $ item > 1 ;
}); // return 3;يمكنك أيضًا تمرير معلمة ثانية لاستخدامها كبديل.
$ collection = collect ([ 1 , 2 , 3 ]);
$ currentItem = 3 ;
$ collection -> after ( $ currentItem , $ collection -> first ()); // return 1;atاسترداد عنصر في الفهرس.
$ data = new Collection ([ 1 , 2 , 3 ]);
$ data -> at ( 0 ); // 1
$ data -> at ( 1 ); // 2
$ data -> at (- 1 ); // 3secondاسترداد العنصر في الفهرس الثاني.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> second (); // 2thirdاسترداد العنصر في الفهرس الثالث.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> third (); // 3fourthاسترداد العنصر في الفهرس الرابع.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> fourth (); // 4fifthاسترداد العنصر في الفهرس الخامس.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> fifth (); // 5sixthاسترداد العنصر في الفهرس السادس.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> sixth (); // 6seventhاسترداد العنصر في الفهرس السابع.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> seventh (); // 7eighthاسترداد العنصر في الفهرس الثامن.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> eighth (); // 8ninthاسترداد العنصر في الفهرس التاسع.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> ninth (); // 9tenthاسترداد العنصر في الفهرس العاشر.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]);
$ data -> tenth (); // 10getNthاسترداد العنصر في العنصر n.
$ data = new Collection ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ]);
$ data -> getNth ( 11 ); // 11beforeاحصل على العنصر السابق من المجموعة.
$ collection = collect ([ 1 , 2 , 3 ]);
$ currentItem = 2 ;
$ currentItem = $ collection -> before ( $ currentItem ); // return 1;
$ collection -> before ( $ currentItem ); // return null;
$ currentItem = $ collection -> before ( function ( $ item ) {
return $ item > 2 ;
}); // return 2;يمكنك أيضًا تمرير معلمة ثانية لاستخدامها كبديل.
$ collection = collect ([ 1 , 2 , 3 ]);
$ currentItem = 1 ;
$ collection -> before ( $ currentItem , $ collection -> last ()); // return 3;catch انظر Try
chunkBy يقوم بتقطيع القيم من المجموعة إلى مجموعات طالما أن رد الاتصال المحدد صحيح. إذا تم تمرير المعلمة الاختيارية $preserveKeys كـ true ، فسوف تحافظ على المفاتيح الأصلية.
collect ([ ' A ' , ' A ' , ' B ' , ' A ' ])-> chunkBy ( function ( $ item ) {
return $ item == ' A ' ;
}); // return Collection([['A', 'A'],['B'], ['A']])collectByاحصل على عنصر عند مفتاح معين، واجمعه.
$ collection = collect ([
' foo ' => [ 1 , 2 , 3 ],
' bar ' => [ 4 , 5 , 6 ],
]);
$ collection -> collectBy ( ' foo ' ); // Collection([1, 2, 3])يمكنك أيضًا تمرير معلمة ثانية لاستخدامها كبديل.
$ collection = collect ([
' foo ' => [ 1 , 2 , 3 ],
' bar ' => [ 4 , 5 , 6 ],
]);
$ collection -> collectBy ( ' baz ' , [ ' Nope ' ]); // Collection(['Nope'])containsAny سيعود true في حالة وجود واحدة أو أكثر من القيم المحددة في المجموعة.
$ collection = collect ([ ' a ' , ' b ' , ' c ' ]);
$ collection -> containsAny ([ ' b ' , ' c ' , ' d ' ]); // returns true
$ collection -> containsAny ([ ' c ' , ' d ' , ' e ' ]); // returns true
$ collection -> containsAny ([ ' d ' , ' e ' , ' f ' ]); // returns false
$ collection -> containsAny ([]); // returns falsecontainsAll سيعود true إذا كانت جميع القيم المحددة موجودة في المجموعة.
$ collection = collect ([ ' a ' , ' b ' , ' c ' ]);
$ collection -> containsAll ([ ' b ' , ' c ' ,]); // returns true
$ collection -> containsAll ([ ' c ' , ' d ' ]); // returns false
$ collection -> containsAll ([ ' d ' , ' e ' ]); // returns false
$ collection -> containsAll ([]); // returns trueeachCons احصل على الجيران المتتاليين التاليين في مجموعة من حجم قطعة معين. إذا تم تمرير المعلمة الاختيارية $preserveKeys كـ true ، فسوف تحافظ على المفاتيح الأصلية.
collect ([ 1 , 2 , 3 , 4 ])-> eachCons ( 2 ); // return collect([[1, 2], [2, 3], [3, 4]])extract استخراج المفاتيح من المجموعة. هذا مشابه جدًا لـ only ، مع وجود اختلافين رئيسيين:
extract يُرجع التابع مصفوفة من القيم، وليس مصفوفة اقترانيةnull بدلاً من حذفها يكون extract مفيدًا عند استخدام بناء جملة PHP 7.1 Short list() .
[ $ name , $ role ] = collect ( $ user )-> extract ( ' name ' , ' role.name ' );filterMapقم بتعيين مجموعة وإزالة القيم الخاطئة دفعة واحدة.
$ collection = collect ([ 1 , 2 , 3 , 4 , 5 , 6 ])-> filterMap ( function ( $ number ) {
$ quotient = $ number / 3 ;
return is_integer ( $ quotient ) ? $ quotient : null ;
});
$ collection -> toArray (); // returns [1, 2]firstOrFail احصل على العنصر الأول. يرمي SpatieCollectionMacrosExceptionsCollectionItemNotFound إذا لم يتم العثور على العنصر.
$ collection = collect ([ 1 , 2 , 3 , 4 , 5 , 6 ])-> firstOrFail ();
$ collection -> toArray (); // returns [1]
collect ([])-> firstOrFail (); // throws SpatieCollectionMacrosExceptionsCollectionItemNotFoundfirstOrPushقم باسترجاع العنصر الأول باستخدام العنصر القابل للاستدعاء المعطى كمعلمة أولى. في حالة عدم وجود قيمة، ادفع قيمة المعلمة الثانية إلى المجموعة. يمكنك تمرير قابل للاستدعاء كمعلمة ثانية.
هذه الطريقة مفيدة حقًا عند التعامل مع خصائص الفئة المخزنة مؤقتًا، حيث تريد تخزين قيمة تم استردادها من واجهة برمجة التطبيقات (API) أو دالة باهظة الثمن حسابيًا في مجموعة لاستخدامها عدة مرات.
$ collection = collect ([ 1 , 2 , 3 ])-> firstOrPush ( fn ( $ item ) => $ item === 4 , 4 );
$ collection -> toArray (); // returns [1, 2, 3, 4]في بعض الأحيان، قد ترغب في تحديد المجموعة المستهدفة التي سيتم الدفع إليها. يمكنك تمرير هذا كمعلمة ثالثة.
$ collection = collect ([ 1 , 2 , 3 ]);
$ collection -> filter ()-> firstOrPush ( fn ( $ item ) => $ item === 4 , 4 , $ collection );
$ collection -> toArray (); // returns [1, 2, 3, 4]fromPairsتحويل مجموعة إلى عنصر مجموعة نموذج مصفوفة ترابطية.
$ collection = collect ([[ ' a ' , ' b ' ], [ ' c ' , ' d ' ], [ ' e ' , ' f ' ]])-> fromPairs ();
$ collection -> toArray (); // returns ['a' => 'b', 'c' => 'd', 'e' => 'f']getCaseInsensitiveاحصل على قيمة المفتاح المحدد.
إذا كان المفتاح عبارة عن سلسلة، فسنبحث عن المفتاح باستخدام مقارنة غير حساسة لحالة الأحرف.
$ collection = collect ([
' foo ' => ' bar ' ,
]);
$ collection -> getCaseInsensitive ( ' Foo ' ); // returns 'bar';glob إرجاع مجموعة من نتيجة glob() .
Collection:: glob ( ' config/*.php ' );groupByModel يشبه الأمر groupBy ، ولكنه يجمع المجموعة حسب نموذج Eloquent. نظرًا لأن المفتاح عبارة عن كائن وليس عددًا صحيحًا أو سلسلة، يتم تقسيم النتائج إلى صفائف منفصلة.
$ posts -> groupByModel ( ' category ' );
// [
// [$categoryA, [/*...$posts*/]],
// [$categoryB, [/*...$posts*/]],
// ]; التوقيع الكامل: groupByModel($callback, $preserveKeys, $modelKey, $itemsKey)
hasCaseInsensitiveتحديد ما إذا كانت المجموعة تحتوي على مفتاح باسم معين.
إذا كان $key عبارة عن سلسلة، فسنبحث عن المفتاح باستخدام مقارنة غير حساسة لحالة الأحرف.
$ collection = collect ([
' foo ' => ' bar ' ,
]);
$ collection -> hasCaseInsensitive ( ' Foo ' ); // returns true;headاسترداد العنصر الأول من المجموعة.
$ collection = collect ([ 1 , 2 , 3 ]);
$ collection -> head (); // return 1
$ collection = collect ([]);
$ collection -> head (); // return nullif يمكن أن يساعد الماكرو if في سلاسل التجميع الفرعية. هذا هو توقيع هذا الماكرو:
if (mixed $ if , mixed $ then = null , mixed $ else = null ): mixed $if و $then و $else يمكن أن يكون من أي نوع. إذا تم تمرير الإغلاق إلى أي من هذه المعلمات، فسيتم تنفيذ هذا الإغلاق وسيستخدم الماكرو نتائجه.
عندما يُرجع $if قيمة صحيحة، فسيتم إرجاع $then ، وإلا فسيتم إرجاع $else .
فيما يلي بعض الأمثلة:
collect ()-> if ( true , then: true , else: false ); // returns true
collect ()-> if ( false , then: true , else: false ); // returns false عندما يتم تمرير الإغلاق إلى $if أو $then أو $else ، سيتم تمرير المجموعة بأكملها كوسيطة لهذا الإغلاق.
// the `then` closure will be executed
// the first element of the returned collection now contains "THIS IS THE VALUE"
$ collection = collect ([ ' this is a value ' ])
-> if (
fn ( Collection $ collection ) => $ collection -> contains ( ' this is a value ' ),
then: fn ( Collection $ collection ) => $ collection -> map ( fn ( string $ item ) => strtoupper ( $ item )),
else: fn ( Collection $ collection ) => $ collection -> map ( fn ( string $ item ) => Str:: kebab ( $ item ))
);
// the `else` closure will be executed
// the first element of the returned collection now contains "this-is-another-value"
$ collection = collect ([ ' this is another value ' ])
-> if (
fn ( Collection $ collection ) => $ collection -> contains ( ' this is a value ' ),
then: fn ( Collection $ collection ) => $ collection -> map ( fn ( string $ item ) => strtoupper ( $ item )),
else: fn ( Collection $ collection ) => $ collection -> map ( fn ( string $ item ) => Str:: kebab ( $ item ))
);ifAnyينفذ الاستدعاء الذي تم تمريره إذا لم تكن المجموعة فارغة. سيتم إرجاع المجموعة بأكملها.
collect ()-> ifAny ( function ( Collection $ collection ) { // empty collection so this won't get called
echo ' Hello ' ;
});
collect ([ 1 , 2 , 3 ])-> ifAny ( function ( Collection $ collection ) { // non-empty collection so this will get called
echo ' Hello ' ;
});ifEmptyينفذ الاستدعاء الذي تم تمريره إذا كانت المجموعة فارغة. سيتم إرجاع المجموعة بأكملها.
collect ()-> ifEmpty ( function ( Collection $ collection ) { // empty collection so this will called
echo ' Hello ' ;
});
collect ([ 1 , 2 , 3 ])-> ifEmpty ( function ( Collection $ collection ) { // non-empty collection so this won't get called
echo ' Hello ' ;
});insertAfterإدراج عنصر بعد التواجد الأول لعنصر معين وإرجاع مثيل المجموعة المحدث. اختياريا يمكن إعطاء مفتاح.
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertAfter ( ' zero ' , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertAfter ( 0 , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]insertAfterKeyإدراج عنصر بعد مفتاح معين وإرجاع نسخة المجموعة المحدثة. اختياريا يمكن إعطاء مفتاح للعنصر الجديد.
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertAfterKey ( 0 , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertAfterKey ( ' zero ' , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]insertAtإدراج عنصر في فهرس معين وإرجاع مثيل المجموعة المحدث. اختياريا يمكن إعطاء مفتاح.
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertAt ( 1 , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertAt ( 1 , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]insertBeforeإدراج عنصر قبل التواجد الأول لعنصر معين وإرجاع مثيل المجموعة المحدث. اختياريا يمكن إعطاء مفتاح.
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertBefore ( ' two ' , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertBefore ( 2 , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]insertBeforeKeyإدراج عنصر قبل مفتاح معين وإرجاع نسخة المجموعة المحدثة. اختياريا يمكن إعطاء مفتاح للعنصر الجديد.
collect ([ ' zero ' , ' two ' , ' three ' ])-> insertBeforeKey ( 1 , ' one ' );
// Collection contains ['zero', 'one', 'two', 'three']
collect ([ ' zero ' => 0 , ' two ' => 2 , ' three ' => 3 ]-> insertBeforeKey ( ' two ' , 5 , ' five ' );
// Collection contains ['zero' => 0, 'five' => 5, 'two' => 2, 'three' => 3]none يتحقق مما إذا كانت المجموعة لا تحتوي على أي تكرارات لعنصر معين، أو زوج من القيمة الرئيسية، أو اجتياز اختبار الحقيقة. تقبل الوظيفة نفس المعلمات مثل طريقة contains .
collect ([ ' foo ' ])-> none ( ' bar ' ); // returns true
collect ([ ' foo ' ])-> none ( ' foo ' ); // returns false
collect ([[ ' name ' => ' foo ' ]])-> none ( ' name ' , ' bar ' ); // returns true
collect ([[ ' name ' => ' foo ' ]])-> none ( ' name ' , ' foo ' ); // returns false
collect ([ ' name ' => ' foo ' ])-> none ( function ( $ key , $ value ) {
return $ key === ' name ' && $ value === ' bar ' ;
}); // returns truepaginate قم بإنشاء مثيل LengthAwarePaginator للعناصر الموجودة في المجموعة.
collect ( $ posts )-> paginate ( 5 ); يؤدي هذا إلى ترقيم محتويات $posts مع 5 عناصر في كل صفحة. يقبل paginate بعض الخيارات، توجه إلى مستندات Laravel للحصول على دليل متعمق.
paginate(int $perPage = 15, string $pageName = 'page', ?int $page = null, ?int $total = null, array $options = [])
path إرجاع عنصر من المجموعة ببيانات متعددة الأبعاد باستخدام علامة "النقطة". يعمل بنفس طريقة pull المجموعة الأصلية، ولكن دون إزالة عنصر من المجموعة.
$ collection = new Collection ([
' foo ' => [
' bar ' => [
' baz ' => ' value ' ,
]
]
]);
$ collection -> path ( ' foo.bar.baz ' ) // 'value'pluckManyإرجاع مجموعة بالمفاتيح المحددة فقط.
$ collection = collect ([
[ ' a ' => 1 , ' b ' => 10 , ' c ' => 100 ],
[ ' a ' => 2 , ' b ' => 20 , ' c ' => 200 ],
]);
$ collection -> pluckMany ([ ' a ' , ' b ' ]);
// returns
// collect([
// ['a' => 1, 'b' => 10],
// ['a' => 2, 'b' => 20],
// ]);pluckManyValuesإرجاع مجموعة بقيم المفاتيح المحددة فقط.
$ collection = collect ([
[ ' a ' => 1 , ' b ' => 10 , ' c ' => 100 ],
[ ' a ' => 2 , ' b ' => 20 , ' c ' => 200 ],
]);
$ collection -> pluckMany ([ ' a ' , ' b ' ]);
// returns
// collect([
// [1, 10],
// [2, 20],
// ]);pluckToArrayإرجاع مجموعة من القيم لمفتاح معين.
$ collection = collect ([
[ ' a ' => 1 , ' b ' => 10 ],
[ ' a ' => 2 , ' b ' => 20 ],
[ ' a ' => 3 , ' b ' => 30 ]
]);
$ collection -> pluckToArray ( ' a ' ); // returns [1, 2, 3]prioritizeانقل العناصر إلى بداية المجموعة.
$ collection = collect ([
[ ' id ' => 1 ],
[ ' id ' => 2 ],
[ ' id ' => 3 ],
]);
$ collection
-> prioritize ( function ( array $ item ) {
return $ item [ ' id ' ] === 2 ;
})
-> pluck ( ' id ' )
-> toArray (); // returns [2, 1, 3]recursiveتحويل المصفوفة وأبنائها إلى مجموعة باستخدام العودية.
collect ([
' item ' => [
' children ' => []
]
])-> recursive ();
// subsequent arrays are now collectionsفي بعض الحالات قد لا ترغب في تحويل جميع الأطفال إلى مجموعة. يمكنك التحويل إلى عمق معين فقط من خلال توفير رقم للطريقة العودية.
collect ([
' item ' => [
' children ' => [
' one ' => [ 1 ],
' two ' => [ 2 ]
]
]
])-> recursive ( 1 ); // Collection(['item' => Collection(['children' => ['one' => [1], 'two' => [2]]])])يمكن أن يكون هذا مفيدًا عندما تعلم أنه عند عمق معين لن يكون ذلك ضروريًا أو أنه قد يؤدي إلى كسر التعليمات البرمجية الخاصة بك.
collect ([
' item ' => [
' children ' => [
' one ' => [ 1 ],
' two ' => [ 2 ]
]
]
])
-> recursive ( 1 )
-> map ( function ( $ item ) {
return $ item -> map ( function ( $ children ) {
return $ children -> mapInto (Model::class);
});
}); // Collection(['item' => Collection(['children' => ['one' => Model(), 'two' => Model()]])])
// If we do not pass a max depth we will get the error "Argument #1 ($attributes) must be of type array"rotateقم بتدوير العناصر الموجودة في المجموعة بالإزاحة المحددة
$ collection = collect ([ 1 , 2 , 3 , 4 , 5 , 6 ]);
$ rotate = $ collection -> rotate ( 1 );
$ rotate -> toArray ();
// [2, 3, 4, 5, 6, 1]sectionBy يقسم المجموعة إلى أقسام مجمعة حسب مفتاح معين. يشبه groupBy ولكنه يحترم ترتيب العناصر الموجودة في المجموعة ويعيد استخدام المفاتيح الموجودة.
$ collection = collect ([
[ ' name ' => ' Lesson 1 ' , ' module ' => ' Basics ' ],
[ ' name ' => ' Lesson 2 ' , ' module ' => ' Basics ' ],
[ ' name ' => ' Lesson 3 ' , ' module ' => ' Advanced ' ],
[ ' name ' => ' Lesson 4 ' , ' module ' => ' Advanced ' ],
[ ' name ' => ' Lesson 5 ' , ' module ' => ' Basics ' ],
]);
$ collection -> sectionBy ( ' module ' );
// [
// ['Basics', [
// ['name' => 'Lesson 1', 'module' => 'Basics'],
// ['name' => 'Lesson 2', 'module' => 'Basics'],
// ]],
// ['Advanced', [
// ['name' => 'Lesson 3', 'module' => 'Advanced'],
// ['name' => 'Lesson 4', 'module' => 'Advanced'],
// ]],
// ['Basics', [
// ['name' => 'Lesson 5', 'module' => 'Basics'],
// ]],
// ]; التوقيع الكامل: sectionBy($callback, $preserveKeys, $sectionKey, $itemsKey)
simplePaginate قم بإنشاء مثيل Paginator للعناصر الموجودة في المجموعة.
collect ( $ posts )-> simplePaginate ( 5 ); يؤدي هذا إلى ترقيم محتويات $posts مع 5 عناصر في كل صفحة. يقبل simplePaginate بعض الخيارات، توجه إلى مستندات Laravel للحصول على دليل متعمق.
simplePaginate(int $perPage = 15, string $pageName = 'page', ?int $page = null, ?int $total = null, array $options = [])
للحصول على دليل متعمق حول ترقيم الصفحات، راجع مستندات Laravel.
sliceBefore قم بتقسيم القيم من المجموعة قبل أن يكون رد الاتصال المحدد صحيحًا. إذا تم تمرير المعلمة الاختيارية $preserveKeys كـ true ، فسوف تحافظ على المفاتيح الأصلية.
collect ([ 20 , 51 , 10 , 50 , 66 ])-> sliceBefore ( function ( $ item ) {
return $ item > 50 ;
}); // return collect([[20],[51, 10, 50], [66])tail استخراج الذيل من المجموعة. لذلك كل شيء ما عدا العنصر الأول. إنه اختصار slice(1)->values() ، ولكنه مع ذلك مفيد جدًا. إذا تم تمرير المعلمة الاختيارية $preserveKeys كـ true ، فستحتفظ بالمفاتيح وترجع إلى slice(1) .
collect ([ 1 , 2 , 3 ])-> tail (); // return collect([2, 3])toPairsتحويل مجموعة إلى مجموعة مع أزواج.
$ collection = collect ([ ' a ' => ' b ' , ' c ' => ' d ' , ' e ' => ' f ' ])-> toPairs ();
$ collection -> toArray (); // returns ['a', 'b'], ['c', 'd'], ['e', 'f']transposeالهدف من النقل هو تدوير مصفوفة متعددة الأبعاد، وتحويل الصفوف إلى أعمدة والأعمدة إلى صفوف.
collect ([
[ ' Jane ' , ' Bob ' , ' Mary ' ],
[ ' [email protected] ' , ' [email protected] ' , ' [email protected] ' ],
[ ' Doctor ' , ' Plumber ' , ' Dentist ' ],
])-> transpose ()-> toArray ();
// [
// ['Jane', '[email protected]', 'Doctor'],
// ['Bob', '[email protected]', 'Plumber'],
// ['Mary', '[email protected]', 'Dentist'],
// ]try إذا قامت أي من التوابع الموجودة بين try catch بإلقاء استثناء، فيمكن معالجة الاستثناء في catch .
collect ([ ' a ' , ' b ' , ' c ' , 1 , 2 , 3 ])
-> try ()
-> map ( fn ( $ letter ) => strtoupper ( $ letter ))
-> each ( function () {
throw new Exception ( ' Explosions in the sky ' );
})
-> catch ( function ( Exception $ exception ) {
// handle exception here
})
-> map ( function () {
// further operations can be done, if the exception wasn't rethrow in the `catch`
}); بينما يتم تسمية الطرق باسم try / catch للتعرف على لغة PHP، فإن المجموعة نفسها تتصرف مثل معاملة قاعدة البيانات. لذا، عند طرح استثناء، يتم إرجاع المجموعة الأصلية (قبل المحاولة).
يمكنك الوصول إلى المجموعة داخل الالتقاط عن طريق إضافة معلمة ثانية إلى المعالج الخاص بك. يمكنك أيضًا معالجة المجموعة ضمن المصيد عن طريق إرجاع قيمة.
$ collection = collect ([ ' a ' , ' b ' , ' c ' , 1 , 2 , 3 ])
-> try ()
-> map ( function ( $ item ) {
throw new Exception ();
})
-> catch ( function ( Exception $ exception , $ collection ) {
return collect ([ ' d ' , ' e ' , ' f ' ]);
})
-> map ( function ( $ item ) {
return strtoupper ( $ item );
});
// ['D', 'E', 'F']validate يُرجع true إذا كان $callback المعطى صحيحًا لكل عنصر. إذا كان $callback عبارة عن سلسلة أو مصفوفة، فاعتبره بمثابة قاعدة للتحقق من الصحة.
collect ([ ' foo ' , ' foo ' ])-> validate ( function ( $ item ) {
return $ item === ' foo ' ;
}); // returns true
collect ([ ' [email protected] ' , ' bla ' ])-> validate ( ' email ' ); // returns false
collect ([ ' [email protected] ' , ' [email protected] ' ])-> validate ( ' email ' ); // returns trueweightedRandom إرجاع عنصر عشوائي بالوزن. في هذا المثال، يتمتع العنصر الذي a على أكبر فرصة للانتقاء، والعنصر الذي يحتوي على c هو الأقل.
// pass the field name that should be used as a weight
$ randomItem = collect ([
[ ' value ' => ' a ' , ' weight ' => 30 ],
[ ' value ' => ' b ' , ' weight ' => 20 ],
[ ' value ' => ' c ' , ' weight ' => 10 ],
])-> weightedRandom ( ' weight ' );وبدلاً من ذلك، يمكنك تمرير عنصر قابل للاستدعاء للحصول على الوزن.
$ randomItem = collect ([
[ ' value ' => ' a ' , ' weight ' => 30 ],
[ ' value ' => ' b ' , ' weight ' => 20 ],
[ ' value ' => ' c ' , ' weight ' => 10 ],
])-> weightedRandom ( function ( array $ item ) {
return $ item [ ' weight ' ];
});withSizeقم بإنشاء مجموعة جديدة بالكمية المحددة من العناصر.
Collection:: withSize ( 1 )-> toArray (); // return [1];
Collection:: withSize ( 5 )-> toArray (); // return [1,2,3,4,5]; الرجاء مراجعة سجل التغيير لمزيد من المعلومات عما تغير مؤخرًا.
$ composer test يرجى الاطلاع على المساهمة للحصول على التفاصيل.
إذا وجدت خطأً يتعلق بالأمان، فيرجى إرسال بريد إلكتروني إلى العنوان [email protected] بدلاً من استخدام أداة تعقب المشكلات.
Spatie هي وكالة تصميم مواقع الإنترنت مقرها في أنتويرب، بلجيكا. ستجد نظرة عامة على جميع مشاريعنا مفتوحة المصدر على موقعنا.
رخصة معهد ماساتشوستس للتكنولوجيا (MIT). يرجى الاطلاع على ملف الترخيص لمزيد من المعلومات.