Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

HTML_Common2 now implements ArrayAccess (see request #15618)

Prepare release 2.1.0

git-svn-id: http://svn.php.net/repository/pear/packages/HTML_Common2/trunk@324960 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
commit 6fae3d3e2a0292da59ad61a815568a51f9ccaf5e 1 parent a8d921d
@sad-spirit sad-spirit authored
View
63 HTML/Common2.php
@@ -56,7 +56,7 @@
* @version Release: @package_version@
* @link http://pear.php.net/package/HTML_Common2
*/
-abstract class HTML_Common2
+abstract class HTML_Common2 implements ArrayAccess
{
/**
* Associative array of attributes
@@ -504,5 +504,66 @@ public function removeClass($class)
protected function onAttributeChange($name, $value = null)
{
}
+
+ /**
+ * Whether or not an offset (HTML attribute) exists
+ *
+ * @param string $offset An offset to check for.
+ *
+ * @return boolean Returns true on success or false on failure.
+ * @link http://php.net/manual/en/arrayaccess.offsetexists.php
+ */
+ public function offsetExists($offset)
+ {
+ return isset($this->attributes[strtolower($offset)]);
+ }
+
+ /**
+ * Returns the value at specified offset (i.e. attribute name)
+ *
+ * @param string $offset The offset to retrieve.
+ *
+ * @return string|null
+ * @link http://php.net/manual/en/arrayaccess.offsetget.php
+ * @see getAttribute()
+ */
+ public function offsetGet($offset)
+ {
+ return $this->getAttribute($offset);
+ }
+
+ /**
+ * Assigns a value to the specified offset (i.e. attribute name)
+ *
+ * @param string $offset The offset to assign the value to
+ * @param string $value The value to set
+ *
+ * @return void
+ * @link http://php.net/manual/en/arrayaccess.offsetset.php
+ * @see setAttribute()
+ */
+ public function offsetSet($offset, $value)
+ {
+ if (null !== $offset) {
+ $this->setAttribute($offset, $value);
+ } else {
+ // handles $foo[] = 'disabled';
+ $this->setAttribute($value);
+ }
+ }
+
+ /**
+ * Unsets an offset (i.e. removes an attribute)
+ *
+ * @param string $offset The offset to unset
+ *
+ * @return void
+ * @link http://php.net/manual/en/arrayaccess.offsetunset.php
+ * @see removeAttribute
+ */
+ public function offsetUnset($offset)
+ {
+ $this->removeAttribute($offset);
+ }
}
?>
View
48 package2.xml
@@ -27,10 +27,10 @@ HTML_Common2 package provides methods for HTML code display and attributes handl
<email>thesaur@php.net</email>
<active>no</active>
</lead>
- <date>2010-10-20</date>
+ <date>2012-04-08</date>
<version>
- <release>2.0.0</release>
- <api>2.0.0</api>
+ <release>2.1.0</release>
+ <api>2.1.0</api>
</version>
<stability>
<release>stable</release>
@@ -38,20 +38,20 @@ HTML_Common2 package provides methods for HTML code display and attributes handl
</stability>
<license uri="http://opensource.org/licenses/bsd-license.php">BSD License</license>
<notes>
- * Due to addition of end-user docs to PEAR manual, the package is now
- declared stable
- * Added array type hinting to removeAttributeArray() and getAttributesString()
- methods
- * Packaging fix: tests now install without redundant subdirectory, to
- @test_dir@/HTML_Common2 rather than to @test_dir@/HTML_Common2/tests
+ * HTML_Common2 now implements ArrayAccess, allowing more intuitive attribute
+ handling similar to that of SimpleXML extension
+ * Unit tests properly run under recent PHPUnit versions and from SVN checkout
+ * Coding standards fixes
</notes>
<contents>
<dir name="/">
<file role="php" name="HTML/Common2.php" baseinstalldir="/">
- <tasks:replace from="@package_version@" to="version" type="package-info" />
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
<dir name="tests">
- <file role="test" name="HTML_Common2_Test.php" />
+ <file role="test" name="HTML_Common2_Test.php">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
<file role="test" name="AllTests.php" />
</dir>
</dir>
@@ -73,6 +73,26 @@ HTML_Common2 package provides methods for HTML code display and attributes handl
</filelist>
</phprelease>
<changelog>
+ <release>
+ <date>2010-10-20</date>
+ <version>
+ <release>2.0.0</release>
+ <api>2.0.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <license uri="http://opensource.org/licenses/bsd-license.php">BSD License</license>
+ <notes>
+ * Due to addition of end-user docs to PEAR manual, the package is now
+ declared stable
+ * Added array type hinting to removeAttributeArray() and getAttributesString()
+ methods
+ * Packaging fix: tests now install without redundant subdirectory, to
+ @test_dir@/HTML_Common2 rather than to @test_dir@/HTML_Common2/tests
+ </notes>
+ </release>
<release>
<version>
@@ -112,7 +132,7 @@ HTML_Common2 package provides methods for HTML code display and attributes handl
removeAttribute(), setAttribute(), setAttributes(), setComment() and
setIndentLevel().
</notes>
- </release>
+ </release>
<release>
<version>
<release>0.3.0</release>
@@ -125,7 +145,7 @@ HTML_Common2 package provides methods for HTML code display and attributes handl
<date>2006-10-16</date>
<license uri="http://opensource.org/licenses/bsd-license.php">BSD License</license>
<notes>
-Radical changes to API are not expected after this release, you may use the
+Radical changes to API are not expected after this release, you may use the
package for development.
Changes:
@@ -133,7 +153,7 @@ Changes:
* Removed toHtml() and display() methods, __toString() is now declared abstract
* Attribute values are always converted to strings
* Minor performance tweaks
-* Switched to BSD license and package.xml 2.0
+* Switched to BSD license and package.xml 2.0
</notes>
</release>
View
2  tests/AllTests.php
@@ -6,7 +6,7 @@
*
* LICENSE:
*
- * Copyright (c) 2004-2011, Alexey Borzov <avb@php.net>
+ * Copyright (c) 2004-2012, Alexey Borzov <avb@php.net>
*
* All rights reserved.
*
View
32 tests/HTML_Common2_Test.php
@@ -6,7 +6,7 @@
*
* LICENSE:
*
- * Copyright (c) 2004-2011, Alexey Borzov <avb@php.net>
+ * Copyright (c) 2004-2012, Alexey Borzov <avb@php.net>
*
* All rights reserved.
*
@@ -55,10 +55,15 @@
require_once 'PHPUnit/Framework.php';
}
-/**
- * HTML_Common2 class
- */
-require_once 'HTML/Common2.php';
+
+if ('@' . 'package_version@' == '@package_version@') {
+ // If running from SVN checkout, do a relative include
+ require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'HTML/Common2.php';
+} else {
+ // If installed, use include_path
+ require_once 'HTML/Common2.php';
+}
+
/**
* A non-abstract subclass of HTML_Common2
@@ -300,5 +305,22 @@ public function testCanRemoveCssClasses()
$obj->removeClass(array('quux'));
$this->assertEquals(null, $obj->getAttribute('class'));
}
+
+ public function testArrayAccess()
+ {
+ $obj = new HTML_Common2_Concrete(array('baz' => 'quux'));
+ $this->assertTrue(isset($obj['baz']));
+ $this->assertEquals('quux', $obj['baz']);
+
+ $obj['foo'] = 'bar';
+ $this->assertTrue(isset($obj['foo']));
+ $this->assertEquals('bar', $obj['foo']);
+ unset($obj['fOo']);
+ $this->assertFalse(isset($obj['foo']));
+
+ $obj[] = 'disabled';
+ $this->assertTrue(isset($obj['disabled']));
+ $this->assertEquals('disabled', $obj['disabled']);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.