PHP code example of colindecarlo / collection
1. Go to this page and download the library: Download colindecarlo/collection library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
colindecarlo / collection example snippets
$imEmpty = new Collection(10);
count($imEmpty);
// 0
$daysOfTheWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
$fromArray = new Collection($daysOfTheWeek);
count($fromArray);
// 7
$occupations = new \SplFixedArray(10);
$occupations[0] = 'Botanist';
$occupations = new Collection($occupations);
count($occupations);
// 1
// using a function name
$words = new Collection(['Lorem', 'ipsum', 'dolor', 'sit' 'amet']);
$shouty = $words->map('strtoupper');
// ['LOREM', 'IPSUM', 'DOLOR', 'SIT' 'AMET'];
// using a callable array
$classesToMock = new Collection(['SomeClass', 'SomeOtherClass', 'YetAnotherClass']);
$mocks = $classesToMack->map(['Mockery', 'mock']);
//[object(Mockery\Mock), object(Mockery\Mock), object(Mockery\Mock)]
// using an anonymous function
$stringyDates = new Collection(['2007-06-08', '2009-05-11', '2014-02-19']);
$dates = $stringyDates->map(function ($date) {
return DateTime::createFromFormat('Y-m-d', $date);
});
// [object(DateTime), object(DateTime), object(DateTime)]
$queueEmail = function ($address) use ($message, $emailQueue) {
$emailQueue->publish(['to' => $address, 'message' => $message]);
};
$adminEmails->each($queueEmail);
$workSchedule = new Collection([
['date' => '2015/04/20', 'start' => '08:00', 'end' => '12:00'],
['date' => '2015/04/21', 'start' => '12:00', 'end' => '17:00'],
['date' => '2015/04/23', 'start' => '08:00', 'end' => '17:00'],
['date' => '2015/04/24', 'start' => '10:00', 'end' => '15:00']
]);
$totalHours = $workSchedule->reduce(function($total, $schedule) {
$start = DateTime::createFromFormat('Y/m/d H:i', $schedule['date'] . ' ' . $schedule['start']);
$end = DateTime::createFromFormat('Y/m/d H:i', $schedule['date'] . ' ' . $schedule['end']);
$hours = $end->diff($start)->h;
return $total + $hours;
});
// 25
$workSchedule = new Collection([
['date' => '2015/04/20', 'start' => '08:00', 'end' => '12:00'],
['date' => '2015/04/21', 'start' => '12:00', 'end' => '17:00'],
['date' => '2015/04/23', 'start' => '08:00', 'end' => '17:00'],
['date' => '2015/04/24', 'start' => '10:00', 'end' => '15:00']
]);
$packALunch = $workSchedule->filter(function($schedule) {
$start = DateTime::createFromFormat('Y/m/d H:i', $schedule['date'] . ' ' . $schedule['start']);
$end = DateTime::createFromFormat('Y/m/d H:i', $schedule['date'] . ' ' . $schedule['end']);
$hours = $end->diff($start)->h;
return $hours > 4;
});
// object(Collection)(
['date' => '2015/04/21', 'start' => '12:00', 'end' => '17:00'],
['date' => '2015/04/23', 'start' => '08:00', 'end' => '17:00'],
['date' => '2015/04/24', 'start' => '10:00', 'end' => '15:00']
)