{"action":"created","issue":{"url":"https://api.github.com/repos/fgrosse/PHPASN1/issues/45","labels_url":"https://api.github.com/repos/fgrosse/PHPASN1/issues/45/labels{/name}","comments_url":"https://api.github.com/repos/fgrosse/PHPASN1/issues/45/comments","events_url":"https://api.github.com/repos/fgrosse/PHPASN1/issues/45/events","html_url":"https://github.com/fgrosse/PHPASN1/issues/45","id":95676630,"number":45,"title":"Most efficient way to parse structures?","user":{"login":"afk11","id":5617245,"avatar_url":"https://avatars.githubusercontent.com/u/5617245?v=3","gravatar_id":"","url":"https://api.github.com/users/afk11","html_url":"https://github.com/afk11","followers_url":"https://api.github.com/users/afk11/followers","following_url":"https://api.github.com/users/afk11/following{/other_user}","gists_url":"https://api.github.com/users/afk11/gists{/gist_id}","starred_url":"https://api.github.com/users/afk11/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/afk11/subscriptions","organizations_url":"https://api.github.com/users/afk11/orgs","repos_url":"https://api.github.com/users/afk11/repos","events_url":"https://api.github.com/users/afk11/events{/privacy}","received_events_url":"https://api.github.com/users/afk11/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/fgrosse/PHPASN1/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-07-17T15:13:21Z","updated_at":"2015-07-18T08:30:26Z","closed_at":null,"body":"This library makes it easy to create encoded structures, but when it comes to parsing them, I'm a little unsure about the best way. \r\n\r\nI have been imperatively checking the type of each element in the structure after doing `Object::fromBinary()`.  \r\n\r\nI wondered if it's possible to do anything similar to this, and parse a structure according to a template? If not I might take a look at PRing something for this. \r\n\r\nI notice some of the structures have optional parameters (eg, tbsCertificateInfo), so I'd hope to handle that also. \r\n\r\n```php\r\n$structure = new Sequence(\r\n   new Set(\r\n      new ObjectIdentifier(),\r\n      new Sequence(\r\n         new Integer(),\r\n         new BitString()\r\n      )\r\n   )\r\n);\r\n\r\n$structure->fromBinary($byteString);\r\n```"},"comment":{"url":"https://api.github.com/repos/fgrosse/PHPASN1/issues/comments/122513408","html_url":"https://github.com/fgrosse/PHPASN1/issues/45#issuecomment-122513408","issue_url":"https://api.github.com/repos/fgrosse/PHPASN1/issues/45","id":122513408,"user":{"login":"fgrosse","id":733004,"avatar_url":"https://avatars.githubusercontent.com/u/733004?v=3","gravatar_id":"","url":"https://api.github.com/users/fgrosse","html_url":"https://github.com/fgrosse","followers_url":"https://api.github.com/users/fgrosse/followers","following_url":"https://api.github.com/users/fgrosse/following{/other_user}","gists_url":"https://api.github.com/users/fgrosse/gists{/gist_id}","starred_url":"https://api.github.com/users/fgrosse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fgrosse/subscriptions","organizations_url":"https://api.github.com/users/fgrosse/orgs","repos_url":"https://api.github.com/users/fgrosse/repos","events_url":"https://api.github.com/users/fgrosse/events{/privacy}","received_events_url":"https://api.github.com/users/fgrosse/received_events","type":"User","site_admin":false},"created_at":"2015-07-18T08:30:26Z","updated_at":"2015-07-18T08:30:26Z","body":"So far I recommend defining a class for the thing you want to parse then move the parsing logic into there (including checking types etc) (check [`X509/CertificateSubject`][1] if you haven't already).\r\nThis gives you full error handling capacities, you can handle optional arguments and have full type savety with phpdoc.\r\n\r\nThe idea of defining templates sounds interesting because it can remove the necessity for the boiler plate code. However, the approach you suggested would only work if I would change the constructors to always accept no arguments which I would rather not do. Instead we could have a nested nested array of type identifiers to define such a template.\r\n\r\nIf you have yet another idea on how to traverse parsed objects while maintaining type savety and error handling I am all ears :)\r\n\r\nI was also thinking about writing a ASN.1 compiler which could take a ASN.1 definition and compile php code which uses PHPASN1 from that. This would be a separate repository though.\r\n\r\n[1]: https://github.com/fgrosse/PHPASN1/blob/master/lib/X509/CertificateSubject.php"}}