├── .gitignore ├── .php_cs ├── .travis.yml ├── LICENSE ├── README.md ├── bin └── .placeholder ├── composer.json ├── phpunit.xml.dist ├── src ├── DateTime │ ├── AgeRange.php │ ├── Clock.php │ ├── Date.php │ ├── DateTime.php │ ├── Duration.php │ ├── Exception │ │ └── UnsupportedCalculation.php │ ├── HourMin.php │ ├── MonthDay.php │ ├── Period.php │ ├── Period │ │ ├── DailyIteratableInterface.php │ │ ├── DailyTrait.php │ │ ├── MonthlyIteratableInterface.php │ │ ├── MonthlyTrait.php │ │ └── ThreeMonthlyTrait.php │ ├── Term.php │ ├── Year.php │ └── YearMonth.php ├── Matrix │ ├── AddressedMatrix.php │ ├── Operation │ │ └── ZeroableInterface.php │ └── TypedMatrix.php └── String │ └── UniqueName.php └── tests ├── DateTime ├── AgeRangeTest.php ├── DateTest.php ├── DateTimeTest.php ├── HourMinTest.php ├── MonthDayTest.php ├── PeriodTraitTest.php ├── ThreeMonthlyTraitTest.php ├── YearMonthTest.php └── YearTest.php ├── String └── UniqueNameTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | ._* 3 | .~lock.* 4 | /.buildpath 5 | /.composer/ 6 | /.idea/ 7 | /.project 8 | /.settings/ 9 | /build/ 10 | /composer.lock 11 | /composer.phar 12 | /nbproject/ 13 | /php-cs-fixer.phar 14 | /vendor/ 15 | cache.properties 16 | -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | in(__DIR__.'/src') 4 | ->in(__DIR__.'/tests') 5 | ; 6 | 7 | return Symfony\CS\Config\Config::create() 8 | ->fixers(array('-empty_return', '-blankline_after_open_tag', 'ordered_use', '-phpdoc_no_empty_return')) 9 | ->finder($finder) 10 | ; 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | sudo: false 4 | 5 | cache: 6 | directories: 7 | - $HOME/.composer/cache 8 | 9 | php: 10 | - 5.5 11 | - 5.6 12 | - 7 13 | - 7.1 14 | 15 | matrix: 16 | allow_failures: 17 | - php: hhvm 18 | fast_finish: true 19 | 20 | before_script: 21 | - rm -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini 22 | - composer self-update 23 | - composer install 24 | 25 | script: 26 | - ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 27 | 28 | after_script: 29 | - wget https://scrutinizer-ci.com/ocular.phar 30 | - php ocular.phar code-coverage:upload --format=php-clover coverage.clover 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 GOTO Hidenori , 2 | 2015 KUBO Atsuhiro , 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Domain Commons 2 | 3 | Commons for domain models 4 | 5 | [![Build Status](https://travis-ci.org/phpmentors-jp/domain-commons.svg?branch=master)](https://travis-ci.org/phpmentors-jp/domain-commons) 6 | [![SensioLabsInsight](https://insight.sensiolabs.com/projects/0e1c452e-3cd6-4e17-a6c2-77ae78a9a878/mini.png)](https://insight.sensiolabs.com/projects/0e1c452e-3cd6-4e17-a6c2-77ae78a9a878) 7 | [![Total Downloads](https://poser.pugx.org/phpmentors/domain-commons/downloads.png)](https://packagist.org/packages/phpmentors/domain-commons) 8 | [![Latest Stable Version](https://poser.pugx.org/phpmentors/domain-commons/v/stable.png)](https://packagist.org/packages/phpmentors/domain-commons) 9 | [![Latest Unstable Version](https://poser.pugx.org/phpmentors/domain-commons/v/unstable.png)](https://packagist.org/packages/phpmentors/domain-commons) 10 | 11 | # Installation 12 | 13 | `Domain Commons` can be installed using [Composer](http://getcomposer.org/). 14 | 15 | ``` 16 | $ composer require phpmentors/domain-commons 17 | ``` 18 | 19 | # Features 20 | 21 | ## DateTime basics 22 | 23 | ### Date and Time 24 | 25 | - Date 26 | - DateTime 27 | - MonthDay 28 | - Year 29 | - YearMonth 30 | - HourMin 31 | - AgeRange 32 | 33 | ### Period 34 | 35 | - Duration 36 | - Period 37 | - Term 38 | 39 | #### Traversable 40 | 41 | - DailyTrait / DailyIteratableInterface 42 | - MonthlyTrait / MonthlyIteratableInterface 43 | 44 | You can define a domain specific period as follows: 45 | 46 | ```php 47 | namespace MyDomain; 48 | 49 | use PHPMentors\DomainCommons\DateTime\Date; 50 | use PHPMentors\DomainCommons\DateTime\Period\DailyIteratableInterface; 51 | use PHPMentors\DomainCommons\DateTime\Period\DailyTrait; 52 | 53 | class DailyPeriod extends Period implements DailyIteratableInterface 54 | { 55 | use DailyTrait; 56 | 57 | public function __construct(Date $start, Date $end) 58 | { 59 | parent::__construct($start, $end); 60 | $this->it = $this->iterate(); // this line enables iterator 61 | } 62 | } 63 | ``` 64 | 65 | You can iterate this period by date using standard `foreach` statement as follows: 66 | 67 | ``` 68 | use PHPMentors\DomainCommons\DateTime\Date; 69 | use MyDomain\DailyPeriod; 70 | 71 | $period = new DailyPeriod(new Date('2015-04-12'), new Date('2015-06-30')); 72 | 73 | $count = 0; 74 | foreach ($period as $one) { 75 | echo $one->format('m/d') . PHP_EOL; 76 | } 77 | ``` 78 | 79 | ### Utility 80 | 81 | - Clock 82 | 83 | ## Matrix (Typed and Addressed) 84 | 85 | - TypedMatrix 86 | - AddressedMatrix 87 | 88 | ### Operation 89 | 90 | - ZeroableInterface 91 | 92 | ## String Utility 93 | 94 | - UniqueName 95 | 96 | # Support 97 | 98 | If you find a bug or have a question, or want to request a feature, create an issue or pull request for it on [Issues](https://github.com/phpmentors-jp/domain-commons/issues). 99 | 100 | # Copyright 101 | 102 | Copyright (c) 2015 GOTO Hidenori, 2015 KUBO Atsuhiro, All rights reserved. 103 | 104 | # License 105 | 106 | [The BSD 2-Clause License](http://opensource.org/licenses/BSD-2-Clause) 107 | -------------------------------------------------------------------------------- /bin/.placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phpmentors-jp/domain-commons/0e3b4057bf0b949a2fc5d36e1bc4195d7fa44dc2/bin/.placeholder -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phpmentors/domain-commons", 3 | "type": "library", 4 | "description": "Commons for domain models", 5 | "keywords": ["modeling", "design", "domain driven design", "ddd", "datetime"], 6 | "license": "BSD-2-Clause", 7 | "authors": [ 8 | { 9 | "name": "GOTO Hidenori", 10 | "email": "hidenorigoto@gmail.com" 11 | }, 12 | { 13 | "name": "KUBO Atsuhiro", 14 | "email": "kubo@iteman.jp" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.5", 19 | "phpmentors/domain-kata": "~1.3" 20 | }, 21 | "require-dev": { 22 | "phpunit/phpunit": "~4.6" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "PHPMentors\\DomainCommons\\": "src/" 27 | } 28 | }, 29 | "extra": { 30 | "branch-alias": { 31 | "dev-master": "1.1.x-dev" 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | tests 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | src 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/DateTime/AgeRange.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | use PHPMentors\DomainKata\Entity\EntityInterface; 16 | 17 | /** 18 | * @since Class available since Release 1.1.0 19 | */ 20 | class AgeRange implements EntityInterface 21 | { 22 | /** 23 | * @var Clock 24 | */ 25 | private $clock; 26 | 27 | /** 28 | * @var int 29 | */ 30 | private $max; 31 | 32 | /** 33 | * @var int 34 | */ 35 | private $min; 36 | 37 | public function __construct() 38 | { 39 | $this->setClock(new Clock()); 40 | } 41 | 42 | /** 43 | * @param Clock $clock 44 | */ 45 | public function setClock(Clock $clock) 46 | { 47 | $this->clock = $clock; 48 | } 49 | 50 | /** 51 | * @param int $max 52 | */ 53 | public function setMax($max) 54 | { 55 | $this->max = $max; 56 | } 57 | 58 | /** 59 | * @param int $min 60 | */ 61 | public function setMin($min) 62 | { 63 | $this->min = $min; 64 | } 65 | 66 | /** 67 | * @return int 68 | */ 69 | public function getMax() 70 | { 71 | return $this->max; 72 | } 73 | 74 | /** 75 | * @return \DateTime 76 | */ 77 | public function getMaxAsDate() 78 | { 79 | return $this->convertAgeToDate($this->max + 1, false); 80 | } 81 | 82 | /** 83 | * @return int 84 | */ 85 | public function getMin() 86 | { 87 | return $this->min; 88 | } 89 | 90 | /** 91 | * @return \DateTime 92 | */ 93 | public function getMinAsDate() 94 | { 95 | return $this->convertAgeToDate($this->min, true); 96 | } 97 | 98 | /** 99 | * @param int $numberOfYears 100 | * @param bool $correctionMatcher 101 | * 102 | * @return \DateTime 103 | */ 104 | private function convertAgeToDate($numberOfYears, $correctionMatcher) 105 | { 106 | $currentDate = $this->clock->now(); 107 | $baseDate = new \DateTime($currentDate->format('Y-m-d')); 108 | $baseDate->modify(sprintf('-%d years', $numberOfYears)); 109 | 110 | if (($currentDate->format('L') == 1 && $currentDate->format('m-d') === '02-29' && $baseDate->format('L') == 0) === $correctionMatcher) { 111 | $baseDate->modify(sprintf('%s1 days', $correctionMatcher ? '-' : '+')); 112 | } 113 | 114 | return $baseDate; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/DateTime/Clock.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class Clock 16 | { 17 | public function now() 18 | { 19 | return new DateTime(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/DateTime/Date.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class Date extends DateTime 16 | { 17 | public function __construct($time = 'now', $timezone = null) 18 | { 19 | $date = new \DateTime($time, $timezone); 20 | $dateStr = $date->format('Y-m-d 00:00:00'); 21 | 22 | parent::__construct($dateStr, $timezone); 23 | } 24 | 25 | public function __toString() 26 | { 27 | return $this->format('Y-m-d'); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/DateTime/DateTime.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | use DateTimeImmutable; 16 | use PHPMentors\DomainCommons\DateTime\Exception\UnsupportedCalculation; 17 | 18 | class DateTime extends DateTimeImmutable 19 | { 20 | public function addDays($days) 21 | { 22 | $class = static::class; 23 | $daysStr = (($days < 0) ? '-' : '+').abs($days).' days'; 24 | 25 | return new $class(date('Y-m-d H:i:s', strtotime($daysStr, $this->getTimestamp()))); 26 | } 27 | 28 | /** 29 | * @param int $months 30 | * @return DateTime 31 | * @throws UnsupportedCalculation 32 | * 33 | * NOTE: This method throws UnsupportedCalculation exception when 34 | * the day of the result is not equal to the original date. 35 | * Don't forget to handle this situation before calling this method. 36 | */ 37 | public function addMonths($months) 38 | { 39 | $class = static::class; 40 | $monthsStr = (($months < 0) ? '-' : '+').abs($months).' month'; 41 | 42 | $newInstance = new $class(date('Y-m-d H:i:s', strtotime($monthsStr, $this->getTimestamp()))); 43 | 44 | if ($newInstance->format('d') != $this->format('d')) { 45 | throw new UnsupportedCalculation(); 46 | } 47 | 48 | return $newInstance; 49 | } 50 | 51 | public function __toString() 52 | { 53 | return $this->format('Y-m-d H:i:s'); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/DateTime/Duration.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class Duration extends Period 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/DateTime/Exception/UnsupportedCalculation.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime\Exception; 14 | 15 | class UnsupportedCalculation extends \RuntimeException 16 | { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/DateTime/HourMin.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class HourMin extends DateTime 16 | { 17 | public function __construct($time = 'now', $timezone = null) 18 | { 19 | $date = new \DateTime($time, $timezone); 20 | $dateStr = $date->format('2012-1-1 H:i:00'); 21 | 22 | parent::__construct($dateStr, $timezone); 23 | } 24 | 25 | public function addDays($days) 26 | { 27 | throw new \RuntimeException('not supported'); 28 | } 29 | 30 | public function addMonths($months) 31 | { 32 | throw new \RuntimeException('not supported'); 33 | } 34 | 35 | public function __toString() 36 | { 37 | return $this->format('H:i'); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/DateTime/MonthDay.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class MonthDay extends DateTime 16 | { 17 | public function __construct($time = 'now', $timezone = null) 18 | { 19 | $date = new \DateTime($time, $timezone); 20 | $dateStr = $date->format('2012-m-d 00:00:00'); 21 | 22 | parent::__construct($dateStr, $timezone); 23 | } 24 | 25 | public function __toString() 26 | { 27 | return $this->format('m-d'); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/DateTime/Period.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class Period 16 | { 17 | /** 18 | * @var DateTime 19 | */ 20 | protected $start; 21 | 22 | /** 23 | * @var DateTime 24 | */ 25 | protected $end; 26 | 27 | public function __construct($start, $end) 28 | { 29 | $this->start = $start; 30 | $this->end = $end; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/DateTime/Period/DailyIteratableInterface.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime\Period; 14 | 15 | interface DailyIteratableInterface extends \IteratorAggregate 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/DateTime/Period/DailyTrait.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime\Period; 14 | 15 | trait DailyTrait 16 | { 17 | protected $it; 18 | 19 | public function getIterator() 20 | { 21 | return $this->it; 22 | } 23 | 24 | /** 25 | * @return \Generator 26 | * @requireProperty DateTime $start 27 | * @requireProperty DateTime $end 28 | */ 29 | public function iterate() 30 | { 31 | $current = clone $this->start; 32 | while (true) { 33 | yield $current; 34 | $current = $current->addDays(1); 35 | if ($current > $this->end) { 36 | break; 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/DateTime/Period/MonthlyIteratableInterface.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime\Period; 14 | 15 | interface MonthlyIteratableInterface extends \IteratorAggregate 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/DateTime/Period/MonthlyTrait.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime\Period; 14 | 15 | trait MonthlyTrait 16 | { 17 | /** 18 | * @return \Generator 19 | * @requireProperty DateTime $start 20 | * @requireProperty DateTime $end 21 | */ 22 | public function iterate() 23 | { 24 | $current = clone $this->start; 25 | while (true) { 26 | yield $current; 27 | $current = $current->addMonths(1); 28 | if ($current > $this->end) { 29 | break; 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/DateTime/Period/ThreeMonthlyTrait.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime\Period; 14 | 15 | use PHPMentors\DomainCommons\DateTime\Date; 16 | use PHPMentors\DomainCommons\DateTime\Term; 17 | 18 | trait ThreeMonthlyTrait 19 | { 20 | protected $it; 21 | protected $_termFactory = null; 22 | 23 | public function getIterator() 24 | { 25 | return $this->it; 26 | } 27 | 28 | public function setTermFactory(\Closure $f) 29 | { 30 | $this->_termFactory = $f; 31 | } 32 | 33 | /** 34 | * @return \Generator 35 | * @requireProperty DateTime $start 36 | * @requireProperty DateTime $end 37 | */ 38 | public function iterate() 39 | { 40 | $start = clone $this->start; 41 | while (true) { 42 | $end = clone $start->addMonths(2); 43 | $end = new Date($end->format('Y-m-t')); 44 | if ($end > $this->end) { 45 | $end = $this->end; 46 | } 47 | 48 | if ($this->_termFactory) { 49 | yield call_user_func($this->_termFactory, $start, $end); 50 | } else { 51 | yield new Term($start, $end); 52 | } 53 | 54 | $start = $end->addDays(1); 55 | if ($start > $this->end) { 56 | break; 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/DateTime/Term.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class Term extends Period 16 | { 17 | } 18 | -------------------------------------------------------------------------------- /src/DateTime/Year.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class Year extends DateTime 16 | { 17 | public function __construct($time = 'now', $timezone = null) 18 | { 19 | $date = new \DateTime($time, $timezone); 20 | $dateStr = $date->format('Y-1-1 00:00:00'); 21 | 22 | parent::__construct($dateStr, $timezone); 23 | } 24 | 25 | public function addDays($days) 26 | { 27 | throw new \RuntimeException('not supported'); 28 | } 29 | 30 | public function addMonths($months) 31 | { 32 | throw new \RuntimeException('not supported'); 33 | } 34 | 35 | public function __toString() 36 | { 37 | return $this->format('Y'); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/DateTime/YearMonth.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class YearMonth extends DateTime 16 | { 17 | public function __construct($time = 'now', $timezone = null) 18 | { 19 | $date = new \DateTime($time, $timezone); 20 | $dateStr = $date->format('Y-m-1 00:00:00'); 21 | 22 | parent::__construct($dateStr, $timezone); 23 | } 24 | 25 | public static function fromLong($ym) 26 | { 27 | $month = $ym % 100; 28 | $year = (int) floor($ym / 100); 29 | 30 | $class = static::class; 31 | 32 | return new $class("$year-$month-1 00:00:00"); 33 | } 34 | 35 | public function toLong() 36 | { 37 | return (int) $this->format('Ym'); 38 | } 39 | 40 | public function addDays($days) 41 | { 42 | throw new \RuntimeException('not supported'); 43 | } 44 | 45 | public function __toString() 46 | { 47 | return $this->format('Y-m'); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Matrix/AddressedMatrix.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\Matrix; 14 | 15 | use PHPMentors\DomainKata\Entity\EntityInterface; 16 | 17 | class AddressedMatrix extends TypedMatrix 18 | { 19 | /** 20 | * @var string[] 21 | */ 22 | protected $_columnNames; 23 | 24 | public function __construct($columnNames, \Closure $elementFactory) 25 | { 26 | $this->_columnNames = $columnNames; 27 | 28 | parent::__construct(count($this->_columnNames), count($this->_columnNames), $elementFactory); 29 | } 30 | 31 | /** 32 | * Get element using Column name addressing. 33 | * 34 | * @api 35 | * 36 | * @param $a 37 | * @param $b 38 | * 39 | * @return EntityInterface 40 | * 41 | * @throws \RuntimeException 42 | */ 43 | public function addressGet($a, $b) 44 | { 45 | $aIndex = $this->getColumnIndex($a); 46 | $bIndex = $this->getColumnIndex($b); 47 | 48 | if ($aIndex === false || $bIndex === false) { 49 | throw new \RuntimeException(); 50 | } 51 | 52 | return $this->get($bIndex, $aIndex); 53 | } 54 | 55 | /** 56 | * Get an elements array of specified row. 57 | * 58 | * @param $a 59 | * 60 | * @return mixed 61 | * 62 | * @throws \RuntimeException 63 | */ 64 | public function addressGetRow($a) 65 | { 66 | $aIndex = $this->getColumnIndex($a); 67 | 68 | if ($aIndex === false) { 69 | throw new \RuntimeException(); 70 | } 71 | 72 | return $this->getCol($aIndex); 73 | } 74 | 75 | /** 76 | * Get an elements array of specified column. 77 | * 78 | * @param $a 79 | * 80 | * @return mixed 81 | * 82 | * @throws \RuntimeException 83 | */ 84 | public function addressGetCol($a) 85 | { 86 | $aIndex = $this->getColumnIndex($a); 87 | 88 | if ($aIndex === false) { 89 | throw new \RuntimeException(); 90 | } 91 | 92 | return $this->getRow($aIndex); 93 | } 94 | 95 | /** 96 | * Set element using Column name addressing. 97 | * 98 | * @param $a 99 | * @param $b 100 | * @param EntityInterface $newValue 101 | * 102 | * @throws \RuntimeException 103 | */ 104 | public function addressSet($a, $b, $newValue) 105 | { 106 | $aIndex = $this->getColumnIndex($a); 107 | $bIndex = $this->getColumnIndex($b); 108 | 109 | if ($aIndex === false || $bIndex === false) { 110 | throw new \RuntimeException(); 111 | } 112 | 113 | $this->set($bIndex, $aIndex, $newValue); 114 | } 115 | 116 | /** 117 | * @api 118 | * 119 | * @return \string[] 120 | */ 121 | public function columnNames() 122 | { 123 | return $this->_columnNames; 124 | } 125 | 126 | /** 127 | * @param $columnName 128 | * 129 | * @return mixed 130 | */ 131 | private function getColumnIndex($columnName) 132 | { 133 | return array_search($columnName, $this->_columnNames); 134 | } 135 | 136 | /** 137 | * @api 138 | * 139 | * @param $target 140 | */ 141 | public function copyFrom(EntityInterface $target) 142 | { 143 | /* @var $target AddressedMatrix */ 144 | assert($target instanceof self); 145 | $this->_columnNames = $target->_columnNames; 146 | $this->copyFromParent($target); 147 | } 148 | 149 | /** 150 | * @param $target 151 | */ 152 | private function copyFromParent(TypedMatrix $target) 153 | { 154 | /* @var $target TypedMatrix */ 155 | parent::copyFrom($target); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /src/Matrix/Operation/ZeroableInterface.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\Matrix\Operation; 14 | 15 | interface ZeroableInterface 16 | { 17 | /** 18 | * @return bool 19 | */ 20 | public function isZero(); 21 | } 22 | -------------------------------------------------------------------------------- /src/Matrix/TypedMatrix.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\Matrix; 14 | 15 | use PHPMentors\DomainCommons\Matrix\Operation\ZeroableInterface; 16 | use PHPMentors\DomainKata\Entity\EntityInterface; 17 | use PHPMentors\DomainKata\Entity\Operation\CopyableInterface; 18 | use PHPMentors\DomainKata\Entity\Operation\EquatableInterface; 19 | 20 | class TypedMatrix implements EntityInterface, EquatableInterface, CopyableInterface, ZeroableInterface 21 | { 22 | /** 23 | * @var int 24 | */ 25 | protected $_rows; 26 | /** 27 | * @var int 28 | */ 29 | protected $_cols; 30 | 31 | /** 32 | * 2 dimension array data (= matrix elements). 33 | * 34 | * @var array 35 | */ 36 | protected $_data; 37 | 38 | /** 39 | * @var \Closure 40 | */ 41 | protected $factory; 42 | 43 | /** 44 | * @param $rows 45 | * @param $cols 46 | * @param \Closure $elementFactory 47 | */ 48 | public function __construct($rows, $cols, $elementFactory = null) 49 | { 50 | $this->_rows = $rows; 51 | $this->_cols = $cols; 52 | $this->factory = $elementFactory; 53 | 54 | if (!$this->factory) { 55 | $this->factory = function () { 56 | return 0; 57 | }; 58 | } 59 | $this->initializeElements(); 60 | } 61 | 62 | /** 63 | * Return row count. 64 | * 65 | * @api 66 | * 67 | * @return int 68 | */ 69 | public function rows() 70 | { 71 | return $this->_rows; 72 | } 73 | 74 | /** 75 | * Return column number. 76 | * 77 | * @api 78 | * 79 | * @return int 80 | */ 81 | public function cols() 82 | { 83 | return $this->_cols; 84 | } 85 | 86 | /** 87 | * Return an element located with row/col. 88 | * 89 | * @api 90 | * 91 | * @param $row 92 | * @param $col 93 | * 94 | * @return mixed 95 | * 96 | * @throws \OutOfRangeException 97 | */ 98 | public function get($row, $col) 99 | { 100 | if (!$this->checkRange($row, $col)) { 101 | throw new \OutOfRangeException(); 102 | } 103 | 104 | return $this->_data[$row][$col]; 105 | } 106 | 107 | /** 108 | * Set an element located with row/col. 109 | * 110 | * @api 111 | * 112 | * @param $row 113 | * @param $col 114 | * @param $value 115 | * 116 | * @return $this 117 | * 118 | * @throws \OutOfRangeException 119 | */ 120 | public function set($row, $col, $value) 121 | { 122 | if (!$this->checkRange($row, $col)) { 123 | throw new \OutOfRangeException(); 124 | } 125 | 126 | $this->_data[$row][$col] = $value; 127 | 128 | return $this; 129 | } 130 | 131 | /** 132 | * Return an array of elements in a specified row. 133 | * 134 | * @param $row 135 | * 136 | * @return mixed 137 | * 138 | * @throws \OutOfRangeException 139 | */ 140 | public function getRow($row) 141 | { 142 | if (!$this->checkRange($row, 0)) { 143 | throw new \OutOfRangeException(); 144 | } 145 | 146 | return $this->_data[$row]; 147 | } 148 | 149 | /** 150 | * Return an array of elements in a specified column. 151 | * 152 | * @param $col 153 | * 154 | * @return array 155 | * 156 | * @throws \OutOfRangeException 157 | */ 158 | public function getCol($col) 159 | { 160 | if (!$this->checkRange(0, $col)) { 161 | throw new \OutOfRangeException(); 162 | } 163 | 164 | return array_column($this->_data, $col); 165 | } 166 | 167 | /** 168 | * returns whether this matrix is `zero matrix`. 169 | * 170 | * @api 171 | * 172 | * @return bool 173 | */ 174 | public function isZero() 175 | { 176 | return $this->reduce(function ($current, $element) { 177 | if ($element instanceof ZeroableInterface) { 178 | return $current & ($element->isZero()); 179 | } elseif (is_numeric($element)) { 180 | return $current & ($element == 0); 181 | } else { 182 | throw new \RuntimeException('Cannot evaluate whether element is zero or not.'); 183 | } 184 | }, true); 185 | } 186 | 187 | /** 188 | * apply closure to each element. 189 | * 190 | * @api 191 | * 192 | * @param callable $f 193 | * 194 | * @return TypedMatrix 195 | */ 196 | public function map(\Closure $f) 197 | { 198 | array_walk_recursive($this->_data, $f); 199 | 200 | return $this; 201 | } 202 | 203 | /** 204 | * @api 205 | * 206 | * @param callable $f arguments $current, $element, $rowIndex, $colIndex 207 | * @param $initial 208 | * 209 | * @return mixed 210 | */ 211 | public function reduce(\Closure $f, $initial) 212 | { 213 | $current = $initial; 214 | for ($rowIndex = 0; $rowIndex < $this->_rows; ++$rowIndex) { 215 | for ($colIndex = 0; $colIndex < $this->_cols; ++$colIndex) { 216 | $current = call_user_func($f, $this->_data[$rowIndex][$colIndex], $current, $rowIndex, $colIndex); 217 | } 218 | } 219 | 220 | return $current; 221 | } 222 | 223 | /** 224 | * builds array of elements. 225 | */ 226 | protected function initializeElements() 227 | { 228 | for ($i = 0; $i < $this->_rows; ++$i) { 229 | for ($j = 0; $j < $this->_cols; ++$j) { 230 | $this->_data[$i][$j] = call_user_func($this->factory); 231 | } 232 | } 233 | } 234 | 235 | /** 236 | * Check whether row/col are valid. 237 | * 238 | * @param $row 239 | * @param $col 240 | * 241 | * @return bool 242 | */ 243 | private function checkRange($row, $col) 244 | { 245 | if ($this->_rows <= $row || $row < 0) { 246 | return false; 247 | } 248 | if ($this->_cols <= $col || $col < 0) { 249 | return false; 250 | } 251 | 252 | return true; 253 | } 254 | 255 | /** 256 | * @api 257 | * {@inheritdoc} 258 | */ 259 | public function copyFrom(EntityInterface $target) 260 | { 261 | /* @var $target TypedMatrix */ 262 | $this->_data = $target->_data; 263 | $this->_cols = $target->_cols; 264 | $this->_rows = $target->_rows; 265 | } 266 | 267 | /** 268 | * {@inheritdoc} 269 | */ 270 | public function copyTo(EntityInterface $destination) 271 | { 272 | /* @var $destination TypedMatrix */ 273 | $destination->_data = $this->_data; 274 | $destination->_cols = $this->_cols; 275 | $destination->_rows = $this->_rows; 276 | } 277 | 278 | /** 279 | * @param EntityInterface $target 280 | * 281 | * @return bool 282 | */ 283 | public function equals(EntityInterface $target) 284 | { 285 | /** @var TypedMatrix $target */ 286 | if ( 287 | $this->_cols !== $target->_cols || 288 | $this->_rows !== $target->_rows 289 | ) { 290 | return false; 291 | } 292 | 293 | for ($i = 0; $i < $this->_rows; ++$i) { 294 | for ($j = 0; $j < $this->_cols; ++$j) { 295 | if (is_object($this->_data[$i][$j])) { 296 | if ($this->_data[$i][$j]->equals($target->_data[$i][$j]) === false) { 297 | return false; 298 | } 299 | } else { 300 | if ($this->_data[$i][$j] !== $target->_data[$i][$j]) { 301 | return false; 302 | } 303 | } 304 | } 305 | } 306 | 307 | return true; 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /src/String/UniqueName.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\String; 14 | 15 | /** 16 | * @since Class available since Release 1.1.1 17 | */ 18 | class UniqueName 19 | { 20 | /** 21 | * @var array 22 | */ 23 | private $source; 24 | /** 25 | * @var string 26 | */ 27 | private $numberingPattern; 28 | /** 29 | * @var string 30 | */ 31 | private $numberingPatternRexex; 32 | private $numberingPatternExtractRegex; 33 | 34 | public function __construct($source, $numberingPattern = ' (n)') 35 | { 36 | $this->source = $source; 37 | $this->numberingPattern = $numberingPattern; 38 | $this->numberingPatternRegex = sprintf('/(%s)$/', 39 | str_replace('n', '\d+', preg_quote($this->numberingPattern))); 40 | $this->numberingPatternExtractRegex = sprintf('/#BASE#(%s)$/', 41 | str_replace('n', '(\d+)', preg_quote($this->numberingPattern))); 42 | } 43 | 44 | public function __invoke($name, $alreadyInSource = true) 45 | { 46 | $sameList = array_filter($this->source, function ($targetName) use ($name) { 47 | return $targetName === $name; 48 | }); 49 | if (count($sameList) <= ($alreadyInSource ? 1 : 0)) { 50 | return $name; 51 | } 52 | 53 | $baseName = preg_replace($this->numberingPatternRegex, '', $name); 54 | 55 | $matchedList = array_filter($this->source, function($targetName) use ($baseName) { 56 | return strpos($targetName, $baseName) !== false; 57 | }); 58 | 59 | $max = 0; 60 | $regex = str_replace('#BASE#', $baseName, $this->numberingPatternExtractRegex); 61 | foreach ($matchedList as $targetName) { 62 | if (preg_match($regex, $targetName, $match)) { 63 | $max = max($max, $match[2]); 64 | } 65 | } 66 | 67 | return $baseName . $this->makeNumber($max + 1); 68 | } 69 | 70 | private function makeNumber($number) 71 | { 72 | return str_replace('n', $number, $this->numberingPattern); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/DateTime/AgeRangeTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | /** 16 | * @since Class available since Release 1.1.0 17 | */ 18 | class AgeRangeTest extends \PHPUnit_Framework_TestCase 19 | { 20 | /** 21 | * @param string $target 22 | * 23 | * @return \DateTime 24 | */ 25 | private function date($target) 26 | { 27 | return new DateTime($target); 28 | } 29 | 30 | /** 31 | * @return array 32 | */ 33 | public function getMaxAsDateData() 34 | { 35 | return [ 36 | [$this->date('2015-09-12'), 40, '1974-09-13'], 37 | [$this->date('2015-02-28'), 39, '1975-03-01'], 38 | [$this->date('2016-02-29'), 39, '1976-03-01'], 39 | [$this->date('2016-02-29'), 40, '1975-03-01'], 40 | [$this->date('2015-12-31'), 40, '1975-01-01'], 41 | ]; 42 | } 43 | 44 | /** 45 | * @test 46 | * @dataProvider getMaxAsDateData 47 | */ 48 | public function getMaxAsDate(\DateTimeInterface $currentDate, $max, $maxAsDate) 49 | { 50 | $clock = $this->getMock('PHPMentors\DomainCommons\DateTime\Clock'); 51 | $clock->method('now')->will($this->returnValue($currentDate)); 52 | $ageRange = new AgeRange(); 53 | $ageRange->setClock($clock); 54 | $ageRange->setMax($max); 55 | 56 | $this->assertThat($ageRange->getMaxAsDate()->format('Y-m-d'), $this->equalTo($maxAsDate)); 57 | } 58 | 59 | /** 60 | * @return array 61 | */ 62 | public function getMinAsDateData() 63 | { 64 | return [ 65 | [$this->date('2015-09-12'), 20, '1995-09-12'], 66 | [$this->date('2015-02-28'), 19, '1996-02-28'], 67 | [$this->date('2016-02-29'), 19, '1997-02-28'], 68 | [$this->date('2016-02-29'), 20, '1996-02-29'], 69 | [$this->date('2015-12-31'), 20, '1995-12-31'], 70 | ]; 71 | } 72 | 73 | /** 74 | * @test 75 | * @dataProvider getMinAsDateData 76 | */ 77 | public function getMinAsDate(\DateTimeInterface $currentDate, $min, $minAsDate) 78 | { 79 | $clock = $this->getMock('PHPMentors\DomainCommons\DateTime\Clock'); 80 | $clock->method('now')->will($this->returnValue($currentDate)); 81 | $ageRange = new AgeRange(); 82 | $ageRange->setClock($clock); 83 | $ageRange->setMin($min); 84 | 85 | $this->assertThat($ageRange->getMinAsDate()->format('Y-m-d'), $this->equalTo($minAsDate)); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /tests/DateTime/DateTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class DateTest extends \PHPUnit_Framework_TestCase 16 | { 17 | /** 18 | * @test 19 | * @dataProvider instanciateData 20 | */ 21 | public function instanciate($dateStr, $expected) 22 | { 23 | $instance = new Date($dateStr); 24 | 25 | $this->assertThat($instance->format('Y-m-d H:i:s'), $this->equalTo($expected)); 26 | } 27 | 28 | public function instanciateData() 29 | { 30 | return [ 31 | ['2014-03-21 12:34:55', '2014-03-21 00:00:00'], 32 | ]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/DateTime/DateTimeTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class DateTimeTest extends \PHPUnit_Framework_TestCase 16 | { 17 | /** 18 | * @test 19 | * @param string $dateStr 20 | * @param int $days 21 | * @param string $expected 22 | * @dataProvider addDaysData 23 | */ 24 | public function addDays($dateStr, $days, $expected) 25 | { 26 | $date = new DateTime($dateStr); 27 | $added = $date->addDays($days); 28 | 29 | $this->assertThat($added->format('Y-m-d'), $this->equalTo($expected)); 30 | } 31 | 32 | public function addDaysData() 33 | { 34 | return [ 35 | ['2015-03-21', 3, '2015-03-24'], 36 | ['2014-12-19', 25, '2015-01-13'], 37 | ['2014-12-19', -3, '2014-12-16'], 38 | ]; 39 | } 40 | 41 | /** 42 | * @test 43 | * @param string $dateStr 44 | * @param int $months 45 | * @param string $expected 46 | * @dataProvider addMonthsData 47 | */ 48 | public function addMonths($dateStr, $months, $expected) 49 | { 50 | $date = new DateTime($dateStr); 51 | 52 | $added = $date->addMonths($months); 53 | 54 | $this->assertThat($added->format('Y-m-d'), $this->equalTo($expected)); 55 | } 56 | 57 | public function addMonthsData() 58 | { 59 | return [ 60 | ['2015-03-21', 3, '2015-06-21'], 61 | ['2017-05-30', 1, '2017-06-30'], 62 | ['2014-12-19', 5, '2015-05-19'], 63 | ['2014-12-19', -2, '2014-10-19'], 64 | ]; 65 | } 66 | 67 | /** 68 | * @test 69 | * @param string $dateStr 70 | * @param int $months 71 | * @dataProvider addMonthsThrowsExceptionData 72 | * @expectedException \PHPMentors\DomainCommons\DateTime\Exception\UnsupportedCalculation 73 | */ 74 | public function addMonthsThrowsException($dateStr, $months) 75 | { 76 | $date = new DateTime($dateStr); 77 | 78 | $date->addMonths($months); 79 | 80 | $this->fail(); 81 | } 82 | 83 | public function addMonthsThrowsExceptionData() 84 | { 85 | return [ 86 | ['2015-01-30', 1], 87 | ['2017-05-31', 1], 88 | ['2017-01-29', 1], 89 | ['2017-01-30', 1], 90 | ['2017-01-31', 1], 91 | ]; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /tests/DateTime/HourMinTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class HourMinTest extends \PHPUnit_Framework_TestCase 16 | { 17 | /** 18 | * @test 19 | * @dataProvider instanciateData 20 | */ 21 | public function instanciate($dateStr, $expected) 22 | { 23 | $instance = new HourMin($dateStr); 24 | 25 | $this->assertThat((string) $instance, $this->equalTo($expected)); 26 | } 27 | 28 | public function instanciateData() 29 | { 30 | return [ 31 | ['2014-03-21 12:34:55', '12:34'], 32 | ]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/DateTime/MonthDayTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class MonthDayTest extends \PHPUnit_Framework_TestCase 16 | { 17 | /** 18 | * @test 19 | * @dataProvider instanciateData 20 | */ 21 | public function instanciate($dateStr, $expected) 22 | { 23 | $instance = new MonthDay($dateStr); 24 | 25 | $this->assertThat((string) $instance, $this->equalTo($expected)); 26 | } 27 | 28 | public function instanciateData() 29 | { 30 | return [ 31 | ['2014-03-21 12:34:55', '03-21'], 32 | ]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/DateTime/PeriodTraitTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | use PHPMentors\DomainCommons\DateTime\Period\DailyIteratableInterface; 16 | use PHPMentors\DomainCommons\DateTime\Period\DailyTrait; 17 | 18 | class DailyPeriod extends Period implements DailyIteratableInterface 19 | { 20 | use DailyTrait; 21 | 22 | public function __construct($start, $end) 23 | { 24 | parent::__construct($start, $end); 25 | $this->it = $this->iterate(); 26 | } 27 | } 28 | 29 | class PeriodTraitTest extends \PHPUnit_Framework_TestCase 30 | { 31 | /** 32 | * @test 33 | * @dataProvider dailyPeriodTest 34 | */ 35 | public function dailyPeriod($start, $end, $expectedCount) 36 | { 37 | $period = new DailyPeriod(new Date($start), new Date($end)); 38 | 39 | $count = 0; 40 | foreach ($period as $one) { 41 | ++$count; 42 | } 43 | $this->assertThat($count, $this->equalTo($expectedCount)); 44 | } 45 | 46 | public function dailyPeriodTest() 47 | { 48 | return [ 49 | ['2015-04-02', '2015-04-05', 4], 50 | ['2015-04-03', '2015-05-10', 38], 51 | ]; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /tests/DateTime/ThreeMonthlyTraitTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | use PHPMentors\DomainCommons\DateTime\Period\MonthlyIteratableInterface; 16 | use PHPMentors\DomainCommons\DateTime\Period\ThreeMonthlyTrait; 17 | 18 | class LongTerm extends Term implements MonthlyIteratableInterface 19 | { 20 | use ThreeMonthlyTrait; 21 | 22 | public function __construct($start, $end) 23 | { 24 | parent::__construct($start, $end); 25 | $this->setTermFactory(function($start, $end){ 26 | return new OneTerm($start, $end); 27 | }); 28 | $this->it = $this->iterate(); 29 | } 30 | 31 | public function getStart(){return $this->start;} 32 | public function getEnd(){return $this->end;} 33 | } 34 | 35 | class OneTerm extends Term 36 | { 37 | public function getStart(){return $this->start;} 38 | public function getEnd(){return $this->end;} 39 | } 40 | 41 | class ThreeMonthlyTraitTest extends \PHPUnit_Framework_TestCase 42 | { 43 | /** 44 | * @test 45 | * @dataProvider everyTheeMonthsTest 46 | */ 47 | public function everyTheeMonthsPeriod($start, $end, $expectedTerms) 48 | { 49 | $term = new LongTerm(new Date($start), new Date($end)); 50 | 51 | $count = 0; 52 | foreach ($term as $oneTerm) { 53 | $this->assertThat( 54 | $oneTerm->getStart()->format('Y-m-d'), 55 | $this->equalTo($expectedTerms[$count][0])); 56 | $this->assertThat( 57 | $oneTerm->getEnd()->format('Y-m-d'), 58 | $this->equalTo($expectedTerms[$count][1])); 59 | ++$count; 60 | } 61 | $this->assertThat($count, $this->equalTo(count($expectedTerms))); 62 | } 63 | 64 | public function everyTheeMonthsTest() 65 | { 66 | return [ 67 | ['2015-01-01', '2015-10-31', [ 68 | ['2015-01-01', '2015-03-31'], 69 | ['2015-04-01', '2015-06-30'], 70 | ['2015-07-01', '2015-09-30'], 71 | ['2015-10-01', '2015-10-31'], 72 | ]], 73 | ['2015-02-01', '2015-03-31', [ 74 | ['2015-02-01', '2015-03-31'], 75 | ]], 76 | ['2015-02-01', '2015-04-30', [ 77 | ['2015-02-01', '2015-04-30'], 78 | ]], 79 | ['2015-02-01', '2016-03-31', [ 80 | ['2015-02-01', '2015-04-30'], 81 | ['2015-05-01', '2015-07-31'], 82 | ['2015-08-01', '2015-10-31'], 83 | ['2015-11-01', '2016-01-31'], 84 | ['2016-02-01', '2016-03-31'], 85 | ]], 86 | ['2015-02-01', '2015-04-15', [ 87 | ['2015-02-01', '2015-04-15'], 88 | ]], 89 | ['2015-02-24', '2015-07-09', [ 90 | ['2015-02-24', '2015-04-30'], 91 | ['2015-05-01', '2015-07-09'], 92 | ]], 93 | ]; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /tests/DateTime/YearMonthTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class YearMonthTest extends \PHPUnit_Framework_TestCase 16 | { 17 | /** 18 | * @test 19 | * @dataProvider instanciateData 20 | */ 21 | public function instanciate($dateStr, $expected) 22 | { 23 | $instance = new YearMonth($dateStr); 24 | 25 | $this->assertThat((string) $instance, $this->equalTo($expected)); 26 | } 27 | 28 | public function instanciateData() 29 | { 30 | return [ 31 | ['2014-03-21 12:34:55', '2014-03'], 32 | ]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/DateTime/YearTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\DateTime; 14 | 15 | class YearTest extends \PHPUnit_Framework_TestCase 16 | { 17 | /** 18 | * @test 19 | * @dataProvider instanciateData 20 | */ 21 | public function instanciate($dateStr, $expected) 22 | { 23 | $instance = new Year($dateStr); 24 | 25 | $this->assertThat($instance->format('Y-m-d H:i:s'), $this->equalTo($expected)); 26 | } 27 | 28 | public function instanciateData() 29 | { 30 | return [ 31 | ['2014-03-21 12:34:55', '2014-01-01 00:00:00'], 32 | ]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/String/UniqueNameTest.php: -------------------------------------------------------------------------------- 1 | , 4 | * All rights reserved. 5 | * 6 | * This file is part of Domain Commons. 7 | * 8 | * This program and the accompanying materials are made available under 9 | * the terms of the BSD 2-Clause License which accompanies this 10 | * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause 11 | */ 12 | 13 | namespace PHPMentors\DomainCommons\String; 14 | 15 | /** 16 | * @since Class available since Release 1.1.1 17 | */ 18 | class UniqueNameTest extends \PHPUnit_Framework_TestCase 19 | { 20 | public function test() 21 | { 22 | $list = [ 23 | 'test1','test2','apple banana','test1 (1)','test2 (3)','php','' 24 | ]; 25 | $uniqueName = new UniqueName($list); 26 | $result = $uniqueName('test1'); 27 | $this->assertThat($result, $this->equalTo('test1')); 28 | $result = $uniqueName('test1', false); 29 | $this->assertThat($result, $this->equalTo('test1 (2)')); 30 | $result = $uniqueName('test2 (2)'); 31 | $this->assertThat($result, $this->equalTo('test2 (2)')); 32 | 33 | 34 | $list = [ 35 | 'test1','test1', 36 | ]; 37 | $uniqueName = new UniqueName($list); 38 | $result = $uniqueName('test1'); 39 | $this->assertThat($result, $this->equalTo('test1 (1)')); 40 | $result = $uniqueName('test1', false); 41 | $this->assertThat($result, $this->equalTo('test1 (1)')); 42 | 43 | 44 | $list = [ 45 | 'test1','test1 (1)', 46 | ]; 47 | $uniqueName = new UniqueName($list); 48 | $result = $uniqueName('test1', false); 49 | $this->assertThat($result, $this->equalTo('test1 (2)')); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |