Skip to content

Commit

Permalink
Merge branch 'release/0.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
shulard committed May 5, 2015
2 parents 223e3bd + 3987aa3 commit 85f17fe
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bee4/robots.txt v0.0.0
bee4/robots.txt v0.0.1
======================

[![Build Status](https://travis-ci.org/bee4/robots.txt.svg?branch=develop)](https://travis-ci.org/bee4/robots.txt)
Expand Down
65 changes: 65 additions & 0 deletions src/Exception/DuplicateRuleException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* This file is part of the beebot package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Bee4 2015
* @author Stephane HULARD <[email protected]>
* @package Bee4\RobotsTxt\Exception
*/

namespace Bee4\RobotsTxt\Exception;

use Exception;
use Bee4\RobotsTxt\Rule;

/**
* Class DuplicateRuleException
* Error thrown when the parser try to add 2 rules for the same UA
* @package Bee4\RobotsTxt\Exception
*/
class DuplicateRuleException extends Exception
{
/**
* @var Rule
*/
protected $rule;

/**
* @var string
*/
protected $ua;

/**
* Rule setter
* @param Rule $rule
* @return DuplicateRuleException
*/
public function setRule(Rule $rule) {
$this->rule = $rule;
return $this;
}
/**
* @return Rule
*/
public function getRule() {
return $this->rule;
}

/**
* User Agent setter
* @param string $ua
* @return DuplicateRuleException
*/
public function setUserAgent($ua) {
$this->ua = $ua;
return $this;
}
/**
* @return string
*/
public function getUserAgent() {
return $this->ua;
}
}
1 change: 1 addition & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Parser
protected $content;

public function __construct($content) {
//Remove the UTF8 BOM
$this->content = trim($content, "\xEF\xBB\xBF");
}

Expand Down
21 changes: 19 additions & 2 deletions src/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Bee4\RobotsTxt;

use Bee4\RobotsTxt\Exception\DuplicateRuleException;

/**
* Class Rules
* Represent a collection of Rules
Expand All @@ -26,6 +28,13 @@ class Rules
*/
protected $collection = [];

private $defaultRule;

public function __construct() {
$this->defaultRule = new Rule();
$this->add(self::DEFAULT_UA, $this->defaultRule);
}

/**
* Add a new rule to the collection
* @param string $ua
Expand All @@ -34,14 +43,22 @@ class Rules
*/
public function add($ua, Rule $rule) {
$ua = $this->handleUa($ua);
if( isset($this->collection[$ua]) ) {
throw new \RuntimeException('You can\'t add 2 rules for the same UserAgent');
if( isset($this->collection[$ua]) && $this->collection[$ua] !== $this->defaultRule ) {
throw (new DuplicateRuleException('You can\'t add 2 rules for the same UserAgent'))
->setRule($rule)
->setUserAgent($ua);
}
$this->collection[$ua] = $rule;

return $this;
}

/**
* Check if the URL match for the given UA or not
* @param string $ua
* @param string $url
* @return boolean
*/
public function match($ua, $url) {
if( ($rule = $this->get($ua)) === null ) {
return false;
Expand Down
27 changes: 26 additions & 1 deletion test/units/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Test\Bee4\RobotsTxt;

use Bee4\RobotsTxt\Parser;
use Bee4\RobotsTxt\Rules;
use Bee4\RobotsTxt\ParserFactory;

/**
Expand All @@ -27,6 +28,13 @@ class ParserTest extends \PHPUnit_Framework_TestCase
Allow: /truite.php
disallow: /";

protected $duplicateRuleContent = "User-agent: *
Disallow: /mentions-legales/
User-agent: *
Allow: /truite.php";


public function testParse() {
$object = new Parser($this->content);
$rules = $object->parse();
Expand All @@ -41,9 +49,26 @@ public function testParse() {
$this->assertTrue($rules->match('Google-Bot v01', '/truite.php'));
}

public function testEmptyContentParse() {
$object = new Parser("");
$rules = $object->parse();

$rule = $rules->get(Rules::DEFAULT_UA);
$this->assertInstanceOf('\Bee4\RobotsTxt\Rule', $rule);
$this->assertTrue($rule->match('/another-page.html'));
}


/**
* @expectedException Bee4\RobotsTxt\Exception\DuplicateRuleException
*/
public function testDuplicateRuleParse() {
$object = new Parser($this->duplicateRuleContent);
$rules = $object->parse();
}

public function testParserFactory() {
$rules = ParserFactory::build("http://www.bee4.fr");

$this->assertInstanceOf('\Bee4\RobotsTxt\Rule', $rules->get('*'));
}
}

0 comments on commit 85f17fe

Please sign in to comment.