Skip to content

Commit

Permalink
Improved code climate
Browse files Browse the repository at this point in the history
  • Loading branch information
jkphl committed Feb 14, 2017
1 parent 199ae8f commit 40be2fd
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Rdfalite/Application/Parser/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ public function addChild(ThingInterface $thing)
{
if ($this->parentThing instanceof ThingInterface) {
$this->parentThing->addChild($thing);
} else {
$this->children[spl_object_hash($thing)] = $thing;
return $this;
}

$this->children[spl_object_hash($thing)] = $thing;
return $this;
}
}
24 changes: 1 addition & 23 deletions src/Rdfalite/Domain/Property/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

namespace Jkphl\Rdfalite\Domain\Property;

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

/**
Expand Down Expand Up @@ -75,32 +74,11 @@ class Property implements PropertyInterface
*/
public function __construct($name, VocabularyInterface $vocabulary, $value)
{
$this->name = self::validatePropertyName($name);
$this->name = (new PropertyService())->validatePropertyName($name);
$this->vocabulary = $vocabulary;
$this->value = $value;
}

/**
* Validate a property name
*
* @param string $name Property name
* @return string Sanitized property name
* @throws RuntimeException If the property name is invalid
*/
public static function validatePropertyName($name)
{
$name = trim($name);

// If the property name is invalid
if (!strlen($name) || !preg_match('/^[a-z][a-zA-Z0-9]*$/', $name)) {
throw new RuntimeException(
sprintf(RuntimeException::INVALID_PROPERTY_NAME_STR, $name), RuntimeException::INVALID_PROPERTY_NAME
);
}

return $name;
}

/**
* Return the property name
*
Expand Down
69 changes: 69 additions & 0 deletions src/Rdfalite/Domain/Property/PropertyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* rdfa-lite
*
* @category Jkphl
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Domain
* @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\Domain\Property;

use Jkphl\Rdfalite\Domain\Exceptions\RuntimeException;

/**
* Property service
*
* @package Jkphl\Rdfalite
* @subpackage Jkphl\Rdfalite\Domain
*/
class PropertyService
{
/**
* Validate a property name
*
* @param string $name Property name
* @return string Sanitized property name
* @throws RuntimeException If the property name is invalid
*/
public function validatePropertyName($name)
{
$name = trim($name);

// If the property name is invalid
if (!strlen($name) || !preg_match('/^[a-z][a-zA-Z0-9]*$/', $name)) {
throw new RuntimeException(
sprintf(RuntimeException::INVALID_PROPERTY_NAME_STR, $name), RuntimeException::INVALID_PROPERTY_NAME
);
}

return $name;
}
}
4 changes: 2 additions & 2 deletions src/Rdfalite/Domain/Thing/Thing.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

use Jkphl\Rdfalite\Domain\Exceptions\OutOfBoundsException;
use Jkphl\Rdfalite\Domain\Exceptions\RuntimeException;
use Jkphl\Rdfalite\Domain\Property\Property;
use Jkphl\Rdfalite\Domain\Property\PropertyInterface;
use Jkphl\Rdfalite\Domain\Property\PropertyService;
use Jkphl\Rdfalite\Domain\Vocabulary\VocabularyInterface;

/**
Expand Down Expand Up @@ -170,7 +170,7 @@ public function getProperties()
*/
public function getProperty($name)
{
$name = Property::validatePropertyName($name);
$name = (new PropertyService())->validatePropertyName($name);

// If the property name is unknown
if (!array_key_exists($name, $this->properties)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Rdfalite/Domain/Thing/ThingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public function addProperty(PropertyInterface $property);
/**
* Return all properties
*
* @return PropertyInterface[] Properties
* @return array[] Properties
*/
public function getProperties();

/**
* Return the values of a single property
*
* @param string $name Property name
* @return PropertyInterface Property
* @return array Property
*/
public function getProperty($name);

Expand Down
11 changes: 3 additions & 8 deletions src/Rdfalite/Infrastructure/Parser/RdfaliteElementProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ protected function processProperty(\DOMElement $element, Context $context)
{
if ($element->hasAttribute('property')) {
$property = explode(':', $element->getAttribute('property'));
$name = array_pop($property);
$prefix = array_pop($property);
$name = strval(array_pop($property));
$prefix = strval(array_pop($property));

// Determine the vocabulary to use
$vocabulary = empty($prefix) ? $context->getDefaultVocabulary() : $context->getVocabulary($prefix);
Expand Down Expand Up @@ -159,7 +159,6 @@ protected function getPropertyValue(\DOMElement $element, Context $context)
switch (strtoupper($element->tagName)) {
case 'META':
return $element->getAttribute('content');
break;
case 'AUDIO':
case 'EMBED':
case 'IFRAME':
Expand All @@ -168,23 +167,20 @@ protected function getPropertyValue(\DOMElement $element, Context $context)
case 'TRACK':
case 'VIDEO':
return $element->getAttribute('src');
break;
case 'A':
case 'AREA':
case 'LINK':
return $element->getAttribute('href');
break;
case 'OBJECT':
return $element->getAttribute('data');
break;
case 'DATA':
return $element->getAttribute('value');
break;
case 'TIME':
$datetime = $element->getAttribute('datetime');
if (!empty($datetime)) {
return $datetime;
}
// fall through
default:
// trigger_error(sprintf('RDFa Lite 1.1 element processor: Unhandled tag name "%s"', $element->tagName), E_USER_WARNING);
return $element->textContent;
Expand All @@ -209,7 +205,6 @@ protected function getThing($typeof, Context $context)
// Determine the vocabulary to use
$vocabulary = empty($prefix) ? $context->getDefaultVocabulary() : $context->getVocabulary($prefix);
if ($vocabulary instanceof VocabularyInterface) {

// Return a new thing
return new Thing($type, $vocabulary);
}
Expand Down

0 comments on commit 40be2fd

Please sign in to comment.