Skip to content

Commit 988a0b0

Browse files
committed
Merge branch 'release/2.0.4'
2 parents 8c55631 + acfb459 commit 988a0b0

14 files changed

+196
-169
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
vendor
22
composer.lock
33
phpunit.xml
4-
.idea
54
.phpunit.result.cache
5+
coverage

.php_cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ $finder = PhpCsFixer\Finder::create()
77
return PhpCsFixer\Config::create()
88
->setUsingCache(false)
99
->setRules(array(
10-
'@PSR2' => true,
11-
'binary_operator_spaces' => true,
12-
'no_whitespace_in_blank_line' => true,
13-
'ternary_operator_spaces' => true,
14-
'cast_spaces' => true,
15-
'trailing_comma_in_multiline_array' => true
10+
'@PhpCsFixer' => true,
1611
))
1712
->setFinder($finder);

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.0.4 - 2021/02/03
4+
- Review .php_cs settings, apply PHP CS Fixer
5+
- Change tests classes to final classes
6+
37
## 2.0.3 - 2021/02/01
48
- issue #3 - getAttributes returns an empty array
59

@@ -10,7 +14,7 @@
1014
- Minor readme and Travis CI updates
1115

1216
## 2.0.0 - 2021/01/26
13-
- use `splitbrain/phpfarm:jessie` as Docker image and fix docker image settings
17+
- Use `splitbrain/phpfarm:jessie` as Docker image and fix docker image settings
1418
- Code requires PHP >= 7.4
1519
- Code cleaning
1620
- Update README

src/AbstractAttributeHandler.php

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,42 @@
88

