I have several libraries defined for my Laravel application that expose constants.
For example, I have a class for calculating holidays and working days, used to calculate the number of working days for some reports.
The definition of my class is as follows:
<?php namespace MyApp\Libraries; class Holidays { const NEW_YEARS_DAY = "new years day"; const INDEPENDENCE_DAY = "independence day"; const CHRISTMAS_DAY = "christmas day"; ...
They are used (for example) by the date method, which takes the value of the holiday constant and year and returns the date of this holiday this year. I have a facade and a service provider, so this library can be used in Laravel Way & trade; . Everything works fine, I have unit tests for everything, and I'm happy with the code.
I have a question on how to access these constants. If I use the facade and calling the library from other parts of my code, it looks like this:
$xmas = \Holidays::date(\MyApp\Libraries\Holidays::CHRISTMAS_DAY, "2014");
This works, but I would prefer to use the facade to do this, for example:
$xmas = \Holidays::date(\Holidays::CHRISTMAS_DAY, "2014");
One solution that I was thinking about is to define constants in the facade. This works, but then I separate the constant values ββfrom the library - for obvious reasons, I would prefer to store the values ββwith the code with which they are associated.
Then I am in a different solution: define the constants as described above, then refer to them on the facade as follows:
<?php namespace MyApp\Facades; use Illuminate\Support\Facades\Facade; class Holidays extends Facade { const NEW_YEARS_DAY = \MyApp\Libraries\Holidays::NEW_YEARS_DAY; const INDEPENDENCE_DAY = \MyApp\Libraries\Holidays::INDEPENDENCE_DAY; const CHRISTMAS_DAY = \MyApp\Libraries\Holidays::CHRISTMAS_DAY; ...
Now I can refer to constants through the facade, and not to a fully functional library class, and I need to determine the value for the constant once (although I need to add any new constants to the library and facade). This works, and I get what I want, but it's a bit like violating the DRY (Do not Repeat Yourself) principle.
So here is the question. Is this the best way to do this?