Skip to content

Commit

Permalink
Restructured domain tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkphl committed Feb 12, 2017
1 parent 26a2991 commit f45d0de
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 2 deletions.
59 changes: 59 additions & 0 deletions src/Rdfalite/Application/Exceptions/OutOfBoundsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* rdfa-lite
*
* @category Jkphl
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Application
* @author Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @copyright Copyright © 2017 Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/

/***********************************************************************************
* The MIT License (MIT)
*
* Copyright © 2017 Joschi Kuphal <joschi@kuphal.net> / @jkphl
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************/

namespace Jkphl\Rdfalite\Application\Exceptions;

/**
* RuntimeException
*
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Application
*/
class OutOfBoundsException extends \RuntimeException implements RdfaliteApplicationExceptionInterface
{
/**
* Unknown vocabulary prefix
*
* @var string
*/
const UNKNOWN_VOCABULARY_PREFIX_STR = 'Unknown vocabulary prefix "%s"';
/**
* Unknown vocabulary prefix
*
* @var int
*/
const UNKNOWN_VOCABULARY_PREFIX = 1486928423;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* rdfa-lite
*
* @category Jkphl
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Application
* @author Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @copyright Copyright © 2017 Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/

/***********************************************************************************
* The MIT License (MIT)
*
* Copyright © 2017 Joschi Kuphal <joschi@kuphal.net> / @jkphl
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************/

namespace Jkphl\Rdfalite\Application\Exceptions;

use Jkphl\Rdfalite\Domain\Exceptions\RdfaliteExceptionInterface;

/**
* RDFa Lite application exception interface
*
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Application
*/
interface RdfaliteApplicationExceptionInterface extends RdfaliteExceptionInterface
{

}
59 changes: 59 additions & 0 deletions src/Rdfalite/Application/Exceptions/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* rdfa-lite
*
* @category Jkphl
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Application
* @author Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @copyright Copyright © 2017 Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/

/***********************************************************************************
* The MIT License (MIT)
*
* Copyright © 2017 Joschi Kuphal <joschi@kuphal.net> / @jkphl
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************/

namespace Jkphl\Rdfalite\Application\Exceptions;

/**
* RuntimeException
*
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Application
*/
class RuntimeException extends \RuntimeException implements RdfaliteApplicationExceptionInterface
{
/**
* Invalid vocabulary prefix
*
* @var string
*/
const INVALID_VOCABULARY_PREFIX_STR = 'Invalid vocabulary prefix "%s"';
/**
* Invalid vocabulary prefix
*
* @var int
*/
const INVALID_VOCABULARY_PREFIX = 1486927326;
}
198 changes: 198 additions & 0 deletions src/Rdfalite/Application/Parser/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

/**
* rdfa-lite
*
* @category Jkphl
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Application
* @author Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @copyright Copyright © 2017 Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/

/***********************************************************************************
* The MIT License (MIT)
*
* Copyright © 2017 Joschi Kuphal <joschi@kuphal.net> / @jkphl
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************/

namespace Jkphl\Rdfalite\Application\Parser;

use Jkphl\Rdfalite\Application\Exceptions\OutOfBoundsException;
use Jkphl\Rdfalite\Application\Exceptions\RuntimeException;
use Jkphl\Rdfalite\Domain\Vocabulary\Vocabulary;
use Jkphl\Rdfalite\Domain\Vocabulary\VocabularyInterface;

/**
* Parsing context
*
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Application
*/
class Context
{
/**
* Default vocabularies and their prefixes
*
* @var array
* @see https://www.w3.org/2011/rdfa-context/rdfa-1.1
* @link https://www.w3.org/2013/json-ld-context/rdfa11
*/
protected static $defaultVocabs = [
'cat' => 'http://www.w3.org/ns/dcat#',
'qb' => 'http://purl.org/linked-data/cube#',
'grddl' => 'http://www.w3.org/2003/g/data-view#',
'ma' => 'http://www.w3.org/ns/ma-ont#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfa' => 'http://www.w3.org/ns/rdfa#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'rif' => 'http://www.w3.org/2007/rif#',
'rr' => 'http://www.w3.org/ns/r2rml#',
'skos' => 'http://www.w3.org/2004/02/skos/core#',
'skosxl' => 'http://www.w3.org/2008/05/skos-xl#',
'wdr' => 'http://www.w3.org/2007/05/powder#',
'void' => 'http://rdfs.org/ns/void#',
'wdrs' => 'http://www.w3.org/2007/05/powder-s#',
'xhv' => 'http://www.w3.org/1999/xhtml/vocab#',
'xml' => 'http://www.w3.org/XML/1998/namespace',
'xsd' => 'http://www.w3.org/2001/XMLSchema#',
'prov' => 'http://www.w3.org/ns/prov#',
'sd' => 'http://www.w3.org/ns/sparql-service-description#',
'org' => 'http://www.w3.org/ns/org#',
'gldp' => 'http://www.w3.org/ns/people#',
'cnt' => 'http://www.w3.org/2008/content#',
'dcat' => 'http://www.w3.org/ns/dcat#',
'earl' => 'http://www.w3.org/ns/earl#',
'ht' => 'http://www.w3.org/2006/http#',
'ptr' => 'http://www.w3.org/2009/pointers#',
'cc' => 'http://creativecommons.org/ns#',
'ctag' => 'http://commontag.org/ns#',
'dc' => 'http://purl.org/dc/terms/',
'dc11' => 'http://purl.org/dc/elements/1.1/',
'dcterms' => 'http://purl.org/dc/terms/',
'foaf' => 'http://xmlns.com/foaf/0.1/',
'gr' => 'http://purl.org/goodrelations/v1#',
'ical' => 'http://www.w3.org/2002/12/cal/icaltzd#',
'og' => 'http://ogp.me/ns#',
'rev' => 'http://purl.org/stuff/rev#',
'sioc' => 'http://rdfs.org/sioc/ns#',
'v' => 'http://rdf.data-vocabulary.org/#',
'vcard' => 'http://www.w3.org/2006/vcard/ns#',
'schema' => 'http://schema.org/',
'describedby' => 'http://www.w3.org/2007/05/powder-s#describedby',
'license' => 'http://www.w3.org/1999/xhtml/vocab#license',
'role' => 'http://www.w3.org/1999/xhtml/vocab#role'
];

/**
* Registered vocabularies
*
* @var array
*/
protected $vocabs;

/**
* Context constructor
*/
public function __construct()
{
$this->vocabs = self::$defaultVocabs;
}

/**
* Register a vocabulary and its prefix
*
* @param string $prefix Vocabulary prefix
* @param string $uri Vocabulary URI
* @return Context New context
*
*/
public function registerVocabulary($prefix, $uri)
{
$prefix = self::validateVocabPrefix($prefix);
$uri = Vocabulary::validateVocabUri($uri);

// Register the new URI
if (empty($this->vocabs[$prefix]) || ($this->vocabs[$prefix] !== $uri)) {
$context = clone $this;
$context->vocabs[$prefix] = $uri;
return $context;
}

return $this;
}

/**
* Validata a vocabulary prefix
*
* @param string $prefix Vocabulary prefix
* @return string Valid vocabulary prefix
* @throws RuntimeException If the vocabulary prefix is invalid
*/
protected static function validateVocabPrefix($prefix)
{
$prefix = trim($prefix);

// If the vocabulary prefix is invalid
if (!strlen($prefix)) {
throw new RuntimeException(
sprintf(RuntimeException::INVALID_VOCABULARY_PREFIX_STR, $prefix),
RuntimeException::INVALID_VOCABULARY_PREFIX
);
}

return $prefix;
}

/**
* Return a particular vocabulary
*
* @param string $prefix Vocabulary Prefix
* @return VocabularyInterface Vocabulary
* @throws OutOfBoundsException If the prefix has not been registered
*/
public function getVocabulary($prefix)
{
$prefix = self::validateVocabPrefix($prefix);

// If the prefix has not been registered
if (empty($this->vocabs[$prefix])) {
throw new OutOfBoundsException(
sprintf(OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX_STR, $prefix),
OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX
);
}

return new Vocabulary($this->vocabs[$prefix]);
}

/**
* Return whether a particular vocabulary prefix has been registered
*
* @param string $prefix Vocabulary prefix
* @return bool Whether the prefix has been registered
*/
public function hasVocabulary($prefix)
{
return !empty($this->vocabs[self::validateVocabPrefix($prefix)]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************/

namespace Jkphl\Rdfalite\Tests;
namespace Jkphl\Rdfalite\Tests\Domain;

use Jkphl\Rdfalite\Domain\Thing\Thing;
use Jkphl\Rdfalite\Domain\Vocabulary\Vocabulary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************/

namespace Jkphl\Rdfalite\Tests;
namespace Jkphl\Rdfalite\Tests\Domain;

use Jkphl\Rdfalite\Domain\Vocabulary\Vocabulary;

Expand Down

0 comments on commit f45d0de

Please sign in to comment.