LICENSE000064400000002047147134003370005556 0ustar00MIT License Copyright (c) 2023 Carbon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. README.md000064400000000776147134003370006037 0ustar00# carbon-doctrine-types Types to use Carbon in Doctrine ## Documentation [Check how to use in the official Carbon documentation](https://carbon.nesbot.com/symfony/) This package is an externalization of [src/Carbon/Doctrine](https://github.com/briannesbitt/Carbon/tree/2.71.0/src/Carbon/Doctrine) from `nestbot/carbon` package. Externalization allows to better deal with different versions of dbal. With version 4.0 of dbal, it no longer sustainable to be compatible with all version using a single code. composer.json000064400000001427147134003370007274 0ustar00{ "name": "carbonphp/carbon-doctrine-types", "description": "Types to use Carbon in Doctrine", "type": "library", "keywords": [ "date", "time", "DateTime", "Carbon", "Doctrine" ], "require": { "php": "^7.1.8 || ^8.0" }, "require-dev": { "doctrine/dbal": ">=2.0.0", "nesbot/carbon": "^2.71.0 || ^3.0.0", "phpunit/phpunit": "^10.3" }, "conflict": { "doctrine/dbal": ">=3.7.0" }, "license": "MIT", "autoload": { "psr-4": { "Carbon\\Doctrine\\": "src/Carbon/Doctrine/" } }, "authors": [ { "name": "KyleKatarn", "email": "kylekatarnls@gmail.com" } ], "minimum-stability": "dev" } src/Carbon/Doctrine/CarbonDoctrineType.php000064400000000626147134003370014563 0ustar00 */ protected function getCarbonClassName(): string { return Carbon::class; } /** * @return string */ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { $maximum = CarbonDoctrineType::MAXIMUM_PRECISION; $precision = ($fieldDeclaration['precision'] ?? null) ?: $maximum; if ($fieldDeclaration['secondPrecision'] ?? false) { $precision = 0; } if ($precision === $maximum) { $precision = DateTimeDefaultPrecision::get(); } $type = parent::getSQLDeclaration($fieldDeclaration, $platform); if (!$precision) { return $type; } if (str_contains($type, '(')) { return preg_replace('/\(\d+\)/', "($precision)", $type); } [$before, $after] = explode(' ', "$type "); return trim("$before($precision) $after"); } /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) * * @return T|null */ public function convertToPHPValue($value, AbstractPlatform $platform) { $class = $this->getCarbonClassName(); if ($value === null || is_a($value, $class)) { return $value; } if ($value instanceof DateTimeInterface) { return $class::instance($value); } $date = null; $error = null; try { $date = $class::parse($value); } catch (Exception $exception) { $error = $exception; } if (!$date) { throw ConversionException::conversionFailedFormat( $value, $this->getName(), 'Y-m-d H:i:s.u or any format supported by '.$class.'::parse()', $error ); } return $date; } /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) * * @return string|null */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { if ($value === null) { return $value; } if ($value instanceof DateTimeInterface) { return $value->format('Y-m-d H:i:s.u'); } $method = method_exists(ConversionException::class, 'conversionFailedInvalidType') ? 'conversionFailedInvalidType' : 'conversionFailed'; // @codeCoverageIgnore throw ConversionException::$method( $value, $this->getName(), ['null', 'DateTime', 'Carbon'] ); } } src/Carbon/Doctrine/DateTimeDefaultPrecision.php000064400000001035147134003370015675 0ustar00 */ use CarbonTypeConverter; /** * @return class-string */ protected function getCarbonClassName(): string { return CarbonImmutable::class; } } src/Carbon/Doctrine/DateTimeType.php000064400000000366147134003370013364 0ustar00 */ use CarbonTypeConverter; }