Last active
September 7, 2015 13:41
-
-
Save sword-jin/4d2cda3bbad646db21bb to your computer and use it in GitHub Desktop.
SPL filtering values
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 | |
$courses = simplexml_load_file('common/data/courses.xml', 'SimpleXMLIterator'); | |
$courses = new CallbackFilterIterator($courses, 'getCoursesByLevel'); | |
foreach ($courses as $course) { | |
echo "$course->title with $course->author (level: $course->level)<br />"; | |
} | |
function getCoursesByLevel($current) | |
{ | |
return strtolower($current->level) == 'beginner'; | |
} | |
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 | |
class AuthorFilter extends FilterIterator | |
{ | |
protected $author; | |
public function __construct(Iterator $iterator, $author) | |
{ | |
parent::__construct($iterator); | |
$this->author = $author; | |
} | |
public function accept() | |
{ | |
return $this->current()->author == $this->author; | |
} | |
} | |
$courses = simplexml_load_file('common/data/courses.xml', 'SimpleXMLIterator'); | |
$courses = new AuthorFilter($courses, 'David Powers'); | |
foreach ($courses as $course) { | |
echo "$course->title by author <b>$course->author</b><br />"; | |
} |
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 | |
$files = new GlobIterator(__DIR__ . '/common/images/*.jpg'); | |
foreach ($files as $file) { | |
echo $file->getFilename() . '<br />'; | |
} |
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 | |
$courses = simplexml_load_file('common/data/courses.xml', 'SimpleXMLIterator'); | |
$courses = new LimitIterator($courses, 10, 5); | |
foreach ($courses as $course) { | |
echo $courses->getPosition() + 1 . ".$course->title with $course->author (duration: $course->duration)<br />"; | |
} |
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 | |
$dirs = new RecursiveDirectoryIterator('.'); | |
$dirs = new ParentIterator($dirs); | |
$dirs = new RecursiveIteratorIterator($dirs, RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($dirs as $dir) { | |
echo $dir->getPathname() . '<br />'; | |
} | |
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 | |
$files = new RecursiveDirectoryIterator('common'); | |
$files->setFlags(RecursiveDirectoryIterator::SKIP_DOTS); | |
$files = new RecursiveCallbackFilterIterator($files, function ($current, $key, $iterator) { | |
if ($iterator->hasChildren()) { | |
return true; | |
} | |
return $current->getSize() > 1024 * 6; | |
}); | |
$files = new RecursiveIteratorIterator($files); | |
foreach ($files as $file) { | |
echo $file->getPathname() . ' is ' . round($file->getSize() / 1024, 1) . 'kb<br />'; | |
} |
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 | |
class FilterByExtension extends RecursiveFilterIterator | |
{ | |
protected $extensions; | |
public function __construct(RecursiveIterator $iterator, $extensions) | |
{ | |
parent::__construct($iterator); | |
$this->extensions = is_array($extensions) ? $extensions : (array) $extensions; | |
} | |
public function accept() | |
{ | |
if ($this->hasChildren()) { | |
return true; | |
} | |
$images = ['png', 'jpg', 'gif', 'webp']; | |
return $this->current()->isFile() and in_array(strtolower($this->current()->getExtension()), $this->extensions); | |
} | |
public function getChildren() | |
{ | |
return new self($this->getInnerIterator()->getChildren(), $this->extensions); | |
} | |
} | |
$files = new RecursiveDirectoryIterator('.'); | |
$files->setFlags(FilesystemIterator::UNIX_PATHS); | |
$files = new FilterByExtension($files, ['png', 'xml']); | |
$files = new RecursiveIteratorIterator($files); | |
foreach ($files as $file) { | |
echo $file->getPathname() . '<br />'; | |
} | |
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 | |
$files = new RecursiveDirectoryIterator('.', FilesystemIterator::UNIX_PATHS); | |
$files = new RecursiveIteratorIterator($files); | |
$files = new RegexIterator($files, '/\.(?:csv|docx)$/i'); | |
foreach ($files as $file) { | |
echo $file->getPathname() . '<br />'; | |
} |
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 | |
$courses = simplexml_load_file('common/data/courses.xml', 'SimpleXMLIterator'); | |
foreach ($courses as $course) { | |
$matches = new RegexIterator($course->author, '/joh?n peck/i'); | |
var_dump($matches); | |
foreach ($matches as $match) { | |
echo $course->title . ' with ' . $match . ' (Duration: ' . $course->duration . ')<br />'; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment