-
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add general purpose Attributes, replace Layoutable #103
Conversation
{ | ||
private $attributes; | ||
|
||
public function __construct(array &$attributes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this by ref. This will be confusing. Say
$ar['color'] = 'red';
$a_attr = new AttributeBagReference($a);
$ar['color'] = 'blue';
$b_attr = new AttributeBagReference($a);
if ($a_attr->getAttribute('color') == $b_attr->getAttribute('color'))
{
echo "Weird";
}
Or did I miss something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AttributeBagRef
is designed to be a lightweight temporary instance so that we can expose the attributes through the AttributeBag
interface without actually storing the instance. I.e. every time you call $vertex->getAttributeBag()
you get another instance (which is just an implementation detail), but you can still access all properties nonetheless.
The AttributeBagContainer
has it's own internal set of attributes and could be used as an alternative to the AttributeBagRef
. Using it instead would require every Vertex to have (or at least lazy-load) a dedicated instance and then implement it like this:
class Vertex
{
public function getAttribute($name)
{
return $this->getAttributeBag()->getAttribute($name);
}
}
I suspected this to be inefficient (additional instances, more references, forwarded method calls etc.), but we should probably run some benchmarks first.
The code needs some more documentation but nice PR. I have no time to test this right now but will within a month. |
* | ||
* @return string[] | ||
*/ | ||
public function getNames(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not yet decided how useful a getNames()
method actually is. It causes a bit of code duplication and it's the only method without "attribute" in its name.
Personally, I'd tend towards dropping it completely, but I'd love to hear some other opinions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. array_keys($bag->getAttributes())
seems useful enough to me?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely, I guess it's safe to just drop this altogether.
Thanks for your feedback :) Yeah, I'm looking towards adding more documentation, but I felt it's already in a state to receive some feedback. Any input is welcome! |
{ | ||
private $attributes = array(); | ||
|
||
public function getAttribute($name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding a $default = null
parameter to this method could be useful. That would allow the user to override the value returned if the attribute doesn't exist. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes perfect sense, thanks for the suggestion!
I'm definitely interested in seeing this feature added - is there anything that could be done to get this moving any faster (e.g., contributing documentation)? 👍 |
Thanks @nubs, your feedback is very much appreciated and certainly helps pushing this further! Contributing documentation is appreciated just as much. Feel free to your our IRC channel (#graphp on freenode.net) or directly file a PR 👍 |
Besides the obvious lack of documentation, there's two more things I'd like to address before this can be merged:
Any thoughts? |
I don't particularly like Why not implement ArrayAccess and optionally Iterator? That would help solve the naming issues, unsetting attributes, and the handling of null vs not set (via I'm not sure how that would play out with namespaced attributes, so some more thought needs to be given there. Something like guzzles collections and particular |
return $this->bag->getAttribute($this->prefix . $name, $default); | ||
} | ||
|
||
public function setAttribute($name, $value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The set
methods in this class don't return $this
for the fluent interface like the other implementations of AttributeBag
do. Is that on purpose? Should the fluent interface be part of the AttributeAware
and AttributeBag
interfaces?
PHPDoc is fussy and doesn't want to allow nice inheritance of the documentation, so there is a fair bit of copy/paste here. The problems seemed to come down to interface inheritance perhaps not being fully supported. I think the class documentation is the most important bit here, the rest is just to help out IDEs and phpdoc generation to save user's a little effort. I'm not sure that the AttributeBagNamespaced not returning a fluent interface for its set* methods is correct, but I documented it the way it was implemented for now.
Update documentation of attributes
I agree on the postfix Array (just 2cents :-/) |
This hasn't really changed ever since this has been raised. I agree on your comments that the suggested names aren't really better either. So I'm all for leaving this as-is for the moment and change the API once we come up with a better name.
This makes perfect sense on the |
Add general purpose Attributes, replace Layoutable
v0.8.0 of the graph library added the ability to store attributes on vertices/edges, so we can use that rather than having to store a parallel commit information data structure. This makes things much simpler :)
Use graphp/graph#103 to simplify code.
Attributes
Layoutable
Fixes #100
Supersedes #40
Fixes #84
Unblocks #97