本文介绍了提供人类可读的标识符表示形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为对上一个问题的跟踪在寻求解决一个已解决问题的解决方案时,我正在尝试找到一种以可读"方式表达任意标识符的方法.

上下文:我们正在使用实体(来自的域模型对象DDD ),具有 identity .此标识(映射到数据库主键)可以表示为字符串:'123''ABC'.

某些实体可以具有复合身份,即由两个或多个其他实体的 identity :array('123','ABC')组成.

有时候,我们想漂亮地打印此 identity ,或在只允许使用单个字符串的地方(例如,在HTML <option>值中)使用它.该过程必须是可预测的和可逆的,即,在如何将其恢复到原始状态时应该没有歧义.

当我们想人工读取 出于调试目的时,与a:2:{i:0;s:3:"123";i:1;s:3:"ABC";}相比,阅读123ABC123~ABC更容易,这就是为什么我们不这样做不想使用serialize()json_encode()之类的内置功能.

json_encode()做得很好,但是当在HTML中使用它(必须对引号进行正确编码)时,它变得非常不可读:

<option value="[&quot;123&quot;,&quot;ABC&quot;]">

我们可以像这样使用一种不错的格式:

<option value="123~ABC">

发布HTML表单时,我们必须能够将此编码的 identity 恢复为原始状态:array('123','ABC')以检索正确的 entity .

最后,如果 identity 包含字母和数字以外的其他字符,那么格式变得复杂(人为读取)是完全可以接受的.

一些基本示例:

'123' => '123'
'ABC' => 'ABC'
array('123','ABC') => '123~ABC'(只是个主意)

'string with non-alphanumeric, even non-àscìì char$' => ?

对于包含其他字符的字符串,任何(或多或少复杂的)表示形式都是可接受的.即使原始字符串包含非ASCII字符,结果字符串也应仅包含ASCII字符.整个过程必须是完全可逆的.

关于如何执行此操作的任何想法?

解决方案

根据您在评论中给出的反馈,我建议您使用 urlencode rawurlencode

然后您可以使用,冒号创建原子组成.

class Identifier {
    static function encode(array $identifier) {
        return implode(', ', array_map('rawurlencode', $identifier));
    }
    static function decode($identifier) {
        return array_map('rawurldecode',
            array_map('trim', explode(',', $identifier))
        );
    }
}

$identifier = array('111', 'abc');
var_dump($identifier);

$encoded = Identifier::encode($identifier);
var_dump($encoded);

$decoded = Identifier::decode($encoded);
var_dump($decoded);

As a follow-up to a previous question where I asked for a solution to a broken problem, I'm trying to find a way to express an arbitrary identifier in a "readable" way.

Context: we are working with entities (domain model objects from DDD), which have an identity. This identity (mapped to a database primary key) can be expressed as a string: '123', 'ABC'.

Some entities can have a compound identity, i.e. composed of two or more other entities' identity: array('123','ABC').

Sometimes, we want to pretty-print this identity, or to use it in a place where just a single string is allowed (for example, in an HTML <option> value). The process has to be predictable and reversible, i.e. there should be no ambiguity in how to reverse it back to its original state.

When we want to human-read this identity, for debugging purposes, it's easier to read 123, ABC, or 123~ABC rather than a:2:{i:0;s:3:"123";i:1;s:3:"ABC";}, that's why we don't want to use built-in functions such as serialize() or json_encode().

json_encode() does a pretty good job, but when it comes to use it in HTML, where quotes have to be properly encoded, it becomes quite unreadable:

<option value="[&quot;123&quot;,&quot;ABC&quot;]">

Where we could use a nice format just like this one:

<option value="123~ABC">

When posting the HTML form, we have to be able to revert this encoded identity to its original state: array('123','ABC') to retrieve the correct entity.

Finally, it is perfectly acceptable that the format becomes complicated to (humanly) read if the identity contains other chars than letters and figures.

Some basic examples:

'123' => '123'
'ABC' => 'ABC'
array('123','ABC') => '123~ABC' (just an idea)

'string with non-alphanumeric, even non-àscìì char$' => ?

Any (more or less complicated) representation is acceptable for strings containing other chars. The resulting string should contain only ASCII chars, even if the original string contains non-ASCII chars. The whole process must be entirely reversible.

Any idea on how to do this?

解决方案

Based on the feedback you gave in the comments I would suggest that you encode identifier-atoms with urlencode or rawurlencode

You can then create atom-composition by using , colons.

class Identifier {
    static function encode(array $identifier) {
        return implode(', ', array_map('rawurlencode', $identifier));
    }
    static function decode($identifier) {
        return array_map('rawurldecode',
            array_map('trim', explode(',', $identifier))
        );
    }
}

$identifier = array('111', 'abc');
var_dump($identifier);

$encoded = Identifier::encode($identifier);
var_dump($encoded);

$decoded = Identifier::decode($encoded);
var_dump($decoded);

这篇关于提供人类可读的标识符表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 11:43