adachsoft / calendar
Calendar logic for adachsoft/calendar-contracts: calendar service with access policy, recurring entries expanded into occurrences for day/week/month views, shared with accounts and groups via principal-contracts.
Requires
- php: ^8.3
- adachsoft/calendar-contracts: ^0.1
- adachsoft/principal-contracts: ^0.2
- adachsoft/recurrence: ^0.1
- adachsoft/recurrence-contracts: ^0.1
- adachsoft/reference-contracts: ^0.1
- psr/clock: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/calendar is the application service for calendars and recurrence-aware entries. It enforces calendar access rules, persists changes through repository contracts, expands occurrences for date-range queries, and publishes change notifications. Storage and framework integration are provided by adapters, not by this package.
Requirements and installation
- PHP 8.3 or later
adachsoft/calendar-contractsadachsoft/recurrence-contractsandadachsoft/recurrenceadachsoft/principal-contractsadachsoft/reference-contractspsr/clock
Install with Composer:
composer require adachsoft/calendar
Usage
Quick start
The in-memory repositories are useful for tests and short-lived examples. They do not persist data between processes.
<?php
declare(strict_types=1);
use AdachSoft\Calendar\CalendarServiceFactory;
use AdachSoft\CalendarContracts\InMemory\InMemoryCalendarRepository;
use AdachSoft\CalendarContracts\InMemory\InMemoryEntryRepository;
use AdachSoft\PrincipalContracts\CompositeMembershipResolver;
use AdachSoft\ReferenceContracts\PrincipalReference;
$service = CalendarServiceFactory::create(
new InMemoryCalendarRepository(),
new InMemoryEntryRepository(),
new CompositeMembershipResolver([]),
);
$owner = new PrincipalReference('account', 'alice');
$calendar = $service->createCalendar($owner, 'Personal', new \DateTimeZone('UTC'));
See the family calendar example for sharing with a group, adding weekly and all-day entries, querying a week, and cancelling one occurrence.
Access rules
| Principal | View | Edit entries | Manage calendar |
|---|---|---|---|
| Owner (including a member of an owner group) | Yes | Yes | Yes |
| Shared principal or its transitive members | Yes | Yes | No |
| Other principals | No | No | No |
The default MembershipCalendarAccessPolicy resolves the actor and its direct or transitive principal memberships through PrincipalMembershipResolverInterface. Calendar ownership may belong to an account or a group.
No-op behavior
An unchanged trimmed calendar name, sharing with the owner or an existing recipient, unsharing a non-recipient, an entry update with unchanged values, moving an entry to its current calendar, and cancelling an already cancelled occurrence are no-ops. They return the current DTO without incrementing its version, saving it, or notifying the listener. Passing null for entry metadata during an update keeps the stored metadata.
Change notifications
An optional CalendarChangeListenerInterface receives notifications after successful persistence. Creation notifications use a null before value; deletion notifications use a null after value. Deleting a calendar notifies the listener about each removed entry and then the calendar. No-op operations do not notify it.
Storage adapters
Implement CalendarRepositoryInterface and EntryRepositoryInterface to connect a database or another storage engine. Repositories own persistence and optimistic concurrency; the service package does not include a production storage adapter. Adapter authors can use the reusable repository contract tests provided by adachsoft/calendar-contracts/testing.
Custom infrastructure
CalendarServiceFactory::create() accepts custom change listeners, PSR-20 clocks, identifier generators, and recurrence calculators. To use a custom CalendarAccessPolicyInterface or different recurrence limits, construct CalendarService with the desired policy, ActivePeriodCalculator, and OccurrenceExpander.
Out of scope
This package does not edit an individual occurrence independently of its recurrence rule, manage invitations, synchronize with external calendars, or provide a web/mobile API. External calendar synchronization and application-specific invitation flows belong in adapters or higher-level services.