Skip to content

Commit

Permalink
Merge pull request #1 from halaei/master
Browse files Browse the repository at this point in the history
README.md
  • Loading branch information
halaei committed Sep 3, 2015
2 parents 04166fe + 5f4b28b commit 3650b10
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 6 deletions.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Opilo Farsi Tools
This package provides Farsi tools for PHP developers.
## Jalali (Higri Shamsi) Date
The `JalaliDate` class represents Iranian calendar. It calculates leap years based on data referenced in [this wiki page](https://fa.wikipedia.org/wiki/گاه‌شماری_رسمی_ایران).

The following code shows how you can convert a `DateTime` object into a `JalaliDate` one and then print it according to a desired format.
All you need is using methods in `DateConverter` and take a look at `JalaliFormatter::$conversionFunctions` array to know what to pass to `JalaliDate::format()` function as format string.

```php
use Opilo\Farsi\DateConverter;

$dateTime = \DateTime::createFromFormat('Y-m-d', '2015-09-03');

$jalaliDate = DateConverter::dateTimeToJalali($dateTime);

print ($jalaliDate->format('D S M ماه سال X'));
```

And the output will be: **پنج‌شنبه دوازدهم شهریور ماه سال یک هزار و سیصد و نود و چهار**

The following sample code shows how to convert numeric inputs representing a Jalali date into a `JalaliDate` and then convert it into `DateTime`.
This may be helpful if you want to validate user's input Jalali date, and then save the appropriate standard timestamp into database.

```php
use Opilo\Farsi\JalaliDate;

$jalaliDate = new JalaliDate(1394, 6, 12);

$dateTime = $jalaliDate->toDateTime();

print($dateTime->format('Y-m-d'));
```

And the output will be: **2015-09-03**

Note that if you try to construct an invalid `JalaliDate`, an `InvalidArgumentException` will be thrown.

## Number to String Converter
If your application is supposed to print financial documents, you probably love this:

```php
use Opilo\Farsi\NumberToStringConverter;

print NumberToStringConverter::toString(21034510);
```
because, this is the output: **بیست و یک میلیون و سی و چهار هزار و پانصد و ده**

And, here is the second note **(نکته‌ی دوم)**:

```php
print ('نکته‌ی ' . NumberToStringConverter::toOrdinalString(2));
```

## String Cleanser
With `StringCleanser` class you can deal with Farsi digits while interacting with the users.
`StringCleanser::arabicToFarsi()` function cleans input Farsi strings out of Arabic characters that commonly presents in standard keywords.

```php
use Opilo\Farsi\StringCleaner;

print(StringCleaner::digitsToFarsi('1394'));
print("\n");

print(StringCleaner::digitsToEnglish('۱۳۹۴'));
print("\n");

print(StringCleaner::arabicToFarsi('كيك پي اچ پي چيست؟'));
print("\n");
```

This will (approximately) output:

۱۳۹۴
1394
کیک پی اچ پی چیز خاصی نیست

## License

The Opilo Farsi package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
56 changes: 56 additions & 0 deletions tests/SampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace OpiloTest\Farsi;

use Opilo\Farsi\DateConverter;
use Opilo\Farsi\JalaliDate;
use Opilo\Farsi\NumberToStringConverter;
use Opilo\Farsi\StringCleaner;

class SampleTest
{
public function sample_1()
{
$dateTime = \DateTime::createFromFormat('Y-m-d', '2015-09-03');

$jalaliDate = DateConverter::dateTimeToJalali($dateTime);

print("\n");
print ($jalaliDate->format('D S M ماه سال X'));

}

public function sample_2()
{
$jalaliDate = new JalaliDate(1394, 6, 12);
$dateTime = $jalaliDate->toDateTime();
print("\n");
print($dateTime->format('Y-m-d'));
print("\n");
}

public function sample_3()
{
print("\n");
print NumberToStringConverter::toString(21034510);
print("\n");
}

public function sample_4()
{
print("\n");
print ('نکته‌ی ' . NumberToStringConverter::toOrdinalString(2));
print("\n");
}

public function sample_5()
{
print(StringCleaner::digitsToFarsi('1394'));
print("\n");
print(StringCleaner::digitsToEnglish('۱۳۹۴'));
print("\n");
print(StringCleaner::arabicToFarsi('كيك پي اچ پي چيست؟'));
print("\n");
}

}
12 changes: 6 additions & 6 deletions tests/StringCleanerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function setUp()

public function test_convert_digits_to_Farsi()
{
$this->assertEquals('۰۹۱۳۴۱۰۷۶۷۲' ,$this->cleaner->digitsToFarsi('09134107672'));
$this->assertEquals('۰۹۱۳۴۱۰۷۶۷۲' ,$this->cleaner->digitsToFarsi('091341۰7672'));
$this->assertEquals('۰۹۱۳۴۱۰۷۶۷۲abcd' ,$this->cleaner->digitsToFarsi('091341۰7672abcd'));
$this->assertEquals('۰۹۱۳۴۱۰۷' ,$this->cleaner->digitsToFarsi('09134107'));
$this->assertEquals('۰۹۱۳۴۱۰۷' ,$this->cleaner->digitsToFarsi('091341۰7'));
$this->assertEquals('۰۹۱۳۴۱۰۷abcd' ,$this->cleaner->digitsToFarsi('091341۰7abcd'));
}

public function test_convert_Arabic_to_Farsi()
Expand All @@ -32,8 +32,8 @@ public function test_convert_Arabic_to_Farsi()

public function test_convert_digits_to_English()
{
$this->assertEquals('09134107672',$this->cleaner->digitsToEnglish('۰۹۱۳۴۱۰۷۶۷۲'));
$this->assertEquals('09134107672' ,$this->cleaner->digitsToEnglish('۰۹۱۳۴۱0۷۶۷۲'));
$this->assertEquals('09134107672abcd' ,$this->cleaner->digitsToEnglish('۰۹۱۳۴۱0۷۶۷۲abcd'));
$this->assertEquals('09134107',$this->cleaner->digitsToEnglish('۰۹۱۳۴۱۰۷'));
$this->assertEquals('09134107' ,$this->cleaner->digitsToEnglish('۰۹۱۳۴۱0۷'));
$this->assertEquals('09134107abcd' ,$this->cleaner->digitsToEnglish('۰۹۱۳۴۱0۷abcd'));
}
}

0 comments on commit 3650b10

Please sign in to comment.