peter279k / carbon-extended

The extended format for Carbon class instance
4 stars 1 forks source link

Provide carbon-extended as trait/mixin #4

Open kylekatarnls opened 4 years ago

kylekatarnls commented 4 years ago

Hello,

Classes that extend Carbon like jensseggers/date or this one have one major inconvenient. You have to choose one or the other. You can't benefit of both methods of 2 or more classes that extend a first one (Carbon in this case).

A more extensible approach is to use traits and Carbon provides out of the box the mixin() method which can take as much traits as you want as long as they don't use the same methods names:

trait BeerDayCarbonTrait
{
    public function nextBeerDay()
    {
        return $this->modify('next Wednesday');
    }

    public function previousBeerDay()
    {
        return $this->modify('previous Wednesday');
    }
}

Carbon::mixin(BeerDayCarbonTrait::class);

$date = Carbon::parse('First saturday of December 2018');

echo $date->previousBeerDay();                                                 // 2018-11-28 00:00:00
echo "\n";
echo $date->nextBeerDay();                                                     // 2018-12-05 00:00:00

[Try on try-carbon.herokuapp.com](https://try-carbon.herokuapp.com/?hide-output-gutter&output-left-padding=10&theme=tomorrow_night&border=none&radius=4&v-padding=15&input=trait%20BeerDayCarbonTrait%0A%7B%0A%20%20%20%20public%20function%20nextBeerDay()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20%24this-%3Emodify(%27next%20wednesday%27)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20function%20previousBeerDay()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20%24this-%3Emodify(%27previous%20wednesday%27)%3B%0A%20%20%20%20%7D%0A%7D%0A%0ACarbon%3A%3Amixin(BeerDayCarbonTrait%3A%3Aclass)%3B%0A%0A%24date%20%3D%20Carbon%3A%3Aparse(%27First%20saturday%20of%20December%202018%27)%3B%0A%0Aecho%20%24date-%3EpreviousBeerDay()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%202018-11-28%2000%3A00%3A00%0Aecho%20%22%5Cn%22%3B%0Aecho%20%24date-%3EnextBeerDay()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%202018-12-05%2000%3A00%3A00%0A&token=live-editor-158)

And this is a backward-compatible change as you can do:

<?php

namespace Lee;

use Carbon\Carbon;

// Optionally you can deprecate it for next version
@trigger_error('CarbonExtended class is deprecated and will be removed in carbon-extended 2, use Carbon::mixin(CarbonExtendedTrait::class) instead.', E_USER_DEPRECATED);

class CarbonExtended extends Carbon
{
  use CarbonExtendedTrait;
}

And Carbon documentation has a macro section where such mixins/macros can be promoted if you want by submitting a pull-request on gh-pages branch on https://github.com/briannesbitt/Carbon

peter279k commented 4 years ago

Hi @kylekatarnls, thanks for your reply.

Thanks for your sharing the Carbon::mixin method :) and it will be useful for someone use many other Carbon extended packages conveniently!

I will do this on my next release 2.x version :).