/** * Sets value for attribute in associative array $list. * * @param string $attribute * @param array $values * */ private function setAttribute(Trace $trace, $attribute, $values) { $new_values = array(); foreach ($values as $value) { if ($value != '') { $new_values[] = $value; } } $values = $new_values; if (count($values) == 0) { $trace->tlog("Ignoring empty attribute '" . $attribute . "'"); return; } $attribute = trim($attribute); if (StrUtil::endsWith($attribute, ':')) { $attribute = substr($attribute, 0, strlen($attribute) - 1); } $id = null; $name = $attribute; if (isset($this->attr_def[$attribute])) { $id = $this->attr_def[$attribute]; $name = InformacnyListAttributeEnum::getUnicodeName($id); } if ($id == self::IGNORE_ATTRIBUTE) { $trace->tlog("Ignoring attribute '" . $attribute . "'"); return; } $this->list[] = array('id' => $id, 'name' => $name, 'rawLabel' => $attribute, 'values' => $values); }
public function testEndsWith() { $this->assertTrue(StrUtil::endsWith('foobar', 'bar'), 'foobar ends with bar'); $this->assertTrue(StrUtil::endsWith('foobar', ''), 'foobar ends with empty string'); $this->assertTrue(StrUtil::endsWith('', ''), 'empty string ends with empty string'); $this->assertFalse(StrUtil::endsWith('foobar', 'baz'), 'foobar does not end with baz'); $this->assertFalse(StrUtil::endsWith('foobar', 'foo'), 'foobar does not end with foo'); $this->assertFalse(StrUtil::endsWith('foobar', 'xfoobar'), 'foobar does not end with xfoobar'); }
/** * Joins two or more path components. * If joining more than two path components, the result is * the same as calling two-argument joinPath successively. * Moreover, this function is associative, i.e. * joinPath('a','b','c') has the same effect as * joinPath(joinPath('a', 'b'), 'c') or joinPath('a', joinPath('b', 'c')) * * Path components of zero length are ignored * * @param string $a first path component * @param string $b second path component * @param string ... any other path components to join * @returns string all the paths joined using a directory separator */ public static function joinPath($a, $b) { $args = func_get_args(); $num_args = count($args); $shouldAddDS = true; // start with $a, omit trailing directory separator if ($a == DIRECTORY_SEPARATOR || $a == '') { $shouldAddDS = false; $path = $a; } else { if (StrUtil::endsWith($a, DIRECTORY_SEPARATOR)) { $path = substr($a, 0, strlen($a) - 1); } else { $path = $a; } } // add other components for ($i = 1; $i < $num_args; $i++) { $part = $args[$i]; // DIRECTORY_SEPARATOR or empty string is a special case if ($part == DIRECTORY_SEPARATOR) { continue; } // first extract range of part without leading or trailing DS if (StrUtil::startsWith($part, DIRECTORY_SEPARATOR)) { $start = 1; } else { $start = 0; } if (StrUtil::endsWith($part, DIRECTORY_SEPARATOR)) { $end = strlen($part) - 1; } else { $end = strlen($part); } // append a path component if ($shouldAddDS) { $path .= DIRECTORY_SEPARATOR; } $shouldAddDS = true; $path .= substr($part, $start, $end); } return $path; }