开发者

Provide a human-readable representation of an identifier?

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 result开发者_C百科ing 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);


str_replace( array('[',']','&quot;',',') ,
             array('','','','~'),
            json_encode($stuff)
);

Your questions is utterly verbous and doenst' explain what you really want to achive.


You can use 2 special characters:

~ - delimiter

* - escape character (to escape a delimiter or an escape character itself)

Examples:

array('123','ABC') => 123~ABC
array('12*3','A~BC') => 12**3~A*~BC

You can choose different characters for delimiter and escape character. If selected characters will be rarely usable then the string usually will be well readable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