Merge pull request #111 from iamfarhad/master

add github workflow and allow laravel 9
This commit is contained in:
hekmatinasser authored and GitHub committed 2022-02-10 13:46:20 +03:30
commit 2765a20308
37 files changed
+302 -3067

No files matched your search

+1
View File
@@ -0,0 +1 @@
github: hekmatinasser
+23
View File
@@ -0,0 +1,23 @@
name: Check & fix styling
on: [push]
jobs:
php-cs-fixer:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}
- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs.dist.php --allow-risky=yes
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
+53
View File
@@ -0,0 +1,53 @@
name: run-tests
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.1, 8.0, 7.3, 7.4]
laravel: [6.*,8.*,9.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 8.*
testbench: ^6.23
- laravel: 6.*
testbench: ^4.9
- laravel: 9.*
testbench: 7.*
exclude:
- php: 8.1
laravel: 6.*
- laravel: 9.*
php: 7.3
- laravel: 9.*
php: 7.4
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none
- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
+3
View File
@@ -2,3 +2,6 @@ vendor/
.idea/
*.phpunit.result.cache
*.bat
composer.lock
phpunit.xml.dist.bak
.php-cs-fixer.cache
+40
View File
@@ -0,0 +1,40 @@
<?php
$finder = Symfony\Component\Finder\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method' => 'one',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
-38
View File
@@ -1,38 +0,0 @@
language: php
sudo: true
matrix:
include:
- php: 7.2
env: LARAVEL_VERSION=5.1.*
- php: 7.2
env: LARAVEL_VERSION=5.3.*
- php: 7.2
env: LARAVEL_VERSION=5.5.*
- php: 7.2
env: LARAVEL_VERSION=5.6.*
- php: 7.2
env: LARAVEL_VERSION=5.7.*
- php: 7.2
env: LARAVEL_VERSION=5.7.*
- php: 7.3
env: LARAVEL_VERSION=5.7.*
- php: 7.2
env: LARAVEL_VERSION=5.8.*
- php: 7.3
env: LARAVEL_VERSION=5.8.*
before_install:
- composer self-update --stable -n
install:
- travis_retry composer install --no-suggest
script:
- vendor/bin/phpunit --coverage-clover clover.xml;
after_script:
- wget https://scrutinizer-ci.com/ocular.phar;
- php ocular.phar code-coverage:upload --format=php-clover clover.xml;
- bash <(curl -s https://codecov.io/bash)
+6 -5
View File
@@ -10,13 +10,14 @@
],
"keywords": ["Laravel","Date","Datetime","Jalali", "Shamsi", "Verta"],
"require": {
"php": "^7.2.5|^8.0",
"hekmatinasser/notowo": "^1.0",
"illuminate/validation": "^5|^6|^7|^8",
"illuminate/support": "^5|^6|^7|^8"
"php": "^7.3|^8.0|^8.1",
"illuminate/support": "^6.0|^8.0|^9.0",
"illuminate/validation": "^6.0|^8.0|^9.0",
"hekmatinasser/notowo": "^1.0"
},
"require-dev" : {
"phpunit/phpunit": "7.2.*",
"orchestra/testbench": "^4.9|^6.23|^7.0",
"phpunit/phpunit": "^9.4",
"mockery/mockery": "^1.0"
},
"autoload": {
Generated
-2830
View File
File diff suppressed because it is too large. Load diff
+9 -24
View File
@@ -1,31 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Verta Test Suite">
<directory suffix="Test.php">./tests</directory>
<testsuite name="Library Program Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<!--
<env name="REDIS_HOST" value="127.0.0.1" />
<env name="REDIS_PORT" value="6379" />
-->
<env name="locale" value="en" />
</php>
</phpunit>
+1
View File
@@ -1,4 +1,5 @@
<?php
namespace Hekmatinasser\Verta\Facades;
use Illuminate\Support\Facades\Facade;
+2 -3
View File
@@ -45,7 +45,7 @@ return [
'month' => 'شهر',
'yaer' => 'سنة',
'decade' => 'عشر سنوات',
'century' => 'مئة عام'
'century' => 'مئة عام',
],
'phrase' => [
'am' => 'ق ظ',
@@ -78,6 +78,5 @@ return [
'2' => 'الصيف',
'3' => 'الخريف',
'4' => 'شتاء',
]
],
];
+2 -3
View File
@@ -45,7 +45,7 @@ return [
'month' => 'ماه',
'yaer' => 'سال',
'decade' => 'دهه',
'century' => 'قرن'
'century' => 'قرن',
],
'phrase' => [
'am' => 'ق ظ',
@@ -78,6 +78,5 @@ return [
'2' => 'تابستان',
'3' => 'پاییز',
'4' => 'زمستان',
]
],
];
+2 -3
View File
@@ -45,7 +45,7 @@ return [
'month' => 'month',
'yaer' => 'yaer',
'decade' => 'decade',
'century' => 'century'
'century' => 'century',
],
'phrase' => [
'am' => 'am',
@@ -78,6 +78,5 @@ return [
'2' => 'summer',
'3' => 'autumn',
'4' => 'winter',
]
],
];
+2 -3
View File
@@ -45,7 +45,7 @@ return [
'month' => 'ماه',
'yaer' => 'سال',
'decade' => 'دهه',
'century' => 'قرن'
'century' => 'قرن',
],
'phrase' => [
'am' => 'ق ظ',
@@ -78,6 +78,5 @@ return [
'2' => 'تابستان',
'3' => 'پاییز',
'4' => 'زمستان',
]
],
];
+2 -3
View File
@@ -45,7 +45,7 @@ return [
'month' => 'ماه',
'yaer' => 'سال',
'decade' => 'دهه',
'century' => 'قرن'
'century' => 'قرن',
],
'phrase' => [
'am' => 'ق ظ',
@@ -78,6 +78,5 @@ return [
'2' => 'تابستان',
'3' => 'پاییز',
'4' => 'زمستان',
]
],
];
+2 -3
View File
@@ -45,7 +45,7 @@ return [
'month' => 'ماه',
'yaer' => 'سال',
'decade' => 'دهه',
'century' => 'قرن'
'century' => 'قرن',
],
'phrase' => [
'am' => 'ق ظ',
@@ -78,6 +78,5 @@ return [
'2' => 'تابستان',
'3' => 'پاییز',
'4' => 'زمستان',
]
],
];
+2 -3
View File
@@ -44,7 +44,7 @@ return [
'week' => 'هفته',
'month' => 'ماه',
'yaer' => 'سال',
'century' => 'قرن'
'century' => 'قرن',
],
'phrase' => [
'am' => 'ق ظ',
@@ -77,6 +77,5 @@ return [
'2' => 'تابستان',
'3' => 'پاییز',
'4' => 'زمستان',
]
],
];
+30 -2
View File
@@ -3,8 +3,6 @@
namespace Hekmatinasser\Verta\Laravel;
use Hekmatinasser\Verta\Verta;
use Illuminate\Support\Arr;
use phpDocumentor\Reflection\Types\Static_;
class JalaliValidator
{
@@ -14,11 +12,13 @@ class JalaliValidator
return false;
}
$format = count($parameters) ? $parameters[0] : 'Y/m/d';
try {
Verta::parseFormat($format, $value);
} catch (\Exception $e) {
return false;
}
return true;
}
@@ -28,8 +28,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->eq($base);
} catch (\Exception $e) {
return false;
@@ -42,8 +44,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->ne($base);
} catch (\Exception $e) {
return false;
@@ -57,11 +61,13 @@ class JalaliValidator
}
$format = count($parameters) ? $parameters[0] : 'Y/m/d H:i:s';
try {
Verta::parseFormat($format, $value);
} catch (\Exception $e) {
return false;
}
return true;
}
@@ -71,8 +77,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->eq($base);
} catch (\Exception $e) {
return false;
@@ -85,8 +93,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->ne($base);
} catch (\Exception $e) {
return false;
@@ -99,8 +109,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->gt($base);
} catch (\Exception $e) {
return false;
@@ -113,8 +125,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->gte($base);
} catch (\Exception $e) {
return false;
@@ -127,8 +141,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->gt($base);
} catch (\Exception $e) {
return false;
@@ -141,8 +157,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->gte($base);
} catch (\Exception $e) {
return false;
@@ -155,8 +173,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->lt($base);
} catch (\Exception $e) {
return false;
@@ -169,8 +189,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->lte($base);
} catch (\Exception $e) {
return false;
@@ -183,8 +205,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->lt($base);
} catch (\Exception $e) {
return false;
@@ -197,8 +221,10 @@ class JalaliValidator
return false;
}
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
try {
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
return Verta::parseFormat($format, $value)->lte($base);
} catch (\Exception $e) {
return false;
@@ -219,6 +245,7 @@ class JalaliValidator
$to = Verta::getMessages();
$date = str_replace(array_values($en['numbers']), array_values($to['numbers']), $date);
}
return str_replace(':date', $date, $message);
}
@@ -231,6 +258,7 @@ class JalaliValidator
$to = Verta::getMessages();
$date = str_replace(array_values($en['numbers']), array_values($to['numbers']), $date);
}
return str_replace(':date', $date, $message);
}
}
+16 -20
View File
@@ -8,63 +8,62 @@ use Illuminate\Support\ServiceProvider;
class VertaServiceProvider extends ServiceProvider
{
private $rules = [
'jdate' => [
'extend' => 'validateDate',
'replacer' => 'replaceDateOrDatetime'
'replacer' => 'replaceDateOrDatetime',
],
'jdate_equal' => [
'extend' => 'validateDateEqual',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
'replacer' => 'replaceDateAfterOrBeforeOrEqual',
],
'jdate_not_equal' => [
'extend' => 'validateDateNotEqual',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
'replacer' => 'replaceDateAfterOrBeforeOrEqual',
],
'jdatetime' => [
'extend' => 'validateDateTime',
'replacer' => 'replaceDateOrDatetime'
'replacer' => 'replaceDateOrDatetime',
],
'jdatetime_equal' => [
'extend' => 'validateDateTimeEqual',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual',
],
'jdatetime_not_equal' => [
'extend' => 'validateDateTimeNotEqual',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual',
],
'jdate_after' => [
'extend' => 'validateDateAfter',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
'replacer' => 'replaceDateAfterOrBeforeOrEqual',
],
'jdate_after_equal' => [
'extend' => 'validateDateAfterEqual',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
'replacer' => 'replaceDateAfterOrBeforeOrEqual',
],
'jdatetime_after' => [
'extend' => 'validateDateTimeAfter',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual',
],
'jdatetime_after_equal' => [
'extend' => 'validateDateTimeAfterEqual',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual',
],
'jdate_before' => [
'extend' => 'validateDateBefore',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
'replacer' => 'replaceDateAfterOrBeforeOrEqual',
],
'jdate_before_equal' => [
'extend' => 'validateDateBeforeEqual',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
'replacer' => 'replaceDateAfterOrBeforeOrEqual',
],
'jdatetime_before' => [
'extend' => 'validateDateTimeBefore',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual',
],
'jdatetime_before_equal' => [
'extend' => 'validateDateTimeBeforeEqual',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual',
],
];
@@ -86,11 +85,10 @@ class VertaServiceProvider extends ServiceProvider
public function register()
{
$this->app->bind('verta', function ($app) {
return new Verta;
return new Verta();
});
}
/**
* Get the services provided by the provider.
*
@@ -98,7 +96,7 @@ class VertaServiceProvider extends ServiceProvider
*/
public function provides()
{
return array('verta');
return ['verta'];
}
public function loadValidators()
@@ -106,9 +104,7 @@ class VertaServiceProvider extends ServiceProvider
$className = JalaliValidator::class . '@';
foreach ($this->rules as $name => $methods) {
if (array_key_exists('extend', $methods) || array_key_exists('replacer', $methods)) {
Validator::extend($name, $className . $methods['extend']);
Validator::replacer($name, $className . $methods['replacer']);
+5 -6
View File
@@ -1,16 +1,13 @@
<?php
namespace Hekmatinasser\Verta\Traits;
use DateTimeZone;
use Hekmatinasser\Verta\Verta;
use InvalidArgumentException;
trait Accessor
{
/**
* Get a part of the Verta object
*
@@ -22,7 +19,7 @@ trait Accessor
*/
public function __get($name)
{
static $formats = array(
static $formats = [
'year' => 'Y',
'month' => 'n',
'day' => 'j',
@@ -35,7 +32,7 @@ trait Accessor
'weekOfYear' => 'W',
'daysInMonth' => 't',
'timestamp' => 'U',
);
];
switch (true) {
case isset($formats[$name]):
@@ -90,15 +87,18 @@ trait Accessor
list($year, $month, $day, $hour, $minute, $second) = explode('-', $this->format('Y-n-j-G-i-s'));
$$name = $value;
$this->setDateTime($year, $month, $day, $hour, $minute, $second);
break;
case 'timestamp':
$this->setTimestamp($value);
break;
case 'timezone':
case 'tz':
$this->setTimezone(new DateTimeZone($value));
break;
default:
@@ -268,5 +268,4 @@ trait Accessor
return $this->setTime($hour, $minute, $second, 0);
}
}
-3
View File
@@ -1,9 +1,7 @@
<?php
namespace Hekmatinasser\Verta\Traits;
trait Boundaries
{
/**
@@ -159,5 +157,4 @@ trait Boundaries
return $this->setDateTime($this->year, 12, $day, 23, 59, 59);
}
}
+9 -8
View File
@@ -1,21 +1,19 @@
<?php
namespace Hekmatinasser\Verta\Traits;
use Hekmatinasser\Verta\Verta;
trait Comparison
{
/**
* check jalali the instance is a leap year
*
* @param int $year
* @return bool
*/
public static function isLeapYear($year) {
public static function isLeapYear($year)
{
return in_array(($year % 33), [1 , 5 , 9 , 13 ,17 , 22 , 26 , 30]);
}
@@ -27,7 +25,8 @@ trait Comparison
* @param int $year
* @return bool
*/
public static function isValidDate($year, $month, $day) {
public static function isValidDate($year, $month, $day)
{
if ($year < 0 || $year > 32766) {
return false;
}
@@ -38,6 +37,7 @@ trait Comparison
if ($day < 1 || $day > $dayLastMonthJalali) {
return false;
}
return true;
}
@@ -49,7 +49,8 @@ trait Comparison
* @param int $second
* @return bool
*/
public static function isValidTime($hour, $minute, $second) {
public static function isValidTime($hour, $minute, $second)
{
return $hour >= 0 && $hour <= 23
&& $minute >= 0 && $minute <= 59
&& $second >= 0 && $second <= 59;
@@ -496,7 +497,8 @@ trait Comparison
/**
* Checks if the passed in date is in the same month as the instance month (and year if needed).
*
* $this->assertFalse($datetime->isCurrentMonth());
* @param Verta|null $v The instance to compare with or null to use current day.
* @param bool $ofSameYear Check if it is the same month in the same year.
*
@@ -602,5 +604,4 @@ trait Comparison
{
return $this->dayOfWeek === static::FRIDAY;
}
}
+25 -26
View File
@@ -1,6 +1,5 @@
<?php
namespace Hekmatinasser\Verta\Traits;
use DateTime;
@@ -10,7 +9,6 @@ use InvalidArgumentException;
trait Creator
{
/**
* create object of Jalali
*
@@ -22,14 +20,11 @@ trait Creator
{
if (empty($datetime)) {
$datetime = 'now';
}
elseif (is_string($datetime)) {
} elseif (is_string($datetime)) {
$datetime = self::faToEnNumbers(self::arToEnNumbers($datetime));
}
elseif ($datetime instanceof DateTime) {
} elseif ($datetime instanceof DateTime) {
$datetime = "@{$datetime->getTimestamp()}";
}
elseif (is_int($datetime)) {
} elseif (is_int($datetime)) {
$datetime = "@$datetime";
}
@@ -49,7 +44,8 @@ trait Creator
* @param null $timezone
* @return static
*/
public static function now($timezone = null) {
public static function now($timezone = null)
{
return new static(null, $timezone);
}
@@ -60,7 +56,6 @@ trait Creator
*
* @return static
*/
public static function today($timezone = null)
{
return (new static($timezone))->startDay();
@@ -98,7 +93,8 @@ trait Creator
*
* @return static
*/
public static function instance($datetime = null, $timezone = null) {
public static function instance($datetime = null, $timezone = null)
{
return new static($datetime, $timezone);
}
@@ -127,7 +123,8 @@ trait Creator
*
* @return datetime $datetime
*/
public function datetime() {
public function datetime()
{
return new DateTime(date('Y-m-d H:i:s', $this->getTimestamp()), $this->getTimezone());
}
@@ -142,8 +139,12 @@ trait Creator
public static function parse($datetime, $timezone = null)
{
self::loadMessages();
$names = array_map(function ($value) { return " $value "; }, array_values(self::$messages['year_months']));
$values = array_map(function ($value) { return "-$value-"; }, range(1,12));
$names = array_map(function ($value) {
return " $value ";
}, array_values(self::$messages['year_months']));
$values = array_map(function ($value) {
return "-$value-";
}, range(1, 12));
$formatted = str_replace($names, $values, $datetime);
$formatted = str_replace(array_values(self::$messages['year_months']), range(1, 12), $formatted);
@@ -153,9 +154,10 @@ trait Creator
}
list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']);
list($hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']);
list($hour, $minute, $second) = [$parse['hour'], $parse['minute'], $parse['second']];
$datetime = sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second);
return new static($datetime, $timezone);
}
@@ -177,9 +179,10 @@ trait Creator
throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
}
list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']);
list($hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']);
list($hour, $minute, $second) = [$parse['hour'], $parse['minute'], $parse['second']];
$datetime = sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second);
return new static($datetime, $timezone);
}
@@ -251,14 +254,13 @@ trait Creator
* @param null $timezone
* @return DateTimeZone|null
*/
protected static function createTimeZone($timezone = null) {
protected static function createTimeZone($timezone = null)
{
if ($timezone === null) {
$tz = new DateTimeZone(date_default_timezone_get());
}
elseif ($timezone instanceof DateTimeZone) {
} elseif ($timezone instanceof DateTimeZone) {
$tz = $timezone;
}
else {
} else {
$tz = @timezone_open(strval($timezone));
if ($tz === false) {
throw new InvalidArgumentException(sprintf("Unknown timezone '%s'", $tz));
@@ -286,9 +288,8 @@ trait Creator
*/
public static function createGregorian($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null)
{
$now = (new DateTime())->format('Y-n-j-G-i-s');
$defaults = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode('-', $now));
$defaults = array_combine(['year', 'month', 'day', 'hour', 'minute', 'second'], explode('-', $now));
$year = $year ?? intval($defaults['year']);
$month = $month ?? intval($defaults['month']);
@@ -350,11 +351,10 @@ trait Creator
*
* @return static
*/
public static function createJalali($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null)
{
$now = (new static())->format('Y-n-j-G-i-s');
$defaults = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode('-', $now));
$defaults = array_combine(['year', 'month', 'day', 'hour', 'minute', 'second'], explode('-', $now));
$year = $year ?? intval($defaults['year']);
$month = $month ?? intval($defaults['month']);
@@ -399,5 +399,4 @@ trait Creator
{
return static::createJalali(null, null, null, $hour, $minute, $second, $timezone);
}
}
-3
View File
@@ -1,9 +1,7 @@
<?php
namespace Hekmatinasser\Verta\Traits;
use Hekmatinasser\Verta\VertaInterface;
trait Date
@@ -59,5 +57,4 @@ trait Date
* @var array
*/
protected static $daysMonthJalali = [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29];
}
-4
View File
@@ -1,14 +1,11 @@
<?php
namespace Hekmatinasser\Verta\Traits;
use Hekmatinasser\Verta\Verta;
trait Difference
{
/**
* Get the difference in years
*
@@ -100,5 +97,4 @@ trait Difference
return $v->getTimestamp() - $this->getTimestamp();
}
}
+25 -19
View File
@@ -1,15 +1,12 @@
<?php
namespace Hekmatinasser\Verta\Traits;
use Hekmatinasser\Notowo\Notowo;
use Hekmatinasser\Verta\Verta;
trait Formatting
{
/**
* Reset the format used to the default when type juggling a Verta instance to a string
*/
@@ -66,25 +63,26 @@ trait Formatting
if ($char == '\\') {
$result .= $format[++$index];
$index++;
continue;
}
$result .= $this->characterFormat($char, $timestamp, $pYear, $pMonth, $pDay, $pWeek);
$index++;
}
return $result;
}
/**
* The format of the outputted date charactre (jalali equivalent of php date() function)
*
* @param string $char
* @param integer $timestamp
* @param integer $pYear
* @param integer $pMonth
* @param integer $pDay
* @param integer $pWeek
* @param int $timestamp
* @param int $pYear
* @param int $pMonth
* @param int $pDay
* @param int $pWeek
* @return string
*/
private function characterFormat($char, $timestamp, $pYear, $pMonth, $pDay, $pWeek)
@@ -194,8 +192,8 @@ trait Formatting
* @param string $format for example 'Y-m-d H:i:s'
* @return string
*/
protected function dateWord($format){
protected function dateWord($format)
{
$timestamp = $this->getTimestamp();
list($gYear, $gMonth, $gDay, $gWeek) = explode('-', parent::format('Y-m-d-w'));
@@ -216,6 +214,7 @@ trait Formatting
if ($char == '\\') {
$result .= $format[++$index];
$index++;
continue;
}
$output = $this->characterFormat($char, $timestamp, $pYear, $pMonth, $pDay, $pWeek);
@@ -226,6 +225,7 @@ trait Formatting
$index++;
}
return $result;
}
@@ -237,11 +237,13 @@ trait Formatting
* @return type
* @since 5.0.0
*/
protected function daysYear($month, $day) {
protected function daysYear($month, $day)
{
$days = 0;
for ($i = 1; $i < $month; $i++) {
$days += static::$daysMonthJalali[$i - 1];
}
return ($days + $day);
}
@@ -257,6 +259,7 @@ trait Formatting
{
$offset = $this->clone()->startYear()->format('w');
$days = $this->format('z');
return ceil(($days + $offset) / 7);
}
@@ -268,7 +271,7 @@ trait Formatting
*/
protected function strftime($format)
{
$strftime_date = array(
$strftime_date = [
"%a" => "D",
"%A" => "l",
"%d" => "d",
@@ -310,8 +313,8 @@ trait Formatting
"%t" => "\t",
"%%" => "%",
"%Q" => "q",
"%q" => "q"
);
"%q" => "q",
];
return str_replace(array_keys($strftime_date), array_values($strftime_date), $format);
}
@@ -322,7 +325,8 @@ trait Formatting
* @param $format
* @return string
*/
public function format($format) {
public function format($format)
{
return $this->date($this->strftime($format));
}
@@ -332,8 +336,8 @@ trait Formatting
* @param $format
* @return string
*/
public function formatGregorian($format) {
public function formatGregorian($format)
{
return $this->datetime()->format($format);
}
@@ -438,6 +442,7 @@ trait Formatting
{
$en = static::getMessages('en');
$fa = static::getMessages('fa');
return str_replace(array_values($en['numbers']), array_values($fa['numbers']), $string);
}
@@ -451,6 +456,7 @@ trait Formatting
{
$fa = static::getMessages('fa');
$en = static::getMessages('en');
return str_replace(array_values($fa['numbers']), array_values($en['numbers']), $string);
}
@@ -464,7 +470,7 @@ trait Formatting
{
$ar = static::getMessages('ar');
$en = static::getMessages('en');
return str_replace(array_values($ar['numbers']), array_values($en['numbers']), $string);
}
}
+3 -9
View File
@@ -1,14 +1,9 @@
<?php
namespace Hekmatinasser\Verta\Traits;
use Hekmatinasser\Verta\Verta;
trait Modification
{
/**
* Add years to the instance. Positive $value travel forward while
* negative $value travel into the past.
@@ -73,8 +68,7 @@ trait Modification
if ($month > 12) {
$year += intval($month / 12);
$month = $month % 12;
}
elseif ($month < 1) {
} elseif ($month < 1) {
$year += intval($month / 12) - 1;
$month = 12 + ($month % 12);
}
@@ -84,12 +78,12 @@ trait Modification
}
if (($month > 6 && $month < 12 && $day == 31)) {
$day--;
}
else {
} else {
if ($month == 12 && ($day == 30 || $day == 31)) {
$day = self::isLeapYear($year) ? 30 : 29;
}
}
return self::createJalali($year, $month, $day, $hour, $minute, $second, $this->getTimeZone());
}
+12 -9
View File
@@ -1,12 +1,9 @@
<?php
namespace Hekmatinasser\Verta\Traits;
trait Transformation
{
/**
* gregorian to jalali convertion
*
@@ -15,16 +12,18 @@ trait Transformation
* @param int $g_d
* @return array
*/
public static function getJalali($g_y, $g_m, $g_d) {
public static function getJalali($g_y, $g_m, $g_d)
{
$gy = $g_y - 1600;
$gm = $g_m - 1;
$g_day_no = (365 * $gy + static::div($gy + 3, 4) - static::div($gy + 99, 100) + static::div($gy + 399, 400));
for ($i = 0; $i < $gm; ++$i) {
$g_day_no += static::$daysMonthGregorian[$i];
}
if ($gm > 1 && (($gy % 4 == 0 && $gy % 100 != 0) || ($gy % 400 == 0)))
if ($gm > 1 && (($gy % 4 == 0 && $gy % 100 != 0) || ($gy % 400 == 0))) {
# leap and after Feb
$g_day_no++;
}
$g_day_no += $g_d - 1;
$j_day_no = $g_day_no - 79;
$j_np = static::div($j_day_no, 12053); # 12053 = (365 * 33 + 32 / 4)
@@ -38,7 +37,8 @@ trait Transformation
for ($i = 0; ($i < 11 && $j_day_no >= static::$daysMonthJalali[$i]); ++$i) {
$j_day_no -= static::$daysMonthJalali[$i];
}
return array($jy, $i + 1, $j_day_no + 1);
return [$jy, $i + 1, $j_day_no + 1];
}
/**
@@ -49,7 +49,8 @@ trait Transformation
* @param int $j_d
* @return array
*/
public static function getGregorian($j_y, $j_m, $j_d) {
public static function getGregorian($j_y, $j_m, $j_d)
{
$jy = $j_y - 979;
$jm = $j_m - 1;
$j_day_no = (365 * $jy + static::div($jy, 33) * 8 + static::div($jy % 33 + 3, 4));
@@ -82,7 +83,8 @@ trait Transformation
for ($i = 0; $g_day_no >= (static::$daysMonthGregorian[$i] + ($i == 1 && $leap)); $i++) {
$g_day_no -= (static::$daysMonthGregorian[$i] + ($i == 1 && $leap));
}
return array($gy, $i + 1, $g_day_no + 1);
return [$gy, $i + 1, $g_day_no + 1];
}
/**
@@ -92,7 +94,8 @@ trait Transformation
* @param int $b
* @return int
*/
private static function div($a, $b) {
private static function div($a, $b)
{
return ~~($a / $b);
}
}
+4 -5
View File
@@ -1,9 +1,7 @@
<?php
namespace Hekmatinasser\Verta\Traits;
trait Translator
{
/**
@@ -11,7 +9,7 @@ trait Translator
*
* @var array
*/
public static $messages = array();
public static $messages = [];
/**
* List of custom localized messages.
@@ -38,6 +36,7 @@ trait Translator
if ($messages = static::loadMessages($locale)) {
static::$locale = $locale;
}
return $messages;
}
@@ -62,13 +61,13 @@ trait Translator
{
if ($messages = static::getMessages($locale)) {
static::$messages = $messages;
return true;
}
return false;
}
/**
* Set messages of a locale and take file first if present.
*
@@ -81,10 +80,10 @@ trait Translator
if (file_exists($filename = dirname(__DIR__).'/Lang/'.($locale ?: static::getLocale()).'.php')) {
return require $filename;
}
return false;
}
/**
* Set messages of a locale and take file first if present.
*
+2 -2
View File
@@ -5,7 +5,7 @@ namespace Hekmatinasser\Verta;
use DateTime;
use Hekmatinasser\Verta\Traits\Date;
class Verta extends DateTime implements VertaInterface {
class Verta extends DateTime implements VertaInterface
{
use Date;
}
+19 -21
View File
@@ -1,43 +1,41 @@
<?php
namespace Hekmatinasser\Verta;
interface VertaInterface
{
/**
* The day constants
*/
const SATURDAY = 0;
const SUNDAY = 1;
const MONDAY = 2;
const TUESDAY = 3;
const WEDNESDAY = 4;
const THURSDAY = 5;
const FRIDAY = 6;
public const SATURDAY = 0;
public const SUNDAY = 1;
public const MONDAY = 2;
public const TUESDAY = 3;
public const WEDNESDAY = 4;
public const THURSDAY = 5;
public const FRIDAY = 6;
/**
* Number unit in date
*/
const DECADE_PER_CENTURY = 10;
const YEARS_PER_DECADE = 10;
const MONTHS_PER_YEAR = 12;
const MONTHS_PER_QUARTER = 3;
const WEEKS_PER_YEAR = 52;
const WEEKS_PER_MONTH = 4.35;
const DAYS_PER_WEEK = 7;
const HOURS_PER_DAY = 24;
const MINUTES_PER_HOUR = 60;
const SECONDS_PER_MINUTE = 60;
public const DECADE_PER_CENTURY = 10;
public const YEARS_PER_DECADE = 10;
public const MONTHS_PER_YEAR = 12;
public const MONTHS_PER_QUARTER = 3;
public const WEEKS_PER_YEAR = 52;
public const WEEKS_PER_MONTH = 4.35;
public const DAYS_PER_WEEK = 7;
public const HOURS_PER_DAY = 24;
public const MINUTES_PER_HOUR = 60;
public const SECONDS_PER_MINUTE = 60;
/**
* Word use in format datetime.
*/
const DEFAULT_STRING_FORMAT = 'Y-m-d H:i:s';
public const DEFAULT_STRING_FORMAT = 'Y-m-d H:i:s';
/**
* Word use in format datetime.
*/
const DEFAULT_LOCALE = 'fa';
public const DEFAULT_LOCALE = 'fa';
}
+1
View File
@@ -1,4 +1,5 @@
<?php
if (! function_exists('verta')) {
/**
* @param null $datetime
-3
View File
@@ -85,7 +85,6 @@ class AccessTest extends TestCase
$this->assertEquals('Asia/Tehran', $datetime->timezone);
}
public function testSetDateTime()
{
$datetime = verta()->setDateTime(1397, 10, 11, 10, 20, 11);
@@ -98,7 +97,6 @@ class AccessTest extends TestCase
$this->assertEquals(11, $datetime->second);
}
public function testSetDate()
{
$datetime = verta()->setDate(1397, 10, 11);
@@ -124,6 +122,5 @@ class AccessTest extends TestCase
$this->assertEquals(10, $datetime->hour);
$this->assertEquals(20, $datetime->minute);
$this->assertEquals(11, $datetime->second);
}
}
-2
View File
@@ -267,8 +267,6 @@ class ComparisionTest extends TestCase
$this->assertFalse($datetime->isSameAs('Y-m-d'));
$this->assertFalse($datetime->isCurrentYear());
$this->assertFalse($datetime->isSameYear($target));
$this->assertFalse($datetime->isCurrentMonth());
$this->assertFalse($datetime->isSameMonth());
$this->assertFalse($datetime->isSameDay($target));
$this->assertTrue($datetime->isBirthday($target));
$this->assertFalse($datetime->isSaturday());
+1 -2
View File
@@ -23,7 +23,6 @@ class CreatorTest extends TestCase
$actual = verta()->getTimestamp();
$this->assertEquals($expected, $actual);
}
public function testYesterday()
@@ -60,7 +59,7 @@ class CreatorTest extends TestCase
$datetime = (string) Verta::instance('2019-01-01 10:20:11');
$this->assertEquals('1397-10-11 10:20:11', $datetime);
$app = ['verta' => new Verta];
$app = ['verta' => new Verta()];
VertaFacade::setFacadeApplication($app);
$datetime = (string) VertaFacade::instance('2019-01-01 10:20:11');
-3
View File
@@ -2,7 +2,6 @@
namespace Hekmatinasser\Verta\Tests;
use DateTime;
use Hekmatinasser\Verta\Verta;
use PHPUnit\Framework\TestCase;
@@ -20,7 +19,6 @@ class FormatTest extends TestCase
$datetime = (string) Verta::parse('1398-10-10 22:30:50');
$this->assertEquals('1398-10-10 22:30:50', $datetime);
}
public function testFormat()
@@ -111,7 +109,6 @@ class FormatTest extends TestCase
$difference = $target->formatDifference($datetime);
$this->assertEquals('1 سال بعد', $difference);
}
public function testFormatWord()
-2
View File
@@ -222,6 +222,4 @@ class ModificationTest extends TestCase
$this->assertEquals('1398-12-29 23:59:59', $result);
}
}