Skip to content

Instantly share code, notes, and snippets.

@nicmart
Last active December 11, 2015 16:08
Show Gist options
  • Save nicmart/4625632 to your computer and use it in GitHub Desktop.
Save nicmart/4625632 to your computer and use it in GitHub Desktop.
I have to choose the more elegant interface for selectors for the treebuilder library. Here I post some alternatives to se how they look like.
<?php
// Current interface
$tree
->leaf('element', 'author')
->key('value', 'author_name')
->value('element', 'name')
->end()
->leaf('element', 'date')
->value(function(\DateTime $dateTime) { return $dateTime->format('d-m-Y'); })
->key('value', 'born in')
->end()
->leaf()
->key('value', 'short description')
->value(function($book) {
$date = $book['date'];
return sprintf('"%s": a book by %s written in %s',
$book['title'],
$book['author']['name'],
$book['date']->format('Y')
);
})
->end()
->each('element', 'related')
->key('value', 'related titles')
->leaf()
->value('element', 'title')
->end()
->end()
;
<?php
// default PA but no resolver
$tree
->leaf('author')
->key(new Value('author_name'))
->value('element', 'name')
->end()
->leaf('date')
->value(function(\DateTime $dateTime) { return $dateTime->format('d-m-Y'); })
->key(new Value('born in'))
->end()
->leaf()
->key(new Value('short description'))
->value(function($book) {
$date = $book['date'];
return sprintf('"%s": a book by %s written in %s',
$book['title'],
$book['author']['name'],
$book['date']->format('Y')
);
})
->end()
->each('related')
->key(new Value('related titles'))
->leaf()
->value('title')
->end()
->end()
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment