mansion Tue Nov 26 14:16:35 2002 EDT
Modified files:
/pear/Config/Config Container.php
Log:
Added a new countChildren method useful when a section has multiple
directives with the same name.
Index: pear/Config/Config/Container.php
diff -u pear/Config/Config/Container.php:1.6
pear/Config/Config/Container.php:1.7
--- pear/Config/Config/Container.php:1.6 Tue Nov 26 08:38:38 2002
+++ pear/Config/Config/Container.php Tue Nov 26 14:16:34 2002
@@ -15,7 +15,7 @@
// | Author: Bertrand Mansion <bmansion@xxxxxxxxxxx> |
// +---------------------------------------------------------------------+
//
-// $Id: Container.php,v 1.6 2002/11/26 13:38:38 mansion Exp $
+// $Id: Container.php,v 1.7 2002/11/26 19:16:34 mansion Exp $
require_once('Config.php');
@@ -215,12 +215,7 @@
if ($this->type != 'section') {
return PEAR::raiseError('Config_Container::getItem must be called
on a section type object.', null, PEAR_ERROR_RETURN);
}
- $testFields = array();
- if ($type == '') {
- return PEAR::raiseError('You must specify an existing type in
Config_Container::getItem.', null, PEAR_ERROR_RETURN);
- } else {
- $testFields[] = 'type';
- }
+ $testFields[] = 'type';
if (!is_null($name)) {
$testFields[] = 'name';
}
@@ -255,7 +250,51 @@
}
}
} // end func &getItem
-
+
+ /**
+ * Returns how many children this container has
+ *
+ * @param string type (optional)type of children counted
+ * @param string type (optional)name of children counted
+ * @return int number of children found
+ */
+ function countChildren($type = null, $name = null)
+ {
+ if ($this->type != 'section') {
+ return PEAR::raiseError('Config_Container::getChildrenNum must be
called on a section type object.', null, PEAR_ERROR_RETURN);
+ }
+ if (is_null($type) && is_null($name)) {
+ return count($this->children);
+ }
+ $count = 0;
+ if (isset($name) && isset($type)) {
+ for ($i = 0; $i < count($this->children); $i++) {
+ if ($this->children[$i]->name == $name &&
+ $this->children[$i]->type == $type) {
+ $count++;
+ }
+ }
+ return $count;
+ }
+ if (isset($type)) {
+ for ($i = 0; $i < count($this->children); $i++) {
+ if ($this->children[$i]->type == $type) {
+ $count++;
+ }
+ }
+ return $count;
+ }
+ if (isset($name)) {
+ // Some directives can have the same name
+ for ($i = 0; $i < count($this->children); $i++) {
+ if ($this->children[$i]->name == $name) {
+ $count++;
+ }
+ }
+ return $count;
+ }
+ } // end func &countChildren
+
/**
* Inserts an item to a specified position.
* The position is relative to a target object if it is defined.
--
PEAR CVS Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
|