├── composer.json ├── readme.md └── src └── PersianDateTrait.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bigsinoos/j-eloquent", 3 | "description": "Convert eloquent date attributes to jalali (Persian) dates on the fly. (supports model propery access, toJson, toString and toArray).", 4 | "keywords": ["laravel", "database", "sql", "orm", "persian", "jalali", "date"], 5 | "require": { 6 | "illuminate/database": "5.1.* || 5.2.* || 5.3.* || 5.4.*", 7 | "miladr/jalali": "0.*" 8 | }, 9 | "license": "GPL", 10 | "authors": [ 11 | { 12 | "name": "Reza Shadman", 13 | "email": "pcfeeler@gmail.com" 14 | } 15 | ], 16 | "autoload": { 17 | "psr-4": { 18 | "Bigsinoos\\JEloquent\\": "src/"} 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ### Deprecation Notice 2 | For newer versions of Laravel just use the simple attribute casting functionality which is a really nicer approach. 3 | 4 | ### What is j-Eloquent? 5 | Thanks to Laravel, Django and Rails we all know that convention over configuration (CoC) makes the development more funny. So suppose that you want to convert the Gregorian date attributes of your ```Eloquent``` models to Jalali (persian) dates, in this case ```j-Eloquent``` helps you to convert them conventionally, for example when you access a property named ```$model->jalali_created_at``` on your model, the ```PersianDateTrait``` detects the convention automatically and tries to convert ```created_at``` property of your model if it is a date attribute. This is also true for ```$model->toJson();``` and ```$model->toArray();``` fields. 6 | 7 | #### Installation 8 | require following line in your composer ```require``` secion : 9 | 10 | 11 | ```javascript 12 | 13 | "require" : { 14 | // Other dependecies , 15 | "bigsinoos/j-eloquent" : "dev-master" // Laravel 5 , "1.0" for Laravel 4 16 | } 17 | 18 | ``` 19 | 20 | #### Requirement 21 | 22 | this package requires : [miladr/jalali](https://github.com/miladr/jalali) 23 | 24 | 25 | #### Features 26 | 27 | - Coneventionally converts Eloquent model date attributes to Jalali date when the original date attribute is accessed prefixed by a string like ```jalali_```. 28 | - Automatically converts date attributes to Jalali dates when model's ```toArrayl```, ```__toString();``` and ```toJson``` are called. 29 | - Custom date formats can be set for dates. 30 | 31 | 32 | #### Documentation 33 | 34 | ##### The ```PersianDateTrait``` : 35 | By using ```\Bigsinoos\JEloquent\PersianDateTrait``` trait in your models you can enable the plugin : 36 | ```php 37 | roles()->where('type', 'premium'); 50 | $userPremiumRole->create_at; // 2014/12/30 22:12:34 51 | $userPremiumRole->jalali_created_at; // 93/09/08 52 | ``` 53 | 54 | ##### Changing ```jalali_``` prefix 55 | You can change the jalali date convention prefix with overriding ```$model->setJalaliFormat($format)``` and ```$model->getJalaliFormat();``` or by overrriding ```$model->jalaliDateFormat``` property on your model class : 56 | 57 | ```php 58 | 59 | class Role extends \Eloquent { 60 | 61 | use \Bigsinoos\JEloquent\PersianDateTrait; 62 | 63 | protected $jalaliDateFormat = 'l j F Y H:i'; 64 | } 65 | 66 | # or 67 | 68 | class Role extends \Eloquent { 69 | 70 | use \Bigsinoos\JEloquent\PersianDateTrait; 71 | 72 | public function setJalaliFormat($format){ 73 | // do custom things here 74 | $this->jalaliDateFormat = $format; return $this; 75 | } 76 | 77 | protected function getJalaliFormat() 78 | { 79 | // return any format you want 80 | return 'l j F Y H:i'; 81 | } 82 | 83 | } 84 | 85 | ``` 86 | 87 | ##### Custom date attributes : 88 | You can tell Eloquent that which one of your fields are date attributes like created_at and updated_at, then Eloquent treats them like ```Carbon``` objects you define multiple date attributes like this : 89 | 90 | ```php 91 | 92 | Class Role extends \Eloquent { 93 | 94 | use \Bigsinoos\JEloquent\PersianDateTrait; 95 | 96 | /** 97 | * Add this method to customize your date attributes 98 | * 99 | * @return array 100 | */ 101 | protected function getDates() 102 | { 103 | return ['created_at', 'updated_at', 'expired_at']; 104 | } 105 | 106 | } 107 | ``` 108 | When using the above trait all of the fields that are treated like date objects by Laravel will be available for conventional converting. They will be also added to model's ```toJson()``` , ```toArray();``` and ```__toString();``` methods. 109 | 110 | ##### Converter helper method 111 | 112 | The ```$model->convertToPersian($attribute, $format);``` method allowes you to normally convert one of your fields, from Gregorian date to Jalali date : 113 | 114 | ```php 115 | 116 | $user = Auth::user(); 117 | $user->convertToPersian('created_at', 'y/m/d'); // 93/09/08 118 | 119 | ``` 120 | 121 | 122 | -------------------------------------------------------------------------------- /src/PersianDateTrait.php: -------------------------------------------------------------------------------- 1 | getJalaliFormat() : $format; 18 | 19 | if (is_null($this->$what)) { 20 | return null; 21 | } 22 | 23 | return jDate::forge($this->$what)->format($format); 24 | } 25 | 26 | /** 27 | * Add persian dates to model's toJson, toArray, __toString 28 | * 29 | * @return array 30 | */ 31 | public function toArray() 32 | { 33 | $arr = parent::toArray(); 34 | 35 | if (!$this->appendsJalaliByDefault()) { 36 | return $arr; 37 | } 38 | 39 | foreach($this->getDates() as $date){ 40 | $key = "{$this->getJalaliPrefix()}{$date}"; 41 | $arr[$key] = $this->convertToPersian($date); 42 | } 43 | return $arr; 44 | } 45 | 46 | /** 47 | * Get jalali dates based on model 48 | * 49 | * @param string $key 50 | * @return mixed 51 | */ 52 | public function __get($key) 53 | { 54 | $prefix = $this->getJalaliPrefix(); 55 | if (Str::startsWith($key, $prefix)){ 56 | $parentKey = str_replace($prefix, '', $key); 57 | $parent = $this->$parentKey; 58 | return is_null($parent) ? null : $this->convertToPersian($parentKey); 59 | } 60 | return parent::__get($key); 61 | } 62 | 63 | /** 64 | * Get jalali prefix for accessing dates 65 | * 66 | * @return string 67 | */ 68 | protected function getJalaliPrefix() 69 | { 70 | if (isset($this->jalaliPrefix)) { 71 | return $this->jalaliPrefix; 72 | } 73 | 74 | return 'jalali_'; 75 | } 76 | 77 | /** 78 | * Get jalali format 79 | * 80 | * @return string 81 | */ 82 | protected function getJalaliFormat() 83 | { 84 | if (isset($this->jalaliDateFormat)) { 85 | return $this->jalaliDateFormat; 86 | } 87 | 88 | return 'y/m/d'; 89 | } 90 | 91 | /** 92 | * Set jalali format 93 | * 94 | * @param $format 95 | * @return $this 96 | */ 97 | public function setJalaliFormat($format) 98 | { 99 | $this->jalaliDateFormat = $format; return $this; 100 | } 101 | 102 | /** 103 | * Appends dates by default 104 | * 105 | * @return bool 106 | */ 107 | protected function appendsJalaliByDefault() 108 | { 109 | if (isset($this->appendsJalaliByDefault)) { 110 | return (bool)$this->appendsJalaliByDefault; 111 | } 112 | 113 | return true; 114 | } 115 | 116 | /** 117 | * Don't append jalali by default 118 | * 119 | * @return $this 120 | */ 121 | public function doNotAppendJalaliByDefault() 122 | { 123 | $this->appendsJalaliByDefault = false; 124 | return $this; 125 | } 126 | } 127 | --------------------------------------------------------------------------------