Skip to content

Commit

Permalink
Added microformats parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jkphl committed Mar 11, 2017
1 parent dad8a3f commit 5e546db
Show file tree
Hide file tree
Showing 14 changed files with 538 additions and 40 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@
"require-dev": {
"clue/graph-composer": "dev-master",
"codeclimate/php-test-reporter": "^0.4.4",
"microformats/test": "@dev",
"mindplay/composer-locator": "^2.1",
"phpunit/phpunit": "^4.8",
"satooshi/php-coveralls": "^1.0",
"squizlabs/php_codesniffer": "^2.8",
"microformats/test": "@dev"
"squizlabs/php_codesniffer": "^2.8"
}
}
30 changes: 23 additions & 7 deletions src/Micrometa/Application/Factory/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

use Jkphl\Micrometa\Application\Exceptions\InvalidArgumentException;
use Jkphl\Micrometa\Application\Item\Item;
use Jkphl\Micrometa\Application\Value\AlternateValues;
use Jkphl\Micrometa\Application\Value\StringValue;
use Jkphl\Micrometa\Domain\Value\ValueInterface;

/**
* Item factory
Expand All @@ -64,6 +67,23 @@ public function __construct($format)
$this->format = $format;
}

/**
* Prepare a single property value
*
* @param mixed $propertyValue Property Value
* @return ValueInterface Value
*/
protected function processPropertyValue($propertyValue)
{
if (is_object($propertyValue)) {
return $this->__invoke($propertyValue);
}
if (is_array($propertyValue)) {
return new AlternateValues($propertyValue);
}
return new StringValue($propertyValue);
}

/**
* Create an item instance
*
Expand All @@ -74,8 +94,9 @@ public function __invoke(\stdClass $item)
{
$type = isset($item->type) ? $item->type : null;
$itemId = isset($item->id) ? $item->id : null;
$value = isset($item->value) ? $item->value : null;
$properties = $this->getProperties($item);
return new Item($this->format, $type, $properties, $itemId);
return new Item($this->format, $type, $properties, $itemId, $value);
}

/**
Expand Down Expand Up @@ -130,11 +151,6 @@ protected function getPropertyValues($propertyValues)
);
}

return array_map(
function ($propertyValue) {
return is_object($propertyValue) ? $this->__invoke($propertyValue) : $propertyValue;
},
$propertyValues
);
return array_map([$this, 'processPropertyValue'], $propertyValues);
}
}
20 changes: 19 additions & 1 deletion src/Micrometa/Application/Item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class Item extends \Jkphl\Micrometa\Domain\Item\Item implements ItemInterface
* @var int
*/
protected $format;
/**
* Item value
*
* @var string
*/
protected $value;

/**
* Item constructor
Expand All @@ -58,10 +64,12 @@ class Item extends \Jkphl\Micrometa\Domain\Item\Item implements ItemInterface
* @param string|array $type Item type(s)
* @param array[] $properties Item properties
* @param string|null $itemId Item id
* @param string|null $value Item value
*/
public function __construct($format, $type, array $properties = [], $itemId = null)
public function __construct($format, $type, array $properties = [], $itemId = null, $value = null)
{
$this->format = $format;
$this->value = $value;
parent::__construct($type, $properties, $itemId);
}

Expand All @@ -74,4 +82,14 @@ public function getFormat()
{
return $this->format;
}

/**
* Return the item value
*
* @return string Item value
*/
public function getValue()
{
return $this->value;
}
}
7 changes: 7 additions & 0 deletions src/Micrometa/Application/Item/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ interface ItemInterface extends \Jkphl\Micrometa\Domain\Item\ItemInterface
* @return int Parser format
*/
public function getFormat();

