Skip to content

Instantly share code, notes, and snippets.

@hakre
Last active June 10, 2020 18:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hakre/4447499 to your computer and use it in GitHub Desktop.
Save hakre/4447499 to your computer and use it in GitHub Desktop.
<?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