-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Class to gather namespace actions. | ||
* | ||
* @license http://www.apache.org/licenses Apache License Version 2.0, January 2004 | ||
* | ||
* @package jackalope | ||
*/ | ||
|
||
namespace Jackalope; | ||
|
||
/** | ||
* Class to gather namespace actions. | ||
* | ||
* @package jackalope | ||
* | ||
*/ | ||
class NamespaceManager { | ||
|
||
/** | ||
* List of predefined namespaces. | ||
* @var array | ||
*/ | ||
protected $defaultNamespaces = array(); | ||
|
||
/** | ||
* Initializes the object to be instantiated. | ||
* | ||
* @param array $defaultNamespaces Set of predefined namespaces. | ||
*/ | ||
public function __construct($defaultNamespaces) { | ||
$this->defaultNamespaces = $defaultNamespaces; | ||
} | ||
|
||
/** | ||
* Verifies the correctness of the given prefix. | ||
* | ||
* Throws the \PHPCR\NamespaceException if an attempt is made to re-assign | ||
* a built-in prefix to a new URI or, to register a namespace with a prefix | ||
* that begins with the characters "xml" (in any combination of case) | ||
* | ||
* @return void | ||
* | ||
* @throws \PHPCR\NamespaceException if re-assign built-in prefix or prefix starting with xml | ||
*/ | ||
protected function checkPrefix($prefix) { | ||
if (! strncasecmp('xml', $prefix, 3)) { | ||
throw new \PHPCR\NamespaceException('Do not use xml in prefixes for namespace changes'); | ||
} | ||
if (array_key_exists($prefix, $this->defaultNamespaces)) { | ||
throw new \PHPCR\NamespaceException('Do not change the predefined prefixes'); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
class NamespaceManagerTest extends PHPUnit_Framework_TestCase { | ||
|
||
/*************************************************************************/ | ||
/* Fixtures | ||
/*************************************************************************/ | ||
|
||
/** | ||
* Create a list of namespaces defined by the \PHPCR\NamespaceRegistryInterface. | ||
* | ||
* @return array Set of namespaces. | ||
*/ | ||
public function getDefaultNamespacesFixture() { | ||
return array( | ||
"jcr" => "http://www.jcp.org/jcr/1.0", | ||
"nt" => "http://www.jcp.org/jcr/nt/1.0", | ||
"mix" => "http://www.jcp.org/jcr/mix/1.0", | ||
"xml" => "http://www.w3.org/XML/1998/namespace", | ||
"" => "" | ||
); | ||
} | ||
|
||
/*************************************************************************/ | ||
/* Tests | ||
/*************************************************************************/ | ||
|
||
/** | ||
* @covers NamespaceManager::checkPrefix | ||
*/ | ||
public function testCheckPrefix() { | ||
$prefix = 'beastie'; | ||
$ns = new NamespaceManagerProxy($this->getDefaultNamespacesFixture()); | ||
|
||
$this->assertNull($ns->checkPrefix($prefix)); | ||
} | ||
|
||
/** | ||
* @dataProvider checkPrefixDataprovider | ||
* @covers NamespaceManager::checkPrefix | ||
* @expectedException \PHPCR\NamespaceException | ||
*/ | ||
public function testCheckPrefixExpexctingNamespaceException($prefix) { | ||
$ns = new NamespaceManagerProxy($this->getDefaultNamespacesFixture()); | ||
$ns->checkPrefix($prefix); | ||
} | ||
|
||
|
||
/*************************************************************************/ | ||
/* Dataprovider | ||
/*************************************************************************/ | ||
|
||
public static function checkPrefixDataprovider() { | ||
return array( | ||
'XML as prefix' => array('xml'), | ||
'prefix in list of default namespaces' => array('jcr'), | ||
'empty prefix' => array(''), | ||
); | ||
} | ||
} | ||
|
||
class NamespaceManagerProxy extends \Jackalope\NamespaceManager { | ||
|
||
public function checkPrefix($prefix) { | ||
return parent::checkPrefix($prefix); | ||
} | ||
} |
This file contains 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