/** * 文字列の先頭が, このオブジェクトにセットされている候補一覧の中のいずれかに合致するかどうかを調べます. * 合致した候補文字列を返します. * * @param string $input 検査対象の文字列 * @return string 合致した候補文字列 */ public function match($input) { foreach ($this->candidates as $candidate) { if (Strings::startsWith($input, $candidate)) { return $candidate; } } return null; }
/** * 指定された UTF-8 の文字列を Unicode 符号点の配列に変換します. * * @param string $text UTF-8 でエンコードされた文字列 * @return array Unicode 符号点の配列 */ public function decode($text) { $bom = chr(0xef) . chr(0xbb) . chr(0xbf); if (Strings::startsWith($text, $bom)) { return $this->decode(substr($text, 3)); } $context = new Utf8Context($text); $result = array(); while ($context->hasNext()) { $result[] = $context->next(); } // 文字列の末尾に不正な文字が存在していた場合, // $result の最後の要素に null が代入されるので取り除く $count = count($result); if ($count && $result[$count - 1] === null) { array_pop($result); } return $result; }
/** * オプション PRETTY_PRINT を指定した場合, * object および array 形式の値を半角スペース 4 個と改行文字で整形して出力することを確認します. * * @covers Peach\DF\JsonCodec::__construct * @covers Peach\DF\JsonCodec::encode * @covers Peach\DF\JsonCodec::encodeValue * @covers Peach\DF\JsonCodec::encodeArray * @covers Peach\DF\JsonCodec::encodeObject */ public function testEncodeWithPrettyPrint() { $obj1 = $this->object; $obj2 = new JsonCodec(array(JsonCodec::PRETTY_PRINT => true)); $test = array("first" => array("test01" => 0, "test02" => 100, "test03" => -50), "second" => array(1.5, -1.5, 1.25E-16), "third" => "hogehoge", "fourth" => array("test04" => array("true" => true, "false" => false, "null" => null))); $result1 = $obj1->encode($test); $expected1 = file_get_contents(__DIR__ . "/JsonCodec/encode-default.txt"); $this->assertSame($expected1, $result1); $result2 = $obj2->encode($test); $expected2 = file_get_contents(__DIR__ . "/JsonCodec/encode-pretty_print.txt"); $this->assertSame(Strings::getLines($expected2), Strings::getLines($result2)); }
/** * Code を読み込みます. * @param Code $code */ public function handleCode(Code $code) { $text = $code->getText(); if (!strlen($text)) { return; } $lines = Strings::getLines($text); $indent = $this->indent(); $this->result .= $indent; $this->result .= implode($this->breakCode() . $indent, $lines); }
/** * 指定されたフィールド名を $fields のインデックスに変換します. * 不正なフィールド名の場合は -1 を返します. * * @param string $field フィールド名 * @return int インデックス * * @see Time::$YEAR * @see Time::$MONTH * @see Time::$DATE * @see Time::$HOUR * @see Time::$MINUTE * @see Time::$SECOND * @codeCoverageIgnore */ private function getFieldIndex($field) { static $mapping = null; if (!isset($mapping)) { $mapping = array("y" => self::$YEAR, "mo" => self::$MONTH, "d" => self::$DATE, "h" => self::$HOUR, "m" => self::$MINUTE, "s" => self::$SECOND); } $field = strtolower($field); foreach ($mapping as $key => $index) { if (Strings::startsWith($field, $key)) { return $index; } } return -1; }
/** * 指定された JSON 文字列を値に変換します. * * 引数が空白文字列 (または null, false) の場合は null を返します. * * @param string $text 変換対象の JSON 文字列 * @return mixed 変換結果 */ public function decode($text) { if (Strings::isWhitespace($text)) { return null; } try { $root = new Root(); $root->handle(new Context($text, $this->decodeOptions)); return $root->getResult(); } catch (DecodeException $e) { throw new InvalidArgumentException($e->getMessage()); } }