einenlum / iron
Requires
- nesbot/carbon: ^2.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14@dev
- phpspec/phpspec: ^5.1@dev
- phpstan/phpstan: ^0.11.0@dev
- phpunit/phpunit: ^7.5@dev
This package is auto-updated.
Last update: 2024-10-29 05:22:47 UTC
README
A Carbon-like tool done the right way.
Install
composer require einenlum/iron:dev-master
Usage
<?php use Iron\Iron; // Can be constructed with a \DateTime or \DateTimeImmutable object $iron = Iron::from(new \DateTimeImmutable()); // A \DateTimeImmutable object, two days later $later = $iron->addDays(2)->toDateTimeImmutable();
Available methods
And every method (and getter) available in Carbon on the CarbonImmutable
objects.
Why?
Carbon is a really useful tool. I'm just not really happy with the implementation they chose. Extending the native \DateTime
class seems dangerous (here is some documentation about this problem in JS, but it works for PHP too). We should not extend the behavior of code we don't own. Also, being able to pass a Carbon
object to a method waiting for a native \DateTime
object, seems wrong to me.
Here we use Iron
to manipulate data in a fluent API using an \ImmutableDateTime
object under the hood and we finally return a native PHP \DateTimeImmutable
(or \DateTime
) object.
For now it just wraps carbon calls and getters. Next step will be for sure to implement here all the methods we need to get rid of Carbon in the end.
Tests
This project is tested thanks to PHPSpec and PHPUnit. To run the tests:
composer test
CS Fixing
Coding Standards are checked and fixed thanks to PHP-CS-Fixer. To fix your code, just run composer cs-fix
.
Contributing
Please, feel free to contribute and improve this project.
License
MIT.