/** * @link https://bitbucket.org/t1gor/strategy/src/242e58cdcd60c61d02ae26d420da9d415117cb0d/application/model/map/MapTileNeighboursIterator.php?at=default */ class TileIterator implements Iterator { private $_side = 'north_west'; private $_neighbours = array(); private $_isValid = true; public function __construct($neighboursArray) { $this->_side = 'north_west'; $this->_neighbours = $neighboursArray; } /** * @return void */ function rewind() { $this->_side = 'north_west'; } /** * @return MapTile */ function current() { return $this->_neighbours[$this->_side]; } /** * @return string */ function key() { return $this->_side; } /** * Loop through neighbours clock-wise * * @return void */ function next() { switch ($this->_side) { case 'north_west': $this->_side = 'north'; break; case 'north': $this->_side = 'north_east'; break; case 'north_east': $this->_side = 'east'; break; case 'east': $this->_side = 'south_east'; break; case 'south_east': $this->_side = 'south'; break; case 'south': $this->_side = 'south_west'; break; case 'south_west': $this->_side = 'west'; break; // this is the end of a circle case 'west': $this->_isValid = false; break; } } function valid() { return $this->_isValid; } }
// , .. $tilesStmt = PDO::prepare("SELECT * FROM tiles ... LIMIT 9"); $tilesStmt->execute(); $tiles = new TileIterator($tilesStmt->fetchAll());
foreach ($tiles as $tile) { ... }
// ... $inputFileType = PHPExcel_IOFactory::identify('example.xlsx'); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $document = $objReader->load($inputFile); $sheet = $document->getSheet(0); // ... 10 $dataForDebug = new LimitIterator($sheet->getRowIterator(), 0, 10);
/** * @link http://ua2.php.net/FilterIterator */ class NotificationFilter extends FilterIterator { /** * */ private $_skip; /** * Build filter * * @param Iterator $iterator * @param array $filter - , * @throws InvalidArgumentException */ public function __construct(Iterator $iterator, $filter) { if (!is_array($filter)) { throw new InvalidArgumentException("Filter should be an array. ".gettype($filter)." given."); } parent::__construct($iterator); $this->_skip = $filter; } /** * Check user data and make sure we can notify him/her * * Filtering by 2 params: * - Does the user belong to your company (avoid spamming clients)? * - Should we skipp the user based on the user ID * - Should we skipp the user based on the user email * * @link http://php.net/manual/filteriterator.accept.php * @link https://github.com/sirprize/basecamp/blob/master/example/basecamp/person/get-by-id * * @return bool */ public function accept() { // get current user from the Iterator $bcUser = $this->getInnerIterator()->current(); // check if skipped by ID $skippedById = in_array($bcUser->getId(), $this->_skip['byID']); // or by email $skippedByEmail = in_array($bcUser->getEmailAddress(), $this->_skip['byEmail']); // check that he/she belongs to your company $belongsToCompany = $yourCompanyBaseCampID === (int) $bcUser->getCompanyId()->__toString(); // notify only if belongs to your company and shouldn't be skipped return $belongsToCompany && !$skippedById && !$skippedByEmail; } }
/** * Basic post class */ class HabraPost { public $name = ''; public $url = ''; public $hubs = null; public static $baseUrl = 'http://habrahabr.ru/hub/'; /** * Some hubs links */ protected static $fullHubList = array( 'infosecurity' => ' ', 'webdev' => '-', 'gdev' => 'Game Development', 'DIY' => 'DIY ', 'pm' => ' ', 'programming' => '', 'space' => '', 'hardware' => '', 'algorithms' => '', 'image_processing' => ' ', ); public function __construct($name, $url, $hubs = array()) { $this->name = $name; $this->url = $url; $this->hubs = $hubs; } public static function getFullHubsList() { $list = self::$fullHubList; asort($list); return $list; } } /** * Post storage object * * @link http://php.net/manual/class.splobjectstorage.php */ class PostsStorage { private $_iterator; public function __construct() { $this->_iterator = new SplObjectStorage(); } /** * Add new post * * @param HabraPost $post * @return void */ public function save(HabraPost $post) { // reduce duplicates if (!$this->_iterator->contains($post)) { $this->_iterator->attach($post); } } /** * Get internal iterator * * @return SplObjectStorage */ public function getIterator() { return $this->_iterator; } } /** * Posts filtering class * * @link http://php.net/manual/class.filteriterator.php */ class HabraPostFilter extends FilterIterator { /** * Hubs to filter by */ private $_filterByHubs = array(); public function __construct(Iterator $iterator, $filteringHubs) { parent::__construct($iterator); $this->_filterByHubs = $filteringHubs; } /** * Accept * * @link http://php.net/manual/filteriterator.accept.php * @return bool */ public function accept() { $object = $this->getInnerIterator()->current(); $aggregate = true; foreach ($this->_filterByHubs as $filterHub) { $aggregate = $aggregate && in_array($filterHub, $object->hubs); } return $aggregate; } }
PostsStorage
,HabraPostFilter
Source: https://habr.com/ru/post/214833/
All Articles