Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bindings/symfony/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
.phpunit.result.cache
75 changes: 75 additions & 0 deletions bindings/symfony/PACKAGIST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Publishing to Packagist

This guide explains how to publish the `4mritgiri/npdatetime-bundle` to Packagist.

## Prerequisites

1. A GitHub account
2. A Packagist account (https://packagist.org)
3. The bundle pushed to GitHub repository

## One-Time Setup

### 1. Register on Packagist

1. Go to https://packagist.org
2. Sign in with your GitHub account
3. Click "Submit" in the top navigation

### 2. Submit Your Package

1. Enter your GitHub repository URL: `https://github.com/4mritGiri/npdatetime`
2. Click "Check"
3. Packagist will detect the `bindings/symfony/composer.json`
4. Click "Submit"

### 3. Configure GitHub Webhook (Automatic Updates)

Packagist will show you a webhook URL. To configure automatic updates:

1. Go to your GitHub repository settings
2. Navigate to "Webhooks"
3. Click "Add webhook"
4. Paste the Packagist webhook URL
5. Set Content type to `application/json`
6. Select "Just the push event"
7. Click "Add webhook"

## How It Works

- **Automatic Updates**: When you push a new git tag (e.g., `v0.2.1`), GitHub notifies Packagist
- **Version Detection**: Packagist reads your `composer.json` and git tags
- **No Manual Publishing**: Unlike PyPI/npm, no GitHub Actions workflow needed!

## Publishing a New Version

1. Update version in files (if needed)
2. Commit changes
3. Create and push a git tag:
```bash
git tag v0.2.1
git push origin v0.2.1
```
4. Packagist automatically detects the new version (within minutes)

## Manual Update

If automatic webhook isn't configured, you can manually trigger updates:

1. Go to https://packagist.org/packages/4mritgiri/npdatetime-bundle
2. Click "Update" button

## Installation for Users

Once published, users install via Composer:

```bash
composer require 4mritgiri/npdatetime-bundle
```

## Notes

- **Package Name**: Must match `name` in `composer.json` (`4mritgiri/npdatetime-bundle`)
- **Type**: Set to `symfony-bundle` for proper Flex integration
- **Minimum Stability**: Packagist respects semver tags (v1.0.0, v0.2.1, etc.)
- **No Tokens Required**: Packagist pulls from public GitHub - no authentication needed
168 changes: 168 additions & 0 deletions bindings/symfony/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# NPDateTime Symfony Bundle

Symfony bundle for Nepali (Bikram Sambat) date handling, wrapping the `npdatetime` PHP extension.

## Requirements

- PHP 8.0 or higher
- Symfony 6.0+ or 7.0+
- npdatetime PHP extension installed

## Installation

### 1. Install the PHP Extension

First, install the `npdatetime` PHP extension. See [PHP bindings README](../php/README.md) for installation instructions.

### 2. Install the Bundle via Composer

```bash
composer require 4mritgiri/npdatetime-bundle
```

### 3. Enable the Bundle

If you're using Symfony Flex, the bundle will be registered automatically. Otherwise, register it manually in `config/bundles.php`:

```php
<?php

return [
// ...
Npdatetime\Bundle\NpdatetimeBundle::class => ['all' => true],
];
```

## Usage

### Inject the Service

The bundle provides `NepaliDateService` which you can inject into your services or controllers:

```php
<?php

namespace App\Controller;

use Npdatetime\Bundle\Service\NepaliDateService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class DateController extends AbstractController
{
public function __construct(
private NepaliDateService $nepaliDateService
) {
}

public function index(): Response
{
// Get today's Nepali date
$today = $this->nepaliDateService->today();

// Create a specific Nepali date
$date = $this->nepaliDateService->create(2077, 5, 19);

// Convert from Gregorian
$nepaliDate = $this->nepaliDateService->fromGregorian(2020, 9, 4);

// Convert from DateTime
$datetime = new \DateTime('2020-09-04');
$nepaliDate = $this->nepaliDateService->fromDateTime($datetime);

// Convert to DateTime
$gregorianDate = $this->nepaliDateService->toDateTime($nepaliDate);

// Format
$formatted = $this->nepaliDateService->format($date, '%Y-%m-%d');

return $this->json([
'today' => (string) $today,
'date' => (string) $date,
'gregorian' => $gregorianDate->format('Y-m-d'),
'formatted' => $formatted,
]);
}
}
```

### Available Service Methods

#### `NepaliDateService`

- `create(int $year, int $month, int $day): NepaliDate` - Create a new Nepali date
- `fromGregorian(int $year, int $month, int $day): NepaliDate` - Create from Gregorian date
- `fromDateTime(\DateTimeInterface $dateTime): NepaliDate` - Create from PHP DateTime
- `today(): NepaliDate` - Get today's Nepali date
- `toDateTime(NepaliDate $nepaliDate): \DateTime` - Convert to PHP DateTime
- `format(NepaliDate $date, string $format = '%Y-%m-%d'): string` - Format the date

### Direct Extension Usage

You can also use the `NepaliDate` class directly:

```php
<?php

// Create a Nepali date
$date = new \NepaliDate(2077, 5, 19);

// Convert to Gregorian
[$year, $month, $day] = $date->to_gregorian();

// Create from Gregorian
$nepaliDate = \NepaliDate::from_gregorian(2020, 9, 4);

// Get today
$today = \NepaliDate::today();

// Format
echo $date->format('%Y-%m-%d');
```

## Format Strings

The extension supports the following format strings:

| Code | Description | Example |
|------|-------------|---------|
| `%Y` | 4-digit year | 2077 |
| `%m` | 2-digit month | 05 |
| `%d` | 2-digit day | 19 |
| `%B` | Full month name | Bhadra |
| `%b` | Short month name | Bha |

## Example: Date Converter Service

```php
<?php

namespace App\Service;

use Npdatetime\Bundle\Service\NepaliDateService;

class UserDateConverter
{
public function __construct(
private NepaliDateService $nepaliDateService
) {
}

public function convertUserBirthdate(\DateTime $gregorianBirthdate): array
{
$nepaliDate = $this->nepaliDateService->fromDateTime($gregorianBirthdate);

return [
'gregorian' => $gregorianBirthdate->format('Y-m-d'),
'nepali' => $this->nepaliDateService->format($nepaliDate),
'year' => $nepaliDate->get_year(),
'month' => $nepaliDate->get_month(),
'day' => $nepaliDate->get_day(),
];
}
}
```

## License

MIT
42 changes: 42 additions & 0 deletions bindings/symfony/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "4mritgiri/npdatetime-bundle",
"description": "Symfony bundle for Nepali (Bikram Sambat) date handling using npdatetime PHP extension",
"type": "symfony-bundle",
"keywords": [
"symfony",
"bundle",
"nepali",
"bikram-sambat",
"calendar",
"date",
"conversion"
],
"homepage": "https://github.com/4mritGiri/npdatetime",
"license": "MIT",
"authors": [
{
"name": "Amrit Giri",
"email": "amritgiri.dev@gmail.com"
}
],
"require": {
"php": ">=8.0",
"ext-npdatetime_php": "*",
"symfony/framework-bundle": "^6.0 || ^7.0",
"symfony/dependency-injection": "^6.0 || ^7.0",
"symfony/config": "^6.0 || ^7.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^6.0 || ^7.0"
},
"autoload": {
"psr-4": {
"Npdatetime\\Bundle\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.2.x-dev"
}
}
}
11 changes: 11 additions & 0 deletions bindings/symfony/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
_defaults:
autowire: true
autoconfigure: true

Npdatetime\Bundle\Service\:
resource: '../src/Service/*'
public: true

Npdatetime\Bundle\Service\NepaliDateService:
public: true
20 changes: 20 additions & 0 deletions bindings/symfony/src/DependencyInjection/NpdatetimeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Npdatetime\Bundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class NpdatetimeExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../../config')
);
$loader->load('services.yaml');
}
}
9 changes: 9 additions & 0 deletions bindings/symfony/src/NpdatetimeBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Npdatetime\Bundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class NpdatetimeBundle extends Bundle
{
}
62 changes: 62 additions & 0 deletions bindings/symfony/src/Service/NepaliDateService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Npdatetime\Bundle\Service;

/**
* Service wrapper for NepaliDate PHP extension
*/
class NepaliDateService
{
/**
* Create a new Nepali date
*/
public function create(int $year, int $month, int $day): \NepaliDate
{
return new \NepaliDate($year, $month, $day);
}

/**
* Create NepaliDate from Gregorian date
*/
public function fromGregorian(int $year, int $month, int $day): \NepaliDate
{
return \NepaliDate::from_gregorian($year, $month, $day);
}

/**
* Create NepaliDate from DateTime
*/
public function fromDateTime(\DateTimeInterface $dateTime): \NepaliDate
{
return \NepaliDate::from_gregorian(
(int) $dateTime->format('Y'),
(int) $dateTime->format('m'),
(int) $dateTime->format('d')
);
}

/**
* Get today's Nepali date
*/
public function today(): \NepaliDate
{
return \NepaliDate::today();
}

/**
* Convert NepaliDate to DateTime
*/
public function toDateTime(\NepaliDate $nepaliDate): \DateTime
{
[$year, $month, $day] = $nepaliDate->to_gregorian();
return new \DateTime(sprintf('%d-%02d-%02d', $year, $month, $day));
}

/**
* Format a Nepali date
*/
public function format(\NepaliDate $date, string $format = '%Y-%m-%d'): string
{
return $date->format($format);
}
}
Loading