Provide a human-readable representation of an identifier?

情到浓时终转凉″ 提交于 2019-12-24 11:05:52

问题


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?


回答1:


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);



回答2:


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

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




回答3:


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.



来源:https://stackoverflow.com/questions/6444404/provide-a-human-readable-representation-of-an-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!