Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 11 commits
  • 17 files changed
  • 0 commit comments
  • 1 contributor
View
2 README.md
@@ -216,7 +216,7 @@ use Gignite\TheCure\Mapper\Container;
class User extends MagicModel {
- public static function fields()
+ public static function attributes()
{
return array(
'name' => new Field('name'),
View
71 classes/Gignite/TheCure/Attribute.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * An attribute of a model
+ *
+ * // Enable setter method functionality
+ * new Attribute('name')
+ *
+ * // Provide an alias
+ * new Attribute('location', array('alias' => 'town'));
+ *
+ * @package TheCure
+ * @category Attribute
+ * @copyright Gignite, 2012
+ */
+namespace Gignite\TheCure;
+
+class Attribute {
+
+ protected $name;
+
+ protected $alias;
+
+ /**
+ * Create a new Attribute.
+ *
+ * @param string field name (as in database)
+ * @param array additional config
+ * @return void
+ */
+ public function __construct($name, array $config = NULL)
+ {
+ $this->name = $name;
+
+ if ($config)
+ {
+ foreach ($config as $_k => $_v)
+ {
+ if (property_exists($this, $_k))
+ {
+ $this->{$_k} = $_v;
+ }
+ }
+ }
+ }
+
+ /**
+ * Get field name.
+ *
+ * @return string
+ */
+ public function name()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Alias name.
+ *
+ * @return string
+ */
+ public function alias()
+ {
+ if ($this->alias)
+ {
+ return $this->alias;
+ }
+
+ return $this->name();
+ }
+
+}
View
57 classes/Gignite/TheCure/Field.php
@@ -2,62 +2,29 @@
/**
* A field
*
- * // Enable setter method functionality
- * new Field('name', array('setter' => TRUE))
+ * // A field with a name of "name"
+ * new Field('name');
+ *
+ * // A field with a name of "location" but aliased to "town"
+ * new Field('name', array('alias' => 'town'));
*
* // Provide a default value
* new Field('verified', array('value' => FALSE));
*
* @package TheCure
+ * @category Attribute
* @category Field
* @copyright Gignite, 2012
*/
namespace Gignite\TheCure;
-class Field {
-
- protected $name;
+class Field extends Attribute {
protected $value;
- protected $setter;
-
protected $rules;
/**
- * Create a new field.
- *
- * @param string field name
- * @param array additional config
- * @return void
- */
- public function __construct($name, array $config = NULL)
- {
- $this->name = $name;
-
- if ($config)
- {
- foreach ($config as $_k => $_v)
- {
- if (property_exists($this, $_k))
- {
- $this->{$_k} = $_v;
- }
- }
- }
- }
-
- /**
- * Get field name.
- *
- * @return string
- */
- public function name()
- {
- return $this->name;
- }
-
- /**
* Get default value.
*
* @return mixed
@@ -68,16 +35,6 @@ public function value()
}
/**
- * Is this field a setter?
- *
- * @return boolean
- */
- public function is_setter()
- {
- return $this->setter;
- }
-
- /**
* Get field rules.
*
* @return array
View
92 classes/Gignite/TheCure/Models/Magic.php
@@ -21,7 +21,7 @@
* @static
* @return array
*/
- public static function fields()
+ public static function attributes()
{
return array();
}
@@ -46,63 +46,84 @@ public function __container(Container $container = NULL)
}
/**
- * @param $fields
- * @param $method
- * @param array|null $args
- * @return array
+ * @param array of attributes
+ * @param string method called
+ * @return Attribute
*/
- private function relation_action($fields, $method, array $args = NULL)
+ private function attribute(array $attributes, $method)
{
- if (isset($fields[$method]) AND $args)
+ foreach ($attributes as $_attr)
{
- $verb = 'Set';
- $key = $method;
+ if ($_attr->alias() === $method)
+ {
+ return $_attr;
+ }
+ }
+ }
+
+ /**
+ * @param $fields
+ * @param $method
+ * @param array|null $args
+ * @return array
+ */
+ private function relationship(array $attributes, $method, array $args)
+ {
+ if ($relationship = $this->attribute($attributes, $method))
+ {
+ if ($args)
+ {
+ $verb = 'set';
+ }
+ else
+ {
+ $verb = 'find';
+ $args[0] = $this->__object()->get($relationship->name());
+ }
}
else
{
$verb = current(explode('_', $method));
- $key = substr($method, strlen($verb) + 1);
+ $relationship = $this->attribute(
+ $attributes,
+ substr($method, strlen($verb) + 1));
}
+ $arg = current($args);
+
$interface = ucfirst($verb);
$interface = "Gignite\\TheCure\\Relation\\{$interface}";
if (interface_exists($interface)
- AND isset($fields[$key])
- AND $fields[$key] instanceOf $interface)
+ AND $relationship
+ AND $relationship instanceOf $interface)
{
- return array($fields[$key], $verb);
+ return $relationship->{$verb}($this->__container(), $this, $arg);
}
+
+ return FALSE;
}
/**
- * @param string $method
- * @param array $args
- * @return mixed|null
- * @throws \BadMethodCallException
+ * @param string $method
+ * @param array $args
+ * @return mixed|null
+ * @throws \BadMethodCallException
*/
public function __call($method, $args)
{
- $fields = static::fields();
+ $attributes = static::attributes();
$object = $this->__object();
+ $relationship = $this->relationship($attributes, $method, $args);
- if ($field_action = $this->relation_action($fields, $method, $args))
+ if ($relationship !== FALSE)
{
- list($field, $action) = $field_action;
- $field->{$action}($this->__container(), $this, $args[0]);
+ return $relationship;
}
- elseif (isset($fields[$method]))
+ elseif ($field = $this->attribute($attributes, $method))
{
- $field = $fields[$method];
-
if ($args)
{
- if ( ! $field->is_setter())
- {
- throw new \BadMethodCallException(
- 'You cannot pass arguments to a non-setter field.');
- }
-
$object->{$field->name()} = $args[0];
return;
}
@@ -121,19 +142,12 @@ public function __call($method, $args)
$value = NULL;
}
- if ($field instanceOf Relationship)
- {
- return $field->find($this->__container(), $value);
- }
- else
- {
- return $value;
- }
+ return $value;
}
}
else
{
- throw new \BadMethodCallException;
+ throw new \BadMethodCallException('Method: '.$method);
}
}
View
4 classes/Gignite/TheCure/Relation/Add.php
@@ -16,10 +16,10 @@
/**
* @abstract
* @param Container $container
- * @param $object
+ * @param $model
* @param $relation
* @return mixed
*/
- public function add(Container $container, $object, $relation);
+ public function add(Container $container, $model, $relation);
}
View
24 classes/Gignite/TheCure/Relation/Find.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Find relation interface
+ *
+ * @package TheCure
+ * @category Relation
+ * @category Relationships
+ * @copyright Gignite, 2012
+ */
+namespace Gignite\TheCure\Relation;
+
+use Gignite\TheCure\Mapper\Container;
+
+interface Find {
+
+ /**
+ * @abstract
+ * @param Container $container
+ * @param $value
+ * @return mixed
+ */
+ public function find(Container $container, $model, $value);
+
+}
View
4 classes/Gignite/TheCure/Relation/Remove.php
@@ -16,10 +16,10 @@
/**
* @abstract
* @param Container $container
- * @param $object
+ * @param $model
* @param $relation
* @return mixed
*/
- public function remove(Container $container, $object, $relation);
+ public function remove(Container $container, $model, $relation);
}
View
4 classes/Gignite/TheCure/Relation/Set.php
@@ -16,10 +16,10 @@
/**
* @abstract
* @param Container $container
- * @param $object
+ * @param $model
* @param $relation
* @return mixed
*/
- public function set(Container $container, $object, $relation);
+ public function set(Container $container, $model, $relation);
}
View
9 classes/Gignite/TheCure/Relationships/OneToMany.php
@@ -12,18 +12,21 @@
use Gignite\TheCure\Mapper\Container;
use Gignite\TheCure\Relation;
-class OneToMany extends Relationship implements Relation\Add, Relation\Remove {
+class OneToMany extends Relationship
+ implements Relation\Find, Relation\Add, Relation\Remove {
/**
* @param Container $container
* @param $ids
* @return mixed
*/
- public function find(Container $container, $ids)
+ public function find(Container $container, $model, $ids)
{
- return $this->mapper($container)->find($this->model_suffix(), array(
+ $find = $this->mapper($container)->find($this->model_suffix(), array(
'_id' => array('$in' => $ids),
));
+
+ return $find;
}
/**
View
5 classes/Gignite/TheCure/Relationships/OneToOne.php
@@ -12,14 +12,15 @@
use Gignite\TheCure\Mapper\Container;
use Gignite\TheCure\Relation;
-class OneToOne extends Relationship implements Relation\Set, Relation\Remove {
+class OneToOne extends Relationship
+ implements Relation\Find, Relation\Set, Relation\Remove {
/**
* @param Container $container
* @param $id
* @return mixed
*/
- public function find(Container $container, $id)
+ public function find(Container $container, $model, $id)
{
return $this->mapper($container)->find_one($this->model_suffix(), $id);
}
View
46 classes/Gignite/TheCure/Relationships/Relationship.php
@@ -3,54 +3,22 @@
* A relationship between models
*
* @package TheCure
- * @category Field
+ * @category Attribute
* @category Relationships
* @copyright Gignite, 2012
*/
namespace Gignite\TheCure\Relationships;
-use Gignite\TheCure\Field;
+use Gignite\TheCure\Attribute;
use Gignite\TheCure\Mapper\Container;
-abstract class Relationship {
-
- protected $name;
+abstract class Relationship extends Attribute {
protected $mapper_suffix;
protected $model_suffix;
/**
- * Create a new relationship.
- *
- * @param $name Relationship name
- * @param array|null $config additional config
- */
- public function __construct($name, array $config = NULL)
- {
- $this->name = $name;
-
- if ($config)
- {
- foreach ($config as $_k => $_v)
- {
- if (property_exists($this, $_k))
- {
- $this->{$_k} = $_v;
- }
- }
- }
- }
-
- /**
- * @return string Relationship name
- */
- public function name()
- {
- return $this->name;
- }
-
- /**
* @return mixed
*/
protected function model_suffix()
@@ -75,12 +43,4 @@ protected function mapper(Container $container)
return $container->mapper($this->mapper_suffix());
}
- /**
- * @abstract
- * @param Container $container
- * @param $value
- * @return mixed
- */
- abstract public function find(Container $container, $value);
-
}
View
13 test/unit/classes/Gignite/TheCure/Models/User/Magic.php
@@ -7,14 +7,15 @@
class Magic extends MagicModel {
- public static function fields()
+ public static function attributes()
{
- return parent::fields() + array(
- 'name' => new Field('name', array('setter' => TRUE)),
- 'age' => new Field('age', array('value' => 1)),
- 'friends' => new OneToMany('friends', array(
+ return parent::attributes() + array(
+ new Field('name'),
+ new Field('location', array('alias' => 'town')),
+ new Field('age', array('value' => 1)),
+ new OneToMany('friends', array(
'mapper_suffix' => 'User',
- 'model_suffix' => 'Magic',
+ // 'model_suffix' => 'Magic',
)),
);
}
View
4 test/unit/classes/Gignite/TheCure/Models/User/MockableRelation.php
@@ -8,9 +8,9 @@ class MockableRelation extends MagicModel {
public static $relation;
- public static function fields()
+ public static function attributes()
{
- return parent::fields() + array(
+ return parent::attributes() + array(
'relation' => call_user_func(static::$relation),
);
}
View
4 test/unit/classes/Gignite/TheCure/Relationships/Mock.php
@@ -6,7 +6,7 @@
use Gignite\TheCure\Relation;
class Mock extends Relationship
- implements Relation\Add, Relation\Remove, Relation\Set {
+ implements Relation\Find, Relation\Add, Relation\Remove, Relation\Set {
protected $method_called;
@@ -15,7 +15,7 @@ public function method_called()
return $this->method_called;
}
- public function find(Container $container, $value)
+ public function find(Container $container, $object, $value)
{
$this->method_called = 'find';
}
View
28 test/unit/classes/Gignite/TheCure/Specs/Models/ModelMagic.php
@@ -69,14 +69,6 @@ public function testMagicCall($model, $expectedName, $args)
$this->assertSame($expectedName, $model->name());
}
- /**
- * @expectedException BadMethodCallException
- */
- public function testItShouldThrowBadMethodCallExceptionWhenNoSetter()
- {
- $this->user('Jim')->age(22);
- }
-
public function testItShouldUseDefaultValueWhenNoValueSet()
{
$this->assertSame(1, $this->user('Jim')->age());
@@ -139,5 +131,25 @@ public function testItShouldThrowBadMethodCallException()
$model->unknown();
}
+ public function provideAccessorMethods()
+ {
+ return array(
+ array('name', 'name', 'Luke'),
+ array('location', 'town', 'Braintree'),
+ );
+ }
+
+ /**
+ * @dataProvider provideAccessorMethods
+ */
+ public function testItShouldUseAccessorMethodName($name, $alias, $value)
+ {
+ $model = new Models\User\Magic;
+ $model->__object(new Object(array(
+ $name => $value,
+ )));
+ $this->assertSame($value, $model->{$alias}());
+ }
+
}
View
2 test/unit/classes/Gignite/TheCure/Specs/Relationships/OneToMany.php
@@ -26,7 +26,7 @@ public function testItShouldFindACollectionOfRelatedModels()
{
$container = $this->container();
$container->mapper('User')->save(new Models\User\Admin);
- $collection = $this->relationship()->find($container, array(0, 1));
+ $collection = $this->relationship()->find($container, NULL, array(0, 1));
$this->assertInstanceOf(
'Gignite\TheCure\Collections\Collection',
$collection);
View
2 test/unit/classes/Gignite/TheCure/Specs/Relationships/OneToOne.php
@@ -26,7 +26,7 @@ public function testItShouldFindARelatedModel()
{
$container = $this->container();
$container->mapper('User')->save(new Models\User\Admin);
- $collection = $this->relationship()->find($container, 0);
+ $collection = $this->relationship()->find($container, NULL, 0);
$this->assertInstanceOf('Gignite\TheCure\Models\User\Admin', $collection);
}

No commit comments for this range

Something went wrong with that request. Please try again.