1 | <?php |
||
15 | class Rules implements \Countable |
||
16 | { |
||
17 | const DEFAULT_UA = '*'; |
||
18 | |||
19 | /** |
||
20 | * The collection of rules |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $collection = []; |
||
24 | |||
25 | protected $sitemaps = []; |
||
26 | |||
27 | /** |
||
28 | * Default rule used if robots.txt is empty |
||
29 | * @var Rule |
||
30 | */ |
||
31 | private $defaultRule; |
||
32 | |||
33 | public function __construct() |
||
38 | |||
39 | public function addSitemap($sitemap) |
||
47 | |||
48 | public function getSitemaps() |
||
52 | |||
53 | /** |
||
54 | * Add a new rule to the collection |
||
55 | * @param Rule $rule |
||
56 | * @return Rules |
||
57 | */ |
||
58 | public function add(Rule $rule) |
||
72 | |||
73 | /** |
||
74 | * Check if the URL match for the given UA or not |
||
75 | * @param string $userAgent |
||
76 | * @param string $url |
||
77 | * @return boolean |
||
78 | */ |
||
79 | public function match($userAgent, $url) |
||
83 | |||
84 | /** |
||
85 | * Retrieve rules for a given UA |
||
86 | * @param string $userAgent |
||
87 | * @return null|Rule |
||
88 | */ |
||
89 | public function get($userAgent) |
||
90 | { |
||
91 | 1 | $item = null; |
|
92 | 1 | $iterator = new \ArrayIterator($this->collection); |
|
93 | 1 | iterator_apply( |
|
94 | $iterator, |
||
95 | 1 | function (\ArrayIterator $iterator, $userAgent) use (&$item) { |
|
96 | 1 | if ($iterator->key() != Rules::DEFAULT_UA && |
|
97 | 1 | preg_match($iterator->key(), $userAgent) === 1 ) { |
|
98 | 1 | $item = $iterator->current(); |
|
99 | 1 | return false; |
|
100 | } |
||
101 | 1 | return true; |
|
102 | 1 | }, |
|
103 | 1 | [$iterator, $userAgent] |
|
104 | ); |
||
105 | |||
106 | 1 | return $item!==null? |
|
107 | 1 | $item: |
|
108 | 1 | $this->collection[self::DEFAULT_UA]; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Update the UA to make a valid regexp |
||
113 | * @param string $userAgent |
||
114 | * @return string |
||
115 | */ |
||
116 | private function handleUa($userAgent) |
||
123 | |||
124 | /** |
||
125 | * Return the number of rules |
||
126 | * @return integer |
||
127 | */ |
||
128 | public function count() |
||
132 | } |
||
133 |