Skip to content

Commit

Permalink
v2.7.3 import
Browse files Browse the repository at this point in the history
  • Loading branch information
villermen committed Dec 11, 2017
0 parents commit 0d8093a
Show file tree
Hide file tree
Showing 31 changed files with 3,553 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2

jobs:
build:
docker:
- image: circleci/php:5.6

steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "composer.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run: composer install --no-interaction

- save_cache:
paths:
- vendor
key: v1-dependencies-{{ checksum "composer.json" }}

- run: vendor/bin/phpunit test/
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
composer.lock
/.idea/

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Villermen

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.
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "villermen/feed-processing",
"description": "Library for processing feeds in an efficient streaming way.",
"type": "library",
"license": "MIT",
"require": {
"php": ">=5.6",
"villermen/data-handling": "^1.9"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-4": {
"Villermen\\FeedProcessing\\": "src/"
}
}
}
98 changes: 98 additions & 0 deletions src/ActuallySimpleXmlElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Villermen\FeedProcessing;

/**
* Because face it, the one PHP provides is just nasty.
*/
class ActuallySimpleXmlElement implements \ArrayAccess
{
public $tagName;
public $attributes = [];
public $text = "";

/** @var ActuallySimpleXmlElement[] */
public $children = [];

/**
* @param \DOMNode $node
* @return ActuallySimpleXmlElement
*/
public static function fromDomNode(\DOMNode $node)
{
$element = new ActuallySimpleXmlElement();

$element->tagName = $node->nodeName;

/** @var \DOMAttr $attribute */
foreach ($node->attributes as $attribute) {
$element->attributes[$attribute->name] = $attribute->value;
}

/** @var \DOMNode $childNode */
foreach ($node->childNodes as $childNode) {
switch ($childNode->nodeType) {
case XML_ELEMENT_NODE:
$element->children[] = self::fromDomNode($childNode);
break;

case XML_TEXT_NODE:
$element->text = trim($childNode->textContent);
break;

case XML_CDATA_SECTION_NODE:
$element->text = $childNode->textContent;
break;
}
}

return $element;
}

/**
* @param string $tagName
* @return ActuallySimpleXmlElement[]
*/
public function get($tagName)
{
return array_filter($this->children, function($child) use ($tagName) {
/** @var ActuallySimpleXmlElement $child */
return $child->tagName === $tagName;
});
}

/**
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
{
return count($this->get($offset)) > 0;
}

/**
* @param string $offset
* @return ActuallySimpleXmlElement The first matched child.
* @throws \Exception When no direct children with the given tag name exist.
*/
public function offsetGet($offset)
{
$matchedChildren = $this->get($offset);

if (count($matchedChildren) === 0) {
throw new \Exception("No direct children with tag name \"{$offset}\" exist.");
}

return current($matchedChildren);
}

public function offsetSet($offset, $value)
{
throw new \Exception("Invalid operation.");
}

public function offsetUnset($offset)
{
throw new \Exception("Invalid operation.");
}
}

0 comments on commit 0d8093a

Please sign in to comment.