PHP Classes

File: Infrastructure/Repository/PageRepository.php

Recommend this page to a friend!
  Classes of Igor Dyshlenko   PHP Image Crawler   Infrastructure/Repository/PageRepository.php   Download  
File: Infrastructure/Repository/PageRepository.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Image Crawler
Crawl Web site pages to find images in the pages
Author: By
Last change:
Date: 3 years ago
Size: 1,273 bytes
 

Contents

Class file image Download
<?php

namespace Infrastructure\Repository;

use
ArrayIterator;
use
Domain\Page;
use
RuntimeException;

class
PageRepository
{
   
/**
     * @var array $pages
     */
   
protected $pages = [];

   
/**
     * @param Page $page
     */
   
public function store(Page $page): void
   
{
       
$this->pages[$page->getUrl()] = $page;
    }

   
/**
     * @param string $url
     *
     * @return Page
     */
   
public function get(string $url): ?Page
   
{
        return
$this->pages[$url] ?? null;
    }

   
/**
     * Get Iterator for stored pages.
     *
     * @return ArrayIterator iterator.
     */
   
public function getPagesIterator(): ArrayIterator
   
{
        return new
ArrayIterator($this->pages);
    }

   
/**
     * Sort the contents of the repository using the uasort() function. Affects the crawl order
     * of the contents of the repository using an iterator obtained from PageRepository :: getPagesIterator ().
     *
     * @param callable $compareFunction - callable function for uasort function.
     * @throws RuntimeException
     */
   
public function order(callable $compareFunction): void
   
{
        if (!
uasort($this->pages, $compareFunction)) {
            throw new
RuntimeException('Error sorting results.');
        }
    }
}