API Reference
Availability & Conflicts
Managing availability and handling schedule conflicts.
Checking Availability
Basic Availability Check
// Check if available at specific time
$available = $user->isAvailableAt('2025-03-15', '14:00', '16:00');
if ($available) {
// User is available, proceed with booking
}
Getting Available Slots
// Get available time slots for a day
$slots = $user->getAvailableSlots(
date: '2025-03-15',
dayStart: '09:00',
dayEnd: '17:00',
slotDuration: 60 // 1-hour slots
);
foreach ($slots as $slot) {
echo "Available: {$slot['start']} - {$slot['end']}";
}
Finding Next Available Slot
// Find next available slot after a specific date
$nextSlot = $user->getNextAvailableSlot(
afterDate: '2025-03-15',
duration: 120, // 2 hours needed
dayStart: '09:00',
dayEnd: '17:00'
);
if ($nextSlot) {
echo "Next available: {$nextSlot['date']} from {$nextSlot['start']} to {$nextSlot['end']}";
}
Conflict Management
Automatic Conflict Detection
use Zap\Exceptions\ScheduleConflictException;
try {
$schedule = Zap::for($user)
->named('Important Meeting')
->from('2025-03-15')
->addPeriod('14:00', '16:00')
->noOverlap() // Enable conflict detection
->save();
} catch (ScheduleConflictException $e) {
// Handle conflicts gracefully
$conflicts = $e->getConflictingSchedules();
foreach ($conflicts as $conflict) {
echo "Conflicts with: {$conflict->name}";
}
}
Manual Conflict Checking
// Check for conflicts before saving
$conflicts = Zap::findConflicts($schedule);
if (!empty($conflicts)) {
// Handle conflicts
foreach ($conflicts as $conflict) {
echo "Conflict detected with: {$conflict->name}";
}
} else {
// Safe to proceed
$schedule->save();
}
Business Rules
Working Hours Validation
The
workingHoursOnly()
method requires enabling the working_hours
validation rule in your config file.$schedule = Zap::for($user)
->named('Client Meeting')
->from('2025-03-15')
->addPeriod('14:00', '16:00')
->workingHoursOnly('09:00', '18:00') // Business hours only
->save();
Duration Limits
The
maxDuration()
method requires enabling the max_duration
validation rule in your config file.$schedule = Zap::for($user)
->named('Long Meeting')
->from('2025-03-15')
->addPeriod('14:00', '18:00')
->maxDuration(240) // Maximum 4 hours
->save();
Rule Override
// Emergency override - bypass overlap validation
$emergency = Zap::for($user)
->named('Emergency Meeting')
->from('2025-03-15')
->addPeriod('10:30', '12:00')
->withRule('no_overlap', ['enabled' => false])
->save();
Real-World Example: Meeting Room
// Room availability
$roomAvailability = Zap::for($room)
->named('Conference Room A')
->availability()
->from('2025-01-01')->to('2025-12-31')
->addPeriod('08:00', '18:00')
->weekly(['monday', 'tuesday', 'wednesday', 'thursday', 'friday'])
->save();
// Check if room is available
$isAvailable = $room->isAvailableAt('2025-03-15', '09:00', '11:00');
if ($isAvailable) {
// Book the room
$meeting = Zap::for($room)
->named('Board Meeting')
->appointment()
->from('2025-03-15')
->addPeriod('09:00', '11:00')
->withMetadata([
'organizer' => 'john@company.com',
'equipment' => ['projector', 'whiteboard']
])
->save();
}