Skip to content

Commit

Permalink
Fix getAttribute/setAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed Jul 12, 2016
1 parent 2190c80 commit f619eee
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 57 deletions.
12 changes: 6 additions & 6 deletions src/Element/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getTerm()
return $this->getCachedProperty(
'term',
function () {
$value = $this->getAttribute('atom:term');
$value = $this->getAttribute('term');
if (null === $value) {
throw new MalformedNodeException('There is no attribute "term"');
}
Expand All @@ -74,7 +74,7 @@ function () {
*/
public function setTerm($value)
{
$this->setAttribute('atom:term', $value);
$this->setAttribute('term', $value);
$this->setCachedProperty('term', $value);

return $this;
Expand All @@ -95,7 +95,7 @@ public function getLabel()
return $this->getCachedProperty(
'label',
function () {
return $this->getAttribute('atom:label');
return $this->getAttribute('label');
}
);
}
Expand All @@ -114,7 +114,7 @@ function () {
*/
public function setLabel($value)
{
$this->setAttribute('atom:label', $value);
$this->setAttribute('label', $value);
$this->setCachedProperty('label', $value);

return $this;
Expand All @@ -135,7 +135,7 @@ public function getScheme()
return $this->getCachedProperty(
'scheme',
function () {
return $this->getAttribute('atom:scheme');
return $this->getAttribute('scheme');
}
);
}
Expand All @@ -154,7 +154,7 @@ function () {
*/
public function setScheme($value)
{
$this->setAttribute('atom:scheme', $value);
$this->setAttribute('scheme', $value);
$this->setCachedProperty('scheme', $value);

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Element/Construct/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getType()
return $this->getCachedProperty(
'type',
function () {
return $this->getAttribute('atom:type') ?: 'text';
return $this->getAttribute('type') ?: 'text';
}
);
}
Expand All @@ -100,7 +100,7 @@ function () {
*/
public function setType($type)
{
$this->setAttribute('atom:type', $type);
$this->setAttribute('type', $type);
$this->setCachedProperty('type', $type);

return $this;
Expand Down
8 changes: 4 additions & 4 deletions src/Element/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getType()
return $this->getCachedProperty(
'type',
function () {
return $this->getAttribute('atom:type') ?: 'text';
return $this->getAttribute('type') ?: 'text';
}
);
}
Expand All @@ -153,7 +153,7 @@ function () {
*/
public function setType($type)
{
$this->setAttribute('atom:type', $type);
$this->setAttribute('type', $type);
$this->setCachedProperty('type', $type);

return $this;
Expand All @@ -174,7 +174,7 @@ public function getSrc()
return $this->getCachedProperty(
'src',
function () {
return $this->getAttribute('atom:src');
return $this->getAttribute('src');
}
);
}
Expand All @@ -191,7 +191,7 @@ function () {
*/
public function setSrc($iri)
{
$this->setAttribute('atom:src', $iri);
$this->setAttribute('src', $iri);
$this->setCachedProperty('src', $iri);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Element/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getUri()
return $this->getCachedProperty(
'href',
function () {
return $this->getAttribute('atom:href');
return $this->getAttribute('href');
}
);
}
Expand All @@ -90,7 +90,7 @@ function () {
*/
public function setUri($iri)
{
$this->setAttribute('atom:href', $iri);
$this->setAttribute('href', $iri);
$this->setCachedProperty('href', $iri);

return $this;
Expand All @@ -110,7 +110,7 @@ public function getVersion()
return $this->getCachedProperty(
'version',
function () {
return $this->getAttribute('atom:version');
return $this->getAttribute('version');
}
);
}
Expand All @@ -128,7 +128,7 @@ function () {
*/
public function setVersion($version)
{
$this->setAttribute('atom:version', $version);
$this->setAttribute('version', $version);
$this->setCachedProperty('version', $version);

return $this;
Expand Down
24 changes: 12 additions & 12 deletions src/Element/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getUri()
return $this->getCachedProperty(
'href',
function () {
$value = $this->getAttribute('atom:href');
$value = $this->getAttribute('href');
if (null === $value) {
throw new MalformedNodeException('There is no attribute "href"');
}
Expand All @@ -74,7 +74,7 @@ function () {
*/
public function setUri($uri)
{
$this->setAttribute('atom:href', $uri);
$this->setAttribute('href', $uri);
$this->setCachedProperty('href', $uri);

return $this;
Expand All @@ -95,7 +95,7 @@ public function getRelation()
return $this->getCachedProperty(
'rel',
function () {
return $this->getAttribute('atom:rel') ?: 'alternate';
return $this->getAttribute('rel') ?: 'alternate';
}
);
}
Expand All @@ -114,7 +114,7 @@ function () {
*/
public function setRelation($type)
{
$this->setAttribute('atom:rel', $type);
$this->setAttribute('rel', $type);
$this->setCachedProperty('rel', $type);

return $this;
Expand All @@ -135,7 +135,7 @@ public function getType()
return $this->getCachedProperty(
'type',
function () {
return $this->getAttribute('atom:type');
return $this->getAttribute('type');
}
);
}
Expand All @@ -154,7 +154,7 @@ function () {
*/
public function setType($type)
{
$this->setAttribute('atom:type', $type);
$this->setAttribute('type', $type);
$this->setCachedProperty('type', $type);

return $this;
Expand All @@ -175,7 +175,7 @@ public function getLanguage()
return $this->getCachedProperty(
'hreflang',
function () {
return $this->getAttribute('atom:hreflang');
return $this->getAttribute('hreflang');
}
);
}
Expand All @@ -194,7 +194,7 @@ function () {
*/
public function setLanguage($language)
{
$this->setAttribute('atom:hreflang', $language);
$this->setAttribute('hreflang', $language);
$this->setCachedProperty('hreflang', $language);

return $this;
Expand All @@ -215,7 +215,7 @@ public function getTitle()
return $this->getCachedProperty(
'title',
function () {
return $this->getAttribute('atom:title');
return $this->getAttribute('title');
}
);
}
Expand All @@ -234,7 +234,7 @@ function () {
*/
public function setTitle($title)
{
$this->setAttribute('atom:title', $title);
$this->setAttribute('title', $title);
$this->setCachedProperty('title', $title);

return $this;
Expand All @@ -255,7 +255,7 @@ public function getLength()
return $this->getCachedProperty(
'length',
function () {
return ((int) $this->getAttribute('atom:length')) ?: null;
return ((int) $this->getAttribute('length')) ?: null;
}
);
}
Expand All @@ -274,7 +274,7 @@ function () {
*/
public function setLength($length)
{
$this->setAttribute('atom:length', $length);
$this->setAttribute('length', $length);
$this->setCachedProperty('length', $length);

return $this;
Expand Down
41 changes: 18 additions & 23 deletions src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,26 @@ public function query($xpath, $flags = 0)
* Extension\NamespaceExtension}. This library registers only "atom:" prefix, but this list
* can be expanded by extensions.
*
* @param string $prefixedName Prefixed attribute name.
* @param string $attrName Attribute name (e. g. "type", "atom:foo").
*
* @return string|null
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function getAttribute($prefixedName)
public function getAttribute($attrName)
{
list($prefix, $name) = explode(':', $prefixedName);
$namespace = $this->getNamespace($prefix);

$element = $this->getDomElement();
$prefix = $element->lookupPrefix($namespace);
if (null === $prefix) {
if (!$element->hasAttribute($name)) {
return null;
}
return $element->getAttribute($name);
if (strpos($attrName, ':') === false) {
return $element->hasAttribute($attrName) ? $element->getAttribute($attrName) : null;
} else {
if (!$element->hasAttributeNS($namespace, $name)) {
return null;
}
return $element->getAttributeNS($namespace, $name);
list($prefix, $name) = explode(':', $attrName);
$namespace = $this->getNamespace($prefix);

return $element->hasAttributeNS($namespace, $name)
? $element->getAttributeNS($namespace, $name)
: null;
}
}

Expand All @@ -156,23 +151,23 @@ public function getAttribute($prefixedName)
* Extension\NamespaceExtension}. This library registers only "atom:" prefix, but this list
* can be expanded by extensions.
*
* @param string $prefixedName Prefixed attribute name.
* @param string $attrName Attribute name (e. g. "type", "atom:foo").
* @param string $value
*
* @throws \InvalidArgumentException
*
* @since 1.0
*/
public function setAttribute($prefixedName, $value)
public function setAttribute($attrName, $value)
{
list($prefix, $name) = explode(':', $prefixedName);
$namespace = $this->getNamespace($prefix);
$element = $this->getDomElement();
$prefix = $element->lookupPrefix($namespace);
if (null === $prefix) {
$element->setAttribute($name, $value);
if (strpos($attrName, ':') === false) {
$element->setAttribute($attrName, $value);
} else {
$element->setAttributeNS($namespace, $prefix . ':' . $name, $value);
list($prefix, $name) = explode(':', $attrName);
$namespace = $this->getNamespace($prefix);

$element->setAttributeNS($namespace, $name, $value);
}
}

Expand Down
14 changes: 8 additions & 6 deletions tests/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,17 @@ public function testGetAttribute()
->willReturn($parent->getExtensions());
/** @var Node $node */

static::assertEquals('baz', $node->getAttribute('atom:bar'));
static::assertNull($node->getAttribute('atom:foo'));
static::assertEquals('baz', $node->getAttribute('bar'));
static::assertNull($node->getAttribute('foo'));
static::assertNull($node->getAttribute('atom:bar'));
}

/**
*
*/
public function testGetAttributeCustomPrefix()
{
$document = $this->createDocument('<a:foo a:bar="baz"/>', 'a:doc');
$document = $this->createDocument('<a:foo a:a1="bar" a2="baz"/>', 'a:doc');
$parent = $this->createFakeNode($document);
$node = $this->getMockForAbstractClass(
Node::class,
Expand All @@ -151,8 +152,9 @@ public function testGetAttributeCustomPrefix()
->willReturn($parent->getExtensions());
/** @var Node $node */

static::assertEquals('baz', $node->getAttribute('atom:bar'));
static::assertNull($node->getAttribute('atom:foo'));
static::assertEquals('bar', $node->getAttribute('atom:a1'));
static::assertEquals('baz', $node->getAttribute('a2'));
static::assertNull($node->getAttribute('atom:a2'));
}

/**
Expand All @@ -162,7 +164,7 @@ public function testSetAttribute()
{
$document = $this->createDocument();
$node = $this->createInstance($document);
$node->setAttribute('atom:bar', 'baz');
$node->setAttribute('bar', 'baz');
static::assertEquals(
'<doc xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml" ' .
'bar="baz"/>',
Expand Down

0 comments on commit f619eee

Please sign in to comment.