/**
* Return the item value
*
* @return string Item value
*/
public function getValue();
}
58 changes: 58 additions & 0 deletions src/Micrometa/Application/Value/AlternateValues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* micrometa
*
* @category Jkphl
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Application
* @author Joschi Kuphal <joschi@kuphal.net> / @jkphl
* @copyright Copyright © 2017 Joschi Kuphal <joschi@kuphal.net> / @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\Micrometa\Application\Value;

use Jkphl\Micrometa\Domain\Value\ValueInterface;

/**
* Alternate value
*
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Application
*/
class AlternateValues extends \ArrayObject implements ValueInterface
{
/**
* Return whether the value should be considered empty
*
* @return boolean Value is empty
*/
public function isEmpty()
{
return !count($this->getArrayCopy());
}
}
75 changes: 75 additions & 0 deletions src/Micrometa/Application/Value/StringValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* micrometa
*
* @category Jkphl
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Application\Value
* @author Joschi Kuphal <joschi@kuphal.net> / @jkphl
* @copyright Copyright © 2017 Joschi Kuphal <joschi@kuphal.net> / @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\Micrometa\Application\Value;

use Jkphl\Micrometa\Domain\Value\ValueInterface;

/**
* String value
*
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Application
*/
class StringValue implements ValueInterface
{
/**
* String value
*
* @var string
*/
protected $value;

/**
* String value constructor
*
* @param string $value String value
*/
public function __construct($value)
{
$this->value = $value;
}

/**
* Return whether the value should be considered empty
*
* @return boolean Value is empty
*/
public function isEmpty()
{
return !strlen(trim($this->value));
}
}
27 changes: 16 additions & 11 deletions src/Micrometa/Domain/Item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

use Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException;
use Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException;
use Jkphl\Micrometa\Domain\Value\ValueInterface;

/**
* Micro information item
Expand Down Expand Up @@ -149,25 +150,19 @@ protected function validatePropertyValues(array $values)
$nonEmptyPropertyValues = [];

// Run through all property values
/** @var ValueInterface $value */
foreach ($values as $value) {
// If it's a string value
if (is_string($value)) {
$nonEmptyValue = trim($value);
if (strlen($nonEmptyValue)) {
$nonEmptyPropertyValues[] = $nonEmptyValue;
}
continue;
}

// If the value is not a nested item
if (!($value instanceof ItemInterface)) {
if (!($value instanceof ValueInterface)) {
throw new InvalidArgumentException(
sprintf(InvalidArgumentException::INVALID_PROPERTY_VALUE_STR, gettype($value)),
InvalidArgumentException::INVALID_PROPERTY_VALUE
);
}

$nonEmptyPropertyValues[] = $value;
if (!$value->isEmpty()) {
$nonEmptyPropertyValues[] = $value;
}
}

return $nonEmptyPropertyValues;
Expand Down Expand Up @@ -222,4 +217,14 @@ public function getProperty($name)

return $this->properties[$name];
}

/**
* Return whether the value should be considered empty
*
* @return boolean Value is empty
*/
public function isEmpty()
{
return false;
}
}
4 changes: 3 additions & 1 deletion src/Micrometa/Domain/Item/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@

namespace Jkphl\Micrometa\Domain\Item;

use Jkphl\Micrometa\Domain\Value\ValueInterface;

/**
* Item interface
*
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Domain
*/
interface ItemInterface
interface ItemInterface extends ValueInterface
{
/**
* Return the item types
Expand Down
53 changes: 53 additions & 0 deletions src/Micrometa/Domain/Value/ValueInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* micrometa
*
* @category Jkphl
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Domain
* @author Joschi Kuphal <joschi@kuphal.net> / @jkphl
* @copyright Copyright © 2017 Joschi Kuphal <joschi@kuphal.net> / @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\Micrometa\Domain\Value;

/**
* Value interface
*
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Domain
*/
interface ValueInterface
{
/**
* Return whether the value should be considered empty
*
* @return boolean Value is empty
*/
public function isEmpty();
}

0 comments on commit 5e546db

Please sign in to comment.