-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
Changes from 4 commits
028b282
df42153
7737c07
0678f50
68b12c4
45d1ce9
87a3dd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Fhaculty\Graph\Attribute; | ||
|
||
/** | ||
* Implemented by any entity that is aware of additional attributes | ||
* | ||
* Each attribute consists of a name (string) and an arbitrary value. | ||
*/ | ||
interface AttributeAware | ||
{ | ||
/** | ||
* get a single attribute with the given $name (or return $default if attribute was not found) | ||
* | ||
* @param string $name | ||
* @param mixed $default to return if attribute was not found | ||
* @return mixed | ||
*/ | ||
public function getAttribute($name, $default = null); | ||
|
||
/** | ||
* set a single attribute with the given $name to given $value | ||
* | ||
* @param string $name | ||
* @param mixed $value | ||
*/ | ||
public function setAttribute($name, $value); | ||
|
||
/** | ||
* get a container for all attributes | ||
* | ||
* @return AttributeBag | ||
*/ | ||
public function getAttributeBag(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Fhaculty\Graph\Attribute; | ||
|
||
/** | ||
* Interface to container that represents multiple attributes | ||
*/ | ||
interface AttributeBag extends AttributeAware | ||
{ | ||
// public function getAttribute($name); | ||
// public function setAttribute($name, $value); | ||
// public function getAttributeBag(); | ||
|
||
/** | ||
* set an array of additional attributes | ||
* | ||
* @param array $attributes | ||
*/ | ||
public function setAttributes(array $attributes); | ||
|
||
/** | ||
* get an array of all attributes | ||
* | ||
* @return array | ||
*/ | ||
public function getAttributes(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Fhaculty\Graph\Attribute; | ||
|
||
class AttributeBagContainer implements AttributeBag | ||
{ | ||
private $attributes = array(); | ||
|
||
public function getAttribute($name, $default = null) | ||
{ | ||
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; | ||
} | ||
|
||
public function setAttribute($name, $value) | ||
{ | ||
$this->attributes[$name] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function getAttributes() | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
public function setAttributes(array $attributes) | ||
{ | ||
$this->attributes = $attributes + $this->attributes; | ||
|
||
return $this; | ||
} | ||
|
||
public function getAttributeBag() | ||
{ | ||
return $this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Fhaculty\Graph\Attribute; | ||
|
||
/** | ||
* An attribute bag that automatically prefixes a given namespace | ||
*/ | ||
class AttributeBagNamespaced implements AttributeBag | ||
{ | ||
private $bag; | ||
private $prefix; | ||
|
||
public function __construct(AttributeAware $bag, $prefix) | ||
{ | ||
if (!($bag instanceof AttributeBag)) { | ||
$bag = $bag->getAttributeBag(); | ||
} | ||
$this->bag = $bag; | ||
$this->prefix = $prefix; | ||
} | ||
|
||
public function getAttribute($name, $default = null) | ||
{ | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
{ | ||
$this->bag->setAttribute($this->prefix . $name, $value); | ||
} | ||
|
||
public function getAttributes() | ||
{ | ||
$attributes = array(); | ||
$len = strlen($this->prefix); | ||
|
||
foreach ($this->bag->getAttributes() as $name => $value) { | ||
if (strpos($name, $this->prefix) === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this unintentionally remove a prefix if the prefix is purposefully included multiple times using nested attribute bags? For example, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see how this could happen? Care to elaborate or perhaps even provide a test case? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you are correct. I didn't quite grasp the implementation here the first time through, but it looks correct even in weird cases like: $bag = new AttributeBagContainer();
$commitBag = new AttributeBagNamespaced($bag, 'commit.');
$commitDetailBag = new AttributeBagNamespaced($commitBag, 'commit.');
// ... I do believe that something like this could cause problems. They aren't critical problems, but they do allow people to get around the namespacing: $bag = new AttributeBagContainer();
$bag->setAttribute('commit.sha', 'abc123');
$commitBag = new AttributeBagNamespaced($bag, 'commit.');
$commitBag->getAttribute('sha'); // 'abc123' |
||
$attributes[substr($name, $len)] = $value; | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
public function setAttributes(array $attributes) | ||
{ | ||
foreach ($attributes as $name => $value) { | ||
$this->bag->setAttribute($this->prefix . $name, $value); | ||
} | ||
} | ||
|
||
public function getAttributeBag() | ||
{ | ||
return $this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Fhaculty\Graph\Attribute; | ||
|
||
class AttributeBagReference implements AttributeBag | ||
{ | ||
private $attributes; | ||
|
||
public function __construct(array &$attributes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. The The 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. |
||
{ | ||
$this->attributes =& $attributes; | ||
} | ||
|
||
public function getAttribute($name, $default = null) | ||
{ | ||
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; | ||
} | ||
|
||
public function setAttribute($name, $value) | ||
{ | ||
$this->attributes[$name] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function getAttributes() | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
public function setAttributes(array $attributes) | ||
{ | ||
$this->attributes = $attributes + $this->attributes; | ||
|
||
return $this; | ||
} | ||
|
||
public function getAttributeBag() | ||
{ | ||
return $this; | ||
} | ||
} |
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.
Nice idea!