/** * does a list of values pass inspection? * * @param mixed $list * the list of data to be examined * @return bool * TRUE if the inspection passes * FALSE otherwise */ public function inspectList($list) { // robustness if (!is_list($list, '$list')) { throw new InvalidArgumentException("'\$list' is not a valid list"); } // a simple foreach() is all that we need here foreach ($list as $name => $item) { if (!$this->inspect($item)) { return false; } } // if we get here, then all is good return true; }
public function serialize(&$var) { if (!isset($var) || $var === NULL) { $this->writeNull(); } elseif (is_scalar($var)) { if (is_int($var)) { $this->writeInteger($var); } elseif (is_bool($var)) { $this->writeBoolean($var); } elseif (is_float($var)) { $this->writeDouble($var); } elseif (is_string($var)) { if ($var === '') { $this->writeEmpty(); } elseif (strlen($var) < 4 && is_utf8($var) && ustrlen($var) == 1) { $this->writeUTF8Char($var); } elseif (is_utf8($var)) { $this->writeString($var, true); } else { $this->writeBytes($var, true); } } } elseif (is_array($var)) { if (is_list($var)) { $this->writeList($var, true); } else { $this->writeMap($var, true); } } elseif (is_object($var)) { if ($var instanceof stdClass) { $this->writeStdObject($var, true); } elseif ($var instanceof HproseDate || $var instanceof HproseDateTime) { $this->writeDate($var, true); } elseif ($var instanceof HproseTime) { $this->writeTime($var, true); } else { $this->writeObject($var, true); } } else { throw new HproseException('Not support to serialize this data'); } }
public static function Option($a = array(), $v = null, $all = null) { $option = null; if ($all) { $selected = $v ? null : 'selected'; $option .= "<option value='' {$selected}>" . strip_tags($all) . "</option>"; } $v = explode(',', $v); settype($v, 'array'); if (is_list($a)) { $a = array_to_map($a); } foreach ($a as $key => $value) { if (is_array($value)) { $key = strval($value['id']); $value = strval($value['name']); } $selected = in_array($key, $v) ? 'selected' : null; $option .= "<option value='{$key}' {$selected}>" . strip_tags($value) . "</option>"; } return $option; }
public function obj2Array($obj, $root_name) { if ($obj) { $array = objectToArray($obj); if ($array != "") { if ($root_name) { //如果填写了root_name,表示内容应该是个数组,但是返回来如果不是数组(有可能只有一项,simplexml_load_string就解析成不是数组了),这样,外面套一个数组的壳,保证外层使用的一致性 if (is_list($array[$root_name])) { return $array[$root_name]; } else { return array($array[$root_name]); } } else { return $array; } } else { return null; } } else { return null; } }
function column_item_value($key, &$data, $column_info, $length) { $display_value = $data[$key]; if ($length != 0) { $display_value = substr($display_value, 0, $length); } if ($column_info[$key]['type'] == 'select') { $options = $column_info[$key]['options']; if (is_list($options)) { $options = array_to_map($options); } if ($options[$display_value]) { $display_value = $options[$display_value]; } } if ($column_info[$key]['link']) { $link = $column_info[$key]['link']; $link_type = $link['link_type']; $column = $link['column']; if (empty($column)) { $column = $key; } if ($link_type == 'school' || $link_type == 'project' || $link_type == 'user') { $html = "<a target='_blank' href='/{$link_type}/detail/" . $data[$column] . "'>" . $display_value . "</a>"; } if ($link_type == 'mail') { $html = "<a target='_blank' href='mailto:" . $display_value . "'>" . $display_value . "</a>"; } if ($link_type == 'website') { $html = "<a target='_blank' href='" . $data[$column] . "'>" . $display_value . "</a>"; } if ($link_type == 'weibo') { $weibo_link = $data['weibo_link']; $weibo_name = $data['weibo_name']; if ($weibo_link && $weibo_name) { $html = "<a target='_blank' href='" . $weibo_link . "'>" . $weibo_name . "</a>"; } } if ($link_type == 'tag') { $html = display_tag_string($data['id'], $data['tags']); } return $html; } else { if ($column_info[$key]['extra_type'] == 'money') { return moneyit($display_value); } return $display_value; } }
function array_template($array, $tpl, $additive = false) { if (gettype($array) != "array" || gettype($tpl) != "array" || is_list($array)) { return $array; } if ($additive) { $result = $tpl; } else { $result = array(); } foreach ($tpl as $idx => $value) { if (isset($array[$idx])) { $result[$idx] = array_template($array[$idx], $value); } } // foreach return $result; }
/** * @covers ::is_list * @dataProvider provideNonLists * * @param mixed $list * the non-list that we're going to use */ public function test_anything_else_returns_false($list) { // ---------------------------------------------------------------- // setup your test // ---------------------------------------------------------------- // perform the change is_list($list); // ---------------------------------------------------------------- // test the results }
public function serialize(&$variable) { switch (gettype($variable)) { case 'NULL': $this->writeNull(); break; case 'boolean': $this->writeBoolean($variable); break; case 'integer': $this->writeInteger($variable); break; case 'double': $this->writeDouble($variable); break; case 'string': if ($variable == '') { $this->writeEmpty(); } elseif (strlen($variable) < 4 && ustrlen($variable) == 1) { $this->writeUTF8Char($variable); } elseif (($ref = $this->ref_search($variable)) !== false) { $this->writeRef($ref); } elseif (is_utf8($variable)) { $this->writeString($variable, false); } else { $this->writeBytes($variable, false); } break; case 'array': if (($ref = $this->ref_search($variable)) !== false) { $this->writeRef($ref); } elseif (is_list($variable)) { $this->writeList($variable, false); } else { $this->writeMap($variable, false); } break; case 'object': if (($ref = $this->ref_search($variable)) !== false) { $this->writeRef($ref); } elseif ($variable instanceof stdClass) { $this->writeMap($variable, false); } elseif ($variable instanceof HproseDate || $variable instanceof HproseDateTime) { $this->writeDate($variable, false); } elseif ($variable instanceof HproseTime) { $this->writeTime($variable, false); } else { $this->writeObject($variable, false); } break; default: throw new HproseException('Not support to serialize this data'); } }
public function serialize(&$variable) { switch (gettype($variable)) { case "NULL": $this->writeNull(); break; case "boolean": $this->writeBoolean($variable); break; case "integer": $this->writeInteger($variable); break; case "double": $this->writeDouble($variable); break; case "string": if ($variable == "") { $this->writeEmpty(); } else { if (strlen($variable) < 4 && ustrlen($variable) == 1) { $this->writeUTF8Char($variable); } else { if (($ref = $this->ref_search($variable)) !== false) { $this->writeRef($ref); } else { if (is_utf8($variable)) { $this->writeString($variable, false); } else { $this->writeBytes($variable, false); } } } } break; case "array": if (($ref = $this->ref_search($variable)) !== false) { $this->writeRef($ref); } else { if (is_list($variable)) { $this->writeList($variable, false); } else { $this->writeMap($variable, false); } } break; case "object": if (($ref = $this->ref_search($variable)) !== false) { $this->writeRef($ref); } else { if ($variable instanceof stdClass) { $this->writeMap($variable, false); } else { if ($variable instanceof HproseDate || $variable instanceof HproseDateTime) { $this->writeDate($variable, false); } else { if ($variable instanceof HproseTime) { $this->writeTime($variable, false); } else { $this->writeObject($variable, false); } } } } break; default: throw new HproseException("Not support to serialize this data"); } }
/** * @depends testWhitespace */ public function testJSON($games) { try { $parser = new JsonParser(); $games = $parser->parse($games, JsonParser::DETECT_KEY_CONFLICTS + JsonParser::PARSE_TO_ASSOC); } catch (Exception $e) { $this->assertTrue('parsing', $e->getMessage()); } // TODO make better is_ tests for mixed fields $allowedKeys = array('appid' => 'is_set', 'title' => 'is_string', 'duplicate' => 'is_npcommid', 'note' => 'is_string', 'map' => 'is_set', 'offset' => 'is_string'); foreach ($games as $appID => $keys) { $this->assertTrue(is_npcommid($appID), 'Key "' . $appID . '" must be a Sony game ID'); if (is_array($keys)) { $this->assertNotEmpty($keys, '"' . $appID . '" can not be an empty array'); foreach ($keys as $key => $value) { $this->assertArrayHasKey($key, $allowedKeys, 'Invalid key "' . $key . '" for "' . $appID . '"'); $this->assertTrue($allowedKeys[$key]($value), '"' . $key . '" for "' . $appID . '" is not "' . $allowedKeys[$key] . '"'); if ($key === 'appid') { if (is_array($value)) { $this->assertNotEmpty($value, '"' . $key . '" can not be an empty array'); foreach ($value as $steamappid) { $this->assertTrue(is_appid($steamappid), 'Value "' . $steamappid . '" in "' . $key . '" for "' . $appID . '" must be a valid Steam appid'); } } else { $this->assertTrue(is_appid($value), 'Value "' . $value . '" in "' . $key . '" for "' . $appID . '" must be a valid Steam appid'); } } else { if ($key === 'note') { $this->assertNotEmpty($value, '"' . $key . '" for "' . $appID . '" can not be an empty string'); } else { if ($key === 'map') { if (is_string($value)) { if (strpos($value, '%d') !== false) { /* We are a direct mapping of 0 → name_0, 1 → name_1 */ } else { if (strpos($value, '%02d') !== false) { /* We are a direct mapping of 0 → name_00, 1 → name_01 */ } else { $this->assertTrue(false, 'Value "' . $value . '" for "map" is not a valid map string'); } } } else { if (is_bool($value)) { // for direct mappings, we use "map": false // XXX rework this to confirm NO map exists $this->assertTrue($value === false, 'Key "' . $key . '" for "' . $appID . '" is not a valid value'); } else { if (is_list($value)) { $this->assertNotEmpty($value, 'Key "' . $key . '" for "' . $appID . '" can not be an empty array'); // XXX: Found the first title that mapped Platinum to a Steam achievement, Trine! // $this->assertFalse( array_key_exists( "0", $value ), '"' . $key . '" for "' . $appID . '" has an achievement mapped to Platinum trophy' ); // Discover is we are a multi-grouped map foreach ($value as $index => $group) { if (is_string($group)) { } else { if (is_assoc($group)) { // Our current process makes this recursive for an extra level… // XXX break out duplicate test } else { if (is_list($group)) { // Find accidental duplicates, remove "-1" entries and flatten multiple pairings $valuesmapped = array_values($group); foreach ($valuesmapped as $index => $achievement) { if ($achievement == "-1") { unset($valuesmapped[$index]); } if (is_array($achievement)) { $valuesmapped[$index] = implode("-", $achievement); } } $this->assertTrue(count($valuesmapped) === count(array_unique($valuesmapped)), '"' . $key . '" for "' . $appID . '" has a duplicate mapping'); unset($valuesmapped); } } } } } else { // XXX We should never get here } } } } else { if ($key === 'offset') { // XXX verify "map" is set, and is using "%d" format // XXX validate info inside to support: // -count : basically to mask the current count (starts with 0) // [+-][0-9]* : the offset to the passed in count } } } } } } else { $this->assertTrue(false, 'Value "' . $appID . '" has an invalid value'); } } return $games; }
public function serialize(&$var) { if (!isset($var) || $var === NULL) { $this->writeNull(); } elseif (is_scalar($var)) { if (is_int($var)) { if ($var >= 0 && $var <= 9) { $this->stream->write((string) $var); } else { $this->writeInteger($var); } } elseif (is_bool($var)) { $this->writeBoolean($var); } elseif (is_float($var)) { $this->writeDouble($var); } elseif (is_string($var)) { if ($var === '') { $this->writeEmpty(); } elseif (strlen($var) < 4 && is_utf8($var) && ustrlen($var) == 1) { $this->writeUTF8Char($var); } elseif (is_utf8($var)) { $this->writeStringWithRef($var); } else { $this->writeBytesWithRef(bytes($var)); } } } elseif (is_array($var)) { if (is_list($var)) { $this->writeListWithRef($var); } else { $m = map($var); $this->writeMapWithRef($m); } } elseif (is_object($var)) { if ($var instanceof stdClass) { $this->writeMapWithRef($var); } elseif ($var instanceof DateTime) { $this->writeDateTimeWithRef($var); } elseif ($var instanceof HproseDate || $var instanceof HproseDateTime) { $this->writeDateWithRef($var); } elseif ($var instanceof HproseTime) { $this->writeTimeWithRef($var); } elseif ($var instanceof HproseBytes) { $this->writeBytesWithRef($var); } elseif ($var instanceof HproseMap) { $this->writeMapWithRef($var); } else { $this->writeObjectWithRef($var); } } else { throw new Exception('Not support to serialize this data'); } }
function hprose_fast_serialize(&$v, $ro) { if ($v === NULL) { return 'n'; } if (is_int($v)) { if ($v >= 0 && $v <= 9) { return (string) $v; } return 'i' . $v . ';'; } if (is_bool($v)) { return $v ? 't' : 'f'; } if (is_float($v)) { if (is_nan($v)) { return 'N'; } if (is_infinite($v)) { return $v > 0 ? 'I+' : 'I-'; } return 'd' . $v . ';'; } if (is_string($v)) { if ($v === '') { return 'e'; } if (is_utf8($v)) { $l = ustrlen($v); if ($l == 1) { return 'u' . $v; } $h = hprose_hash($v, $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } $ro->r[$h] = $ro->length++; return 's' . $l . '"' . $v . '"'; } $h = hprose_hash($v, $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } $ro->r[$h] = $ro->length++; return 'b' . strlen($v) . '"' . $v . '"'; } if (is_array($v)) { $c = count($v); if (is_list($v)) { $h = hprose_hash($v, $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } $ro->r[$h] = $ro->length++; if ($c == 0) { return 'a{}'; } $s = 'a' . $c . '{'; foreach ($v as &$val) { $s .= hprose_fast_serialize($val, $ro); } return $s . '}'; } $h = hprose_hash(map($v), $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } $ro->r[$h] = $ro->length++; $s = 'm' . $c . '{'; foreach ($v as $key => &$val) { $s .= hprose_fast_serialize($key, $ro) . hprose_fast_serialize($val, $ro); } return $s . '}'; } if (is_object($v)) { $h = hprose_hash($v, $ro); if (array_key_exists($h, $ro->r)) { return 'r' . $ro->r[$h] . ';'; } if ($v instanceof stdClass) { $ro->r[$h] = $ro->length++; $v = (array) $v; $c = count($v); if ($c == 0) { return 'a{}'; } $s = 'm' . $c . '{'; foreach ($v as $key => &$val) { $s .= hprose_fast_serialize($key, $ro) . hprose_fast_serialize($val, $ro); } return $s . '}'; } if ($v instanceof DateTime) { $ro->r[$h] = $ro->length++; if ($v->getOffset() == 0) { return $v->format("\\DYmd\\THis.u\\Z"); } return $v->format("\\DYmd\\THis.u;"); } if ($v instanceof HproseDate || $v instanceof HproseDateTime) { $ro->r[$h] = $ro->length++; if ($v->utc) { return 'D' . $v->toString(false); } return 'D' . $v->toString(false) . 'Z'; } if ($v instanceof HproseTime) { $ro->r[$h] = $ro->length++; if ($v->utc) { return 'T' . $v->toString(false); } return 'T' . $v->toString(false) . 'Z'; } if ($v instanceof HproseBytes) { $ro->r[$h] = $ro->length++; $c = strlen($v->value); if ($c == 0) { return 'b""'; } return 'b' . $c . '"' . $v->value . '"'; } if ($v instanceof HproseMap) { $ro->r[$h] = $ro->length++; $c = count($v->value); if ($c == 0) { return 'a{}'; } $s = 'm' . $c . '{'; foreach ($v->value as $key => &$val) { $s .= hprose_fast_serialize($key, $ro) . hprose_fast_serialize($val, $ro); } return $s . '}'; } $class = get_class($v); $alias = HproseClassManager::getClassAlias($class); $a = (array) $v; if (array_key_exists($alias, $ro->cr)) { $index = $ro->cr[$alias]; $fields = $ro->fr[$index]; $c = count($fields); $s = ''; } else { $l = ustrlen($alias); $s = 'c' . $l . '"' . $alias . '"'; $fields = array_keys($a); $c = count($fields); if ($c > 0) { $s .= $c . '{'; foreach ($fields as $field) { if ($field[0] === "") { $field = substr($field, strpos($field, "", 1) + 1); } $fh = hprose_hash($field, $ro); $ro->r[$fh] = $ro->length++; $s .= 's' . ustrlen($field) . '"' . $field . '"'; } $s .= '}'; } else { $s .= '{}'; } $index = count($ro->fr); $ro->cr[$alias] = $index; $ro->fr[$index] = $fields; } $ro->r[$h] = $ro->length++; $s .= 'o' . $index . '{'; for ($i = 0; $i < $c; ++$i) { $s .= hprose_fast_serialize($a[$fields[$i]], $ro); } return $s . '}'; } throw new Exception('Not support to serialize this data'); }
function json_encode_for_xedit($array) { if (!$array) { return false; } if (is_list($array)) { $array = array_to_map($array); } foreach ($array as $k => $v) { $temp_array[] = array('value' => $k, 'text' => $v); } return json_encode($temp_array); }