omnicolor/calendar

用于处理带有笔记和事件的日历的包

dev-main 2024-08-01 02:49 UTC

This package is auto-updated.

Last update: 2024-09-12 13:48:28 UTC


README

Code Coverage Badge

此包允许存储与日期相关联的任意数据。在我特定的用例中,我想要能够存储关于角色扮演游戏中发生的事情的信息,以便渲染到目前为止游戏的丰富历史。

用法

此示例使用stdClass来表示任意数据,但也可以是PHP类。存储到数据库中可以通过将对象转换为JSON表示形式或序列化来实现,具体取决于您特定用例的最佳做法。

<?php

declare(strict_types=1);

use Carbon\CarbonImmutable as Date;
use Omnicolor\Calendar\Calendar;

// Create a calendar for January 2080.
$calendar = new Calendar(new Date('2080-01-01'), new Date('2080-01-31'));

// Add some random journal entries.
$calendar[new Date('2080-01-01')] = (object)[
    'description' => 'Feeling rather tired after the New Years party.',
    'mood' => 3,
];
$calendar[new Date('2080-01-02')] = (object)[
    'description' => 'Starting the new year off with a job opportunity. '
        . 'The pay looks promising.',
    'mood' => 10,
    'potentialPay' => 20000,
];
$calendar[new Date('2080-01-08')] = (object)[
    'description' => 'Should have known there would be a double-cross. '
        . 'Not only did we not get paid, but we lost three hundred!',
    'mood' => 1,
    'pay' => -300,
];

foreach ($calendar as $date => $entry) {
    // Render calendar however you see fit. If a $date
    // doesn't have an entry, $entry will be null.
}

渲染

如果您想在网页上渲染普通日历,有一个示例装饰器会将日历的日期包裹在 <div> 标签中,您可以按需进行样式设计。它将生成类似的内容

<div class="calendar">
    <div class="week">
        <div class="day">1</div>
        <div class="day">2</div>
        <div class="day">3</div>
        <div class="day">4</div>
        <div class="day">5</div>
        <div class="day">6</div>
        <div class="day">7</div>
    </div>
    ...
</div>

如果您想渲染日历的条目数据,您需要实现类似于 BasicHtmlDecorator 的功能,以处理您特定的数据。

测试

您可以使用 composer test 运行日历的单元测试,或者如果您想查看代码覆盖率报告,请使用 composer coverage。代码风格(composer cs-fix)和静态分析(composer phpstan)也是可用的。composer all 会运行带有覆盖率、代码风格和静态分析的测试。