Last active
June 10, 2020 18:38
-
-
Save hakre/4447499 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Get all 52 weeks of the year and work days given the first day of the January of current year | |
* @link http://stackoverflow.com/a/14147613/367456 | |
* @author hakre <http://hakre.wordpress.com/credits> | |
*/ | |
/** | |
* Filter a DatePeriod by year | |
*/ | |
class DatePeriodYearFilter extends FilterIterator | |
{ | |
private $year; | |
public function __construct(DatePeriod $period, $year) { | |
$this->year = $year; | |
parent::__construct(new IteratorIterator($period)); | |
} | |
public function accept() { | |
return $this->current()->format('Y') == $this->year; | |
} | |
} | |
/** | |
* Iterator that iterates over the first and last element of | |
* an Iterator | |
*/ | |
class FirstAndLastIterator extends CachingIterator | |
{ | |
public function __construct(Iterator $iterator) { | |
parent::__construct($iterator, self::TOSTRING_USE_KEY); | |
} | |
public function next() { | |
while (parent::hasNext()) { | |
parent::next(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment