cvs: pear /HTML_Common2 Common2.php /HTML_Common2/tests HTML_Common2_Test.php

From: Date: Fri, 23 Jun 2006 14:09:41 +0000
Subject: cvs: pear /HTML_Common2 Common2.php /HTML_Common2/tests HTML_Common2_Test.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-40878@lists.php.net to get a copy of this message
avb Fri Jun 23 14:09:41 2006 UTC Modified files: /pear/HTML_Common2 Common2.php /pear/HTML_Common2/tests HTML_Common2_Test.php Log: * made the attributes array protected rather than private * added the magic __toString() method, deprecated display() * It is now possible to watch attributes for changes, this may be useful later for e.g. 'type' attribute of <input /> tag --- should be read-only 'id' attribute of tag --- change should be announced to the containing element

http://cvs.php.net/viewvc.cgi/pear/HTML_Common2/Common2.php?r1=1.2&r2=1.3&diff_format=u Index: pear/HTML_Common2/Common2.php diff -u pear/HTML_Common2/Common2.php:1.2 pear/HTML_Common2/Common2.php:1.3 --- pear/HTML_Common2/Common2.php:1.2 Wed Oct 27 09:02:57 2004 +++ pear/HTML_Common2/Common2.php Fri Jun 23 14:09:40 2006 @@ -11,9 +11,9 @@ * @category HTML * @package HTML_Common2 * @author Alexey Borzov <avb@php.net> - * @copyright 2004 The PHP Group + * @copyright 2004, 2005, 2006 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License - * @version CVS: $Id: Common2.php,v 1.2 2004/10/27 09:02:57 avb Exp $ + * @version CVS: $Id: Common2.php,v 1.3 2006/06/23 14:09:40 avb Exp $ * @link http://pear.php.net/package/HTML_Common2 */ @@ -24,7 +24,7 @@ * attribute strings. Port of HTML_Common class for PHP4 originally written by * Adam Daniel with contributions from numerous other developers. * - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * @author Alexey Borzov <avb@php.net> */ abstract class HTML_Common2 @@ -33,7 +33,15 @@ * Associative array of attributes * @var array */ - private $_attributes = array(); + protected $attributes = array(); + + /** + * List of attribites changes to which will be announced via onAttributeChange() + * method rather than performed by HTML_Common2 class itself + * @var array + * @see onAttributeChange() + */ + protected $watchedAttributes = array(); /** * Indentation level of the element @@ -90,44 +98,59 @@ } /** + * Parses the HTML attributes given as string + * + * @param string HTML attribute string + * @return array An associative aray of attributes + */ + protected static function parseAttributes($attrString) + { + $attributes = array(); + if (preg_match_all( + "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" . + "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/", + $attrString, + $regs + )) { + for ($i = 0; $i < count($regs[1]); $i++) { + $name = trim($regs[1][$i]); + $check = trim($regs[0][$i]); + $value = trim($regs[7][$i]); + if ($name == $check) { + $attributes[strtolower($name)] = strtolower($name); + } else { + if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) { + $value = substr($value, 1, -1); + } + $attributes[strtolower($name)] = $value; + } + } + } + return $attributes; + } + + /** * Creates a valid attribute array from either a string or an array * * @param mixed Array of attributes or HTML attribute string * @return array An associative aray of attributes */ - protected static function parseAttributes($attributes) + protected static function prepareAttributes($attributes) { - $ret = array(); - if (is_array($attributes)) { + $prepared = array(); + if (is_string($attributes)) { + $prepared = self::parseAttributes($attributes); + } elseif (is_array($attributes)) { foreach ($attributes as $key => $value) { if (is_int($key)) { $key = $value = strtolower($value); } else { $key = strtolower($key); } - $ret[$key] = $value; - } - - } elseif (is_string($attributes)) { - $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" . - "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/"; - if (preg_match_all($preg, $attributes, $regs)) { - for ($i = 0; $i < count($regs[1]); $i++) { - $name = trim($regs[1][$i]); - $check = trim($regs[0][$i]); - $value = trim($regs[7][$i]); - if ($name == $check) { - $ret[strtolower($name)] = strtolower($name); - } else { - if (!empty($value) && ($value{0} == '\'' || $value{0} == '"')) { - $value = substr($value, 1, -1); - } - $ret[strtolower($name)] = $value; - } - } + $prepared[$key] = (string)$value; } } - return $ret; + return $prepared; } /** @@ -169,7 +192,7 @@ */ public function __construct($attributes = null) { - $this->setAttributes($attributes); + $this->mergeAttributes($attributes); } /** @@ -184,7 +207,11 @@ if (is_null($value)) { $value = $name; } - $this->_attributes[$name] = $value; + if (in_array($name, $this->watchedAttributes)) { + $this->onAttributeChange($name, $value); + } else { + $this->attributes[$name] = $value; + } } /** @@ -196,7 +223,7 @@ public function getAttribute($name) { $name = strtolower($name); - return isset($this->_attributes[$name])? $this->_attributes[$name]: null; + return isset($this->attributes[$name])? $this->attributes[$name]: null; } /** @@ -206,7 +233,20 @@ */ public function setAttributes($attributes) { - $this->_attributes = self::parseAttributes($attributes); + $attributes = self::prepareAttributes($attributes); + $watched = array(); + foreach ($this->watchedAttributes as $watchedKey) { + if (isset($attributes[$watchedKey])) { + $this->setAttribute($watchedKey, $attributes[$watchedKey]); + unset($attributes[$watchedKey]); + } else { + $this->removeAttribute($watchedKey); + } + if (isset($this->attributes[$watchedKey])) { + $watched[$watchedKey] = $this->attributes[$watchedKey]; + } + } + $this->attributes = array_merge($watched, $attributes); } /** @@ -218,9 +258,9 @@ public function getAttributes($asString = false) { if ($asString) { - return self::getAttributesString($this->_attributes); + return self::getAttributesString($this->attributes); } else { - return $this->_attributes; + return $this->attributes; } } @@ -231,7 +271,14 @@ */ public function mergeAttributes($attributes) { - $this->_attributes = array_merge($this->_attributes, self::parseAttributes($attributes)); + $attributes = self::prepareAttributes($attributes); + foreach ($this->watchedAttributes as $watchedKey) { + if (isset($attributes[$watchedKey])) { + $this->onAttributeChange($watchedKey, $attributes[$watchedKey]); + unset($attributes[$watchedKey]); + } + } + $this->attributes = array_merge($this->attributes, $attributes); } /** @@ -241,7 +288,11 @@ */ public function removeAttribute($attribute) { - self::removeAttributeArray($this->_attributes, $attribute); + if (in_array(strtolower($attribute), $this->watchedAttributes)) { + $this->onAttributeChange(strtolower($attribute), null); + } else { + self::removeAttributeArray($this->attributes, $attribute); + } } /** @@ -305,11 +356,41 @@ abstract public function toHtml(); /** + * A magic method to let the object work with echo and print constructs + * + * @return string + * @see toHtml() + */ + public function __toString() + { + return $this->toHtml(); + } + + /** * Displays the element + * + * @deprecated Deprecated, using <code>echo $obj;</code> will work just as well */ public function display() { echo $this->toHtml(); } + + /** + * Called if trying to change an attribute with name in $watchedAttributes + * + * This method is called for each attribute whose name is in the + * $watchedAttributes array and which is being changed by setAttribute(), + * setAttributes() or mergeAttributes() or removed via removeAttribute(). + * Note that the operation for the attribute is not carried on after calling + * this method, it is the responsibility of this method to change or remove + * (or not) the attribute. + * + * @param string Attribute name + * @param string Attribute value, null if attribute is being removed + */ + protected function onAttributeChange($name, $value = null) + { + } } ?> http://cvs.php.net/viewvc.cgi/pear/HTML_Common2/tests/HTML_Common2_Test.php?r1=1.2&r2=1.3&diff_format=u Index: pear/HTML_Common2/tests/HTML_Common2_Test.php diff -u pear/HTML_Common2/tests/HTML_Common2_Test.php:1.2 pear/HTML_Common2/tests/HTML_Common2_Test.php:1.3 --- pear/HTML_Common2/tests/HTML_Common2_Test.php:1.2 Wed Oct 27 09:02:57 2004 +++ pear/HTML_Common2/tests/HTML_Common2_Test.php Fri Jun 23 14:09:41 2006 @@ -3,7 +3,7 @@ * Unit test suite for HTML_Common2 class * * @author Alexey Borzov <avb@php.net> - * @version $Id: HTML_Common2_Test.php,v 1.2 2004/10/27 09:02:57 avb Exp $ + * @version $Id: HTML_Common2_Test.php,v 1.3 2006/06/23 14:09:41 avb Exp $ */ if (!defined('PHPUnit2_MAIN_METHOD')) { @@ -13,7 +13,7 @@ require_once 'PHPUnit2/Framework/TestCase.php'; require_once 'HTML/Common2.php'; -// HTML_Common2 cannot be instantiated due to abstract toHtml() +// HTML_Common2 cannot be instantiated directly class HTML_Common2_Concrete extends HTML_Common2 { public function toHtml() @@ -22,6 +22,30 @@ } } +class HTML_Common2_WatchedAttributes extends HTML_Common2_Concrete +{ + protected $watchedAttributes = array('readonly', 'uppercase'); + + protected $attributes = array( + 'readonly' => 'this attribute is readonly', + 'uppercase' => 'VALUE OF THIS IS ALWAYS UPPERCASE' + ); + + protected function onAttributeChange($name, $value = null) + { + if ('readonly' == $name) { + return; + } + if ('uppercase' == $name) { + if (null === $value) { + unset($this->attributes[$name]); + } else { + $this->attributes[$name] = strtoupper($value); + } + } + } +} + class HTML_Common2_Test extends PHPUnit2_Framework_TestCase { public function testUnknownOptionIsNull() @@ -116,6 +140,39 @@ $obj->getAttributes(true) ); } + + public function testCanWatchAttributes() + { + $obj = new HTML_Common2_WatchedAttributes(); + + $obj->setAttributes(array('readonly' => 'something', 'uppercase' => 'new value', 'foo' => 'bar')); + $this->assertEquals( + array('readonly' => 'this attribute is readonly', 'uppercase' => 'NEW VALUE', 'foo' => 'bar'), + $obj->getAttributes() + ); + + $obj->mergeAttributes(array('readonly' => 'something', 'uppercase' => 'other value', 'foo' => 'baz')); + $this->assertEquals( + array('readonly' => 'this attribute is readonly', 'uppercase' => 'OTHER VALUE', 'foo' => 'baz'), + $obj->getAttributes() + ); + + $obj->setAttribute('readonly', 'something else'); + $obj->setAttribute('uppercase', 'yet another value'); + $obj->setAttribute('foo', 'quux'); + $this->assertEquals( + array('readonly' => 'this attribute is readonly', 'uppercase' => 'YET ANOTHER VALUE', 'foo' => 'quux'), + $obj->getAttributes() + ); + + $obj->removeAttribute('readonly'); + $obj->removeAttribute('uppercase'); + $obj->removeAttribute('foo'); + $this->assertEquals( + array('readonly' => 'this attribute is readonly'), + $obj->getAttributes() + ); + } } require_once 'PHPUnit2/Framework/TestSuite.php';
« previous php.pear.cvs (#40878) next »