99
class AbstractAttributeHandler extends AbstractNodeHandler
1010
{
11-
const DEFAULT_VALUE_TYPE = 'string';
11+
public const DEFAULT_VALUE_TYPE = 'string';
1212

13-
const ATTRIBUTE_NAMESPACE = 'namespace';
13+
public const ATTRIBUTE_NAMESPACE = 'namespace';
1414

15-
const ATTRIBUTE_NAME = 'name';
15+
public const ATTRIBUTE_NAME = 'name';
1616

17-
const ATTRIBUTE_REF = 'ref';
17+
public const ATTRIBUTE_REF = 'ref';
1818

19-
const ATTRIBUTE_VALUE = 'value';
19+
public const ATTRIBUTE_VALUE = 'value';
2020

21-
const ATTRIBUTE_TYPE = 'type';
21+
public const ATTRIBUTE_TYPE = 'type';
2222

23-
const ATTRIBUTE_ABSTRACT = 'abstract';
23+
public const ATTRIBUTE_ABSTRACT = 'abstract';
2424

25-
const ATTRIBUTE_MAX_OCCURS = 'maxOccurs';
25+
public const ATTRIBUTE_MAX_OCCURS = 'maxOccurs';
2626

27-
const ATTRIBUTE_MIN_OCCURS = 'minOccurs';
27+
public const ATTRIBUTE_MIN_OCCURS = 'minOccurs';
2828

29-
const ATTRIBUTE_NILLABLE = 'nillable';
29+
public const ATTRIBUTE_NILLABLE = 'nillable';
3030

31-
const VALUE_UNBOUNDED = 'unbounded';
31+
public const VALUE_UNBOUNDED = 'unbounded';
3232

3333
/**
3434
* @deprecated
3535
*/
36-
const DEFAULT_OCCURENCE_VALUE = 1;
36+
public const DEFAULT_OCCURENCE_VALUE = 1;
3737

38-
const DEFAULT_OCCURRENCE_VALUE = 1;
38+
public const DEFAULT_OCCURRENCE_VALUE = 1;
3939

4040
public function getAttribute(): DOMAttr
4141
{
4242
return $this->getNode();
4343
}
44+
4445
/**
45-
* Tries to get attribute type on the same node in order to return the value of the attribute in its type
46-
* @return string|null
46+
* Tries to get attribute type on the same node in order to return the value of the attribute in its type.
4747
*/
4848
public function getType(): ?string
4949
{
@@ -58,10 +58,10 @@ public function getType(): ?string
5858
public function getValue(bool $withNamespace = false, bool $withinItsType = true, ?string $asType = self::DEFAULT_VALUE_TYPE)
5959
{
6060
$value = $this->getAttribute()->value;
61-
if ($withNamespace === false && !empty($value)) {
61+
if (false === $withNamespace && !empty($value)) {
6262
$value = implode('', array_slice(explode(':', $value), -1, 1));
6363
}
64-
if ($value !== null && $withinItsType === true) {
64+
if (null !== $value && true === $withinItsType) {
6565
$value = self::getValueWithinItsType($value, empty($asType) ? $this->getType() : $asType);
6666
}
6767

@@ -72,22 +72,24 @@ public function getValueNamespace(): ?string
7272
{
7373
$value = $this->getAttribute()->value;
7474
$namespace = null;
75-
if (strpos($value, ':') !== false) {
75+
if (false !== strpos($value, ':')) {
7676
$namespace = implode('', array_slice(explode(':', $value), 0, -1));
7777
}
7878

7979
return $namespace;
8080
}
8181

8282
/**
83-
* Returns the value with good type
84-
* @param mixed $value the value
85-
* @param string|null $knownType the value
83+
* Returns the value with good type.
84+
*
85+
* @param mixed $value the value
86+
* @param null|string $knownType the value
87+
*
8688
* @return mixed
8789
*/
8890
public static function getValueWithinItsType($value, ?string $knownType = null)
8991
{
90-
if (is_int($value) || (!is_null($value) && in_array($knownType, array(
92+
if (is_int($value) || (!is_null($value) && in_array($knownType, [
9193
'time',
9294
'positiveInteger',
9395
'unsignedLong',
@@ -96,19 +98,21 @@ public static function getValueWithinItsType($value, ?string $knownType = null)
9698
'long',
9799
'int',
98100
'integer',
99-
), true))) {
101+
], true))) {
100102
return intval($value);
101-
} elseif (is_float($value) || (!is_null($value) && in_array($knownType, array(
103+
}
104+
if (is_float($value) || (!is_null($value) && in_array($knownType, [
102105
'float',
103106
'double',
104107
'decimal',
105-
), true))) {
108+
], true))) {
106109
return floatval($value);
107-
} elseif (is_bool($value) || (!is_null($value) && in_array($knownType, array(
110+
}
111+
if (is_bool($value) || (!is_null($value) && in_array($knownType, [
108112
'bool',
109113
'boolean',
110-
), true))) {
111-
return ($value === 'true' || $value === true || $value === 1 || $value === '1');
114+
], true))) {
115+
return 'true' === $value || true === $value || 1 === $value || '1' === $value;
112116
}
113117

114118
return $value;

src/AbstractDomDocumentHandler.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,21 @@ public function __construct(DOMDocument $domDocument)
2323
$this->initRootElement();
2424
}
2525

26-
/**
27-
* Find valid root node (not a comment, at least a DOMElement node)
28-
* @throws InvalidArgumentException
29-
*/
30-
protected function initRootElement()
31-
{
32-
if ($this->domDocument->hasChildNodes()) {
33-
foreach ($this->domDocument->childNodes as $node) {
34-
if ($node instanceof DOMElement) {
35-
$this->rootElement = $this->getElementHandler($node, $this);
36-
break;
37-
}
38-
}
39-
} else {
40-
throw new InvalidArgumentException('Document seems to be invalid', __LINE__);
41-
}
42-
}
43-
4426
public function getHandler($node, int $index = -1): AbstractNodeHandler
4527
{
4628
if ($node instanceof DOMElement) {
4729
return $this->getElementHandler($node, $this, $index);
48-
} elseif ($node instanceof DOMAttr) {
30+
}
31+
if ($node instanceof DOMAttr) {
4932
return $this->getAttributeHandler($node, $this, $index);
50-
} elseif ($node instanceof DOMNameSpaceNode) {
33+
}
34+
if ($node instanceof DOMNameSpaceNode) {
5135
return new NameSpaceHandler($node, $this, $index);
5236
}
5337

5438
return $this->getNodeHandler($node, $this, $index);
5539
}
5640

57-
abstract protected function getNodeHandler(DOMNode $node, AbstractDomDocumentHandler $domDocument, int $index = -1): NodeHandler;
58-
59-
abstract protected function getElementHandler(DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1): ElementHandler;
60-
61-
abstract protected function getAttributeHandler(DOMAttr $attribute, AbstractDomDocumentHandler $domDocument, int $index = -1): AttributeHandler;
62-
6341
public function getNodeByName(string $name): ?NodeHandler
6442
{
6543
return $this->domDocument->getElementsByTagName($name)->length > 0 ? $this->getNodeHandler($this->domDocument->getElementsByTagName($name)->item(0), $this) : null;
@@ -77,7 +55,7 @@ public function getElementByName(string $name): ?ElementHandler
7755

7856
public function getNodesByName(string $name, ?string $checkInstance = null): array
7957
{
80-
$nodes = array();
58+
$nodes = [];
8159
if ($this->domDocument->getElementsByTagName($name)->length > 0) {
8260
foreach ($this->domDocument->getElementsByTagName($name) as $node) {
8361
if (is_null($checkInstance) || $node instanceof $checkInstance) {
@@ -110,13 +88,13 @@ public function getElementsByNameAndAttributes(string $name, array $attributes,
11088

11189
public function getElementsHandlers(DOMNodeList $nodeList): array
11290
{
113-
$nodes = array();
91+
$nodes = [];
11492
if (!empty($nodeList)) {
11593
$index = 0;
11694
foreach ($nodeList as $node) {
11795
if ($node instanceof DOMElement) {
11896
$nodes[] = $this->getElementHandler($node, $this, $index);
119-
$index++;
97+
++$index;
12098
}
12199
}
122100
}
@@ -129,7 +107,7 @@ public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $nod
129107
$xpath = new DOMXPath($node ? $node->ownerDocument : $this->domDocument);
130108
$xQuery = sprintf("%s//*[local-name()='%s']", $node instanceof DOMNode ? '.' : '', $name);
131109
foreach ($attributes as $attributeName => $attributeValue) {
132-
if (strpos($attributeValue, '*') !== false) {
110+
if (false !== strpos($attributeValue, '*')) {
133111
$xQuery .= sprintf("[contains(@%s, '%s')]", $attributeName, str_replace('*', '', $attributeValue));
134112
} else {
135113
$xQuery .= sprintf("[@%s='%s']", $attributeName, $attributeValue);
@@ -145,4 +123,30 @@ public function getElementByNameAndAttributes(string $name, array $attributes):
145123

146124
return array_shift($elements);
147125
}
126+
127+
/**
128+
* Find valid root node (not a comment, at least a DOMElement node).
129+
*
130+
* @throws InvalidArgumentException
131+
*/
132+
protected function initRootElement()
133+
{
134+
if ($this->domDocument->hasChildNodes()) {
135+
foreach ($this->domDocument->childNodes as $node) {
136+
if ($node instanceof DOMElement) {
137+
$this->rootElement = $this->getElementHandler($node, $this);
138+
139+
break;
140+
}
141+
}
142+
} else {
143+
throw new InvalidArgumentException('Document seems to be invalid', __LINE__);
144+
}
145+
}
146+
147+
abstract protected function getNodeHandler(DOMNode $node, AbstractDomDocumentHandler $domDocument, int $index = -1): NodeHandler;
148+
149+
abstract protected function getElementHandler(DOMElement $element, AbstractDomDocumentHandler $domDocument, int $index = -1): ElementHandler;
150+
151+
abstract protected function getAttributeHandler(DOMAttr $attribute, AbstractDomDocumentHandler $domDocument, int $index = -1): AttributeHandler;
148152
}

src/AbstractElementHandler.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getAttributeValue(string $name, bool $withNamespace = false, boo
4141

4242
public function getChildrenByName(string $name): array
4343
{
44-
$children = array();
44+
$children = [];
4545
if ($this->hasChildren()) {
4646
foreach ($this->getElement()->getElementsByTagName($name) as $index => $node) {
4747
$children[] = $this->getDomDocumentHandler()->getHandler($node, $index);
@@ -53,7 +53,7 @@ public function getChildrenByName(string $name): array
5353

5454
public function getElementChildren(): array
5555
{
56-
$children = array();
56+
$children = [];
5757
if ($this->hasChildren()) {
5858
$children = $this->getDomDocumentHandler()->getElementsHandlers($this->getChildNodes());
5959
}
@@ -74,7 +74,8 @@ public function getChildByNameAndAttributes(string $name, array $attributes): ?E
7474
}
7575

7676
/**
77-
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
77+
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
78+
*
7879
* @return mixed
7980
*/
8081
public function getMaxOccurs()
@@ -91,7 +92,8 @@ public function getMaxOccurs()
9192
}
9293

9394
/**
94-
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
95+
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
96+
*
9597
* @return int
9698
*/
9799
public function getMinOccurs()
@@ -105,53 +107,45 @@ public function getMinOccurs()
105107
}
106108

107109
/**
108-
* Info at {@link http://www.w3schools.com/xml/el_element.asp}
109-
* @return bool
110+
* Info at {@link http://www.w3schools.com/xml/el_element.asp}.
110111
*/
111112
public function getNillable(): bool
112113
{
113114
return (bool) $this->getAttributeValue(AbstractAttributeHandler::ATTRIBUTE_NILLABLE, false, true, 'bool');
114115
}
115116

116117
/**
117-
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
118-
* @return bool
118+
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
119119
*/
120120
public function canOccurSeveralTimes(): bool
121121
{
122122
return (1 < $this->getMinOccurs()) || (1 < $this->getMaxOccurs()) || (AbstractAttributeHandler::VALUE_UNBOUNDED === $this->getMaxOccurs());
123123
}
124124

125125
/**
126-
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
127-
* @return bool
126+
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
128127
*/
129128
public function canOccurOnlyOnce(): bool
130129
{
131130
return 1 === $this->getMaxOccurs();
132131
}
133132

134133
/**
135-
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
136-
* @return bool
134+
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
137135
*/
138136
public function isOptional(): bool
139137
{
140138
return 0 === $this->getMinOccurs();
141139
}
142140

143141
/**
144-
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}
145-
* @return bool
142+
* Info at {@link https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints}.
146143
*/
147144
public function isRequired(): bool
148145
{
149146
return 1 <= $this->getMinOccurs();
150147
}
151148

152-
/**
153-
* @return bool
154-
*/
155149
public function isRemovable(): bool
156150
{
157151
return $this->isOptional() && $this->getNillable();

0 commit comments

Comments
 (0)