Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

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.
base fork: Gignite/the-cure
base: fb284f7000
...
head fork: Gignite/the-cure
compare: 75060448fa
  • 14 commits
  • 20 files changed
  • 0 commit comments
  • 1 contributor
View
45 README.md
@@ -202,7 +202,7 @@ $model = $container->mapper('Profile')->find_one($id);
## Magic
Usually I try to avoid magic code but expressing fields and
-relationships is repetative and I believe some so-called magic
+relationships is repetitive and I believe some so-called magic
is acceptable in specialised areas.
``` php
@@ -232,6 +232,8 @@ class User extends MagicModel {
$user = new User;
$user->__container(new Container('Mock'));
+// Or try with mongo!!
+// $user->__container(new Container('Mongo'));
$user->name('Luke');
var_dump($user->name());
@@ -239,9 +241,50 @@ $bob = new User;
$bob->name('Bob');
$user->add_friends($bob);
var_dump($user->friends()->current()->name());
+var_dump($user->friends()->count());
$user->remove_friends($bob);
var_dump($user->friends()->count());
?>
```
+## Object, a data transfer object
+
+In order to transfer data between models and mappers we have
+devised a DTO called `Object`.
+
+### Creating an Object
+
+`Object::__construct()` optionally takes two arguments. The
+first is an array of data. The second a whitelist of keys to
+extract from the first arg.
+
+``` php
+<?php
+$object = new Object(array('name' => 'Luke'));
+
+// Or filter your data
+$object = new Object($_POST, array('name'));
+?>
+```
+
+### Updating an Object
+
+``` php
+<?php
+$object->name = 'Jim';
+$object->set('name', 'Jim');
+$object->accessor('name', 'Jim');
+?>
+```
+
+### Getting data from an Object
+
+``` php
+<?php
+var_dump($object->name);
+var_dump($object->get('name'));
+var_dump($object->accessor('name'));
+var_dump($object->as_array());
+?>
+```
View
10 classes/Gignite/TheCure/Collections/Model.php
@@ -15,6 +15,7 @@
namespace Gignite\TheCure\Collections;
use Gignite\TheCure\IdentityMap;
+use Gignite\TheCure\Object;
class Model extends Iterable {
@@ -22,6 +23,7 @@ class Model extends Iterable {
protected $class_name;
+
public function __construct(
$collection,
IdentityMap $identities,
@@ -57,9 +59,13 @@ public function current()
}
else
{
+ if ( ! $object instanceOf Object)
+ {
+ $object = new Object($object);
+ }
+
$model = new $class;
- $model->__object((object) $object);
-
+ $model->__object($object);
$this->identities()->set($model);
}
View
6 classes/Gignite/TheCure/Mapper/Container.php
@@ -93,11 +93,7 @@ protected function mapper_config()
{
$mappers = $this->config('mappers');
$key = $this->type();
-
- if (isset($mappers[$key]))
- {
- return $mappers[$key];
- }
+ return $mappers[$key];
}
protected function connection()
View
7 classes/Gignite/TheCure/Mappers/Mongo.php
@@ -12,6 +12,7 @@
*/
namespace Gignite\TheCure\Mappers;
+use Gignite\TheCure\Object;
use Gignite\TheCure\Connections\Connection;
use Gignite\TheCure\Mapper\ConnectionSetGet;
use Gignite\TheCure\Mappers\Mapper;
@@ -64,7 +65,7 @@ public function find_one($suffix = NULL, $where = NULL)
$where,
function ($where) use ($collection)
{
- return (object) $collection->findOne($where);
+ return new Object($collection->findOne($where));
});
}
@@ -77,7 +78,7 @@ public function save(Model $model)
$model,
function ($object) use ($collection, $options)
{
- $array = (array) $object;
+ $array = $object->as_array();
if (isset($object->_id))
{
@@ -91,7 +92,7 @@ function ($object) use ($collection, $options)
$collection->insert($array, $options);
}
- return (object) $array;
+ return new Object($array);
});
}
View
6 classes/Gignite/TheCure/Models/Model.php
@@ -23,6 +23,8 @@
*/
namespace Gignite\TheCure\Models;
+use Gignite\TheCure\Object;
+
abstract class Model {
protected $__object;
@@ -33,13 +35,13 @@
* @param StdClass If setting pass in a StdClass
* @return StdClass If getting
*/
- public function __object($object = NULL)
+ public function __object(Object $object = NULL)
{
if ($object === NULL)
{
if ($this->__object === NULL)
{
- $this->__object = new \StdClass;
+ $this->__object = new Object;
}
return $this->__object;
View
84 classes/Gignite/TheCure/Object.php
@@ -0,0 +1,84 @@
+<?php
+/**
+ * A data transfer object
+ *
+ * @package TheCure
+ * @category Object
+ * @copyright Gignite, 2012
+ */
+namespace Gignite\TheCure;
+
+class Object {
+
+ protected $data;
+
+ public function __construct(array $data = NULL, array $filter = NULL)
+ {
+ if ($filter)
+ {
+ $clean_data = array();
+
+ foreach ($filter as $_field)
+ {
+ if (array_key_exists($_field, $data))
+ {
+ $clean_data[$_field] = $data[$_field];
+ }
+ }
+
+ $data = $clean_data;
+ }
+
+ if ($data === NULL)
+ {
+ $data = array();
+ }
+
+ $this->data = $data;
+ }
+
+ public function get($field)
+ {
+ if (isset($this->data[$field]))
+ {
+ return $this->data[$field];
+ }
+ }
+
+ public function set($field, $value)
+ {
+ $this->data[$field] = $value;
+ }
+
+ public function accessor($field, $value = NULL)
+ {
+ if ($value)
+ {
+ $this->set($field, $value);
+ return;
+ }
+
+ return $this->get($field);
+ }
+
+ public function as_array()
+ {
+ return $this->data;
+ }
+
+ public function __isset($field)
+ {
+ return isset($this->data[$field]);
+ }
+
+ public function __get($field)
+ {
+ return $this->get($field);
+ }
+
+ public function __set($field, $value)
+ {
+ return $this->set($field, $value);
+ }
+
+}
View
15 classes/Gignite/TheCure/Relationships/OneToMany.php
@@ -33,12 +33,17 @@ public function add(Container $container, $model, $relation)
$object = $model->__object();
- if ( ! isset($object->{$this->name()}))
+ if (isset($object->{$this->name()}))
{
- $object->{$this->name()} = array();
+ $relations = $object->{$this->name()};
+ }
+ else
+ {
+ $relations = array();
}
- $object->{$this->name()}[] = $relation->__object()->_id;
+ $relations[] = $relation->__object()->_id;
+ $object->{$this->name()} = $relations;
}
public function remove(Container $container, $model, $relation)
@@ -53,7 +58,9 @@ public function remove(Container $container, $model, $relation)
{
if ($_id == $relation->__object()->_id)
{
- unset($model_object->{$this->name()}[$_k]);
+ $relations = $model_object->{$this->name()};
+ unset($relations[$_k]);
+ $model_object->{$this->name()} = $relations;
return;
}
}
View
2  config/thecure.php
@@ -17,7 +17,7 @@
'profiling' => TRUE,
'safe' => TRUE,
),
- 'connection_class' => 'Gignite\Connections\Mongo',
+ 'connection_class' => 'Gignite\TheCure\Connections\Mongo',
),
),
);
View
1  phpunit.xml
@@ -19,6 +19,7 @@
<file>test/ModelMagicTest.php</file>
<file>test/RelationshipOneToOneTest.php</file>
<file>test/RelationshipOneToManyTest.php</file>
+ <file>test/ObjectTest.php</file>
</testsuite>
</testsuites>
<filter>
View
65 smoke.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * Smoke test
+ *
+ * @package TheCure
+ * @category Test
+ * @author Luke Morton
+ * @copyright Gignite, 2012
+ */
+namespace Models;
+
+define('EXT', '.php');
+define('APPPATH', __DIR__.'/test/');
+define('SYSPATH', APPPATH.'/system/');
+
+error_reporting(E_ALL | E_STRICT);
+
+require SYSPATH.'classes/Kohana/Core.php';
+require SYSPATH.'classes/Kohana.php';
+
+spl_autoload_register(array('Kohana', 'auto_load'));
+
+\I18n::lang('en-gb');
+
+\Kohana::$config = new \Kohana_Config;
+\Kohana::$config->attach(new \Config_File);
+
+\Kohana::modules(array('the-cure' => __DIR__.'/'));
+
+use Gignite\TheCure\Models\Magic as MagicModel;
+use Gignite\TheCure\Field;
+use Gignite\TheCure\Relationships\OneToMany as OneToManyRelationship;
+use Gignite\TheCure\Mapper\Container;
+
+class User extends MagicModel {
+
+ public static function fields()
+ {
+ return array(
+ 'name' => new Field('name'),
+ 'age' => new Field('age'),
+ 'friends' => new OneToManyRelationship('friends', array(
+ 'mapper_suffix' => 'User',
+ // 'model_suffix' => 'Admin',
+ )),
+ );
+ }
+
+}
+
+$user = new User;
+$user->__container(new Container('Mock'));
+// Or try with mongo!!
+// $user->__container(new Container('Mongo'));
+$user->name('Luke');
+var_dump($user->name());
+
+$bob = new User;
+$bob->name('Bob');
+$user->add_friends($bob);
+var_dump($user->friends()->current()->name());
+var_dump($user->friends()->count());
+
+$user->remove_friends($bob);
+var_dump($user->friends()->count());
View
5 test/CollectionModelTest.php
@@ -12,6 +12,7 @@
* @license MIT
*/
use Gignite\TheCure\IdentityMap;
+use Gignite\TheCure\Object;
use Gignite\TheCure\Collections\Model as ModelCollection;
class CollectionModelTest extends CollectionTest {
@@ -20,8 +21,8 @@ public function testConstruct()
{
return new ModelCollection(
new ArrayIterator(array(
- (object) array('_id' => 0),
- (object) array('_id' => 1),
+ new Object(array('_id' => 0)),
+ new Object(array('_id' => 1)),
)),
$this->map = new IdentityMap,
'Models\User');
View
3  test/IdentityMapTest.php
@@ -11,6 +11,7 @@
* @license MIT
*/
use Gignite\TheCure\IdentityMap;
+use Gignite\TheCure\Object;
class IdentityMapTest extends PHPUnit_Framework_TestCase {
@@ -19,7 +20,7 @@ class IdentityMapTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
$this->domain = new Models\User;
- $this->domain->__object((object) array('_id' => 2));
+ $this->domain->__object(new Object(array('_id' => 2)));
}
public function testConstruct()
View
5 test/MapperMockTest.php
@@ -12,6 +12,7 @@
*/
use Gignite\TheCure\Factory;
use Gignite\TheCure\IdentityMap;
+use Gignite\TheCure\Object;
class MapperMockTest extends MapperTest {
@@ -34,10 +35,10 @@ protected static function mapper()
protected static function prepareData()
{
$mapper = static::mapper();
- $mapper->data[] = $data = (object) array(
+ $mapper->data[] = $data = new Object(array(
'_id' => 0,
'name' => 'Luke',
- );
+ ));
return $data;
}
View
3  test/MapperMongoTest.php
@@ -12,6 +12,7 @@
*/
use Gignite\TheCure\Factory;
use Gignite\TheCure\IdentityMap;
+use Gignite\TheCure\Object;
use Gignite\TheCure\Connections\Mongo as MongoConnection;
class MapperMongoTest extends MapperTest {
@@ -48,7 +49,7 @@ protected static function prepareData()
$collection = static::collection(static::db());
$data = array('name' => 'Luke');
$collection->insert($data);
- return (object) $data;
+ return new Object($data);
}
protected static function mapper()
View
9 test/MapperTest.php
@@ -1,4 +1,5 @@
<?php
+use Gignite\TheCure\Object;
abstract class MapperTest extends PHPUnit_Framework_TestCase {
@@ -72,7 +73,7 @@ public function testFindNone()
public function providerTestSave()
{
$model = new Models\User;
- $model->__object((object) array('name' => 'Luke'));
+ $model->__object(new Object(array('name' => 'Luke')));
return array(
array($model),
@@ -107,7 +108,7 @@ public function testDeleteWithModel()
$expectedCount = $mapper->find()->count();
$model = new Models\User;
- $model->__object((object) array('name' => 'Luke'));
+ $model->__object(new Object(array('name' => 'Luke')));
$mapper->save($model);
$this->assertSame($expectedCount + 1, $mapper->find()->count());
@@ -125,7 +126,7 @@ public function testDeleteWithCollection()
$query = array('name' => 'Bob');
$bobObject = function ()
{
- return (object) array('name' => 'Bob');
+ return new Object(array('name' => 'Bob'));
};
$expectedCount = $mapper->find($query)->count();
@@ -154,7 +155,7 @@ public function testDeleteWithQuery()
$query = array('name' => 'Jim');
$bobObject = function ()
{
- return (object) array('name' => 'Jim');
+ return new Object(array('name' => 'Jim'));
};
$expectedCount = $mapper->find($query)->count();
View
9 test/ModelMagicTest.php
@@ -1,4 +1,5 @@
<?php
+use Gignite\TheCure\Object;
use Gignite\TheCure\Mapper\Container;
class ModelMagicTest extends PHPUnit_Framework_TestCase {
@@ -13,17 +14,17 @@ protected function container()
$jim = new Models\User\Magic;
$jim->__container($container);
- $jim->__object((object) array(
+ $jim->__object(new Object(array(
'name' => 'Jim',
- ));
+ )));
$container->mapper('User')->save($jim);
$luke = new Models\User\Magic;
$luke->__container($container);
- $luke->__object((object) array(
+ $luke->__object(new Object(array(
'name' => 'Luke',
'friends' => array($jim->__object()->_id),
- ));
+ )));
$container->mapper('User')->save($luke);
$this->container = $container;
View
2  test/ModelTest.php
@@ -5,7 +5,7 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public function testDefaultObjectIsStdClass()
{
$model = new Models\User;
- $this->assertInstanceOf('StdClass', $model->__object());
+ $this->assertInstanceOf('Gignite\TheCure\Object', $model->__object());
}
}
View
35 test/ObjectTest.php
@@ -0,0 +1,35 @@
+<?php
+use Gignite\TheCure\Object;
+
+class ObjectTest extends PHPUnit_Framework_TestCase {
+
+ public function testItShouldFilterArray()
+ {
+ $data = array('name' => 'Luke', 'age' => 22);
+ $filter = array('name');
+ $object = new Object($data, $filter);
+ $expected = array('name' => 'Luke');
+ $this->assertSame($expected, $object->as_array());
+ }
+
+ public function testItShouldReturnNullIfFieldNotSet()
+ {
+ $object = new Object;
+ $this->assertNull($object->get('unknown'));
+ }
+
+ public function testItShouldGetViaAccessor()
+ {
+ $object = new Object(array('name' => 'Luke'));
+ $this->assertSame('Luke', $object->accessor('name'));
+ }
+
+ public function testItShouldSetViaAccessor()
+ {
+ $object = new Object(array('name' => 'Luke'));
+ $object->accessor('name', 'Jake');
+ $this->assertSame('Jake', $object->name);
+ }
+
+}
+
View
24 test/RelationshipOneToManyTest.php
@@ -1,4 +1,5 @@
<?php
+use Gignite\TheCure\Object;
use Gignite\TheCure\Mapper\Container;
use Gignite\TheCure\Relationships\OneToMany;
@@ -28,18 +29,27 @@ public function testItShouldFindACollectionOfRelatedModels()
$collection);
}
+ protected function relation()
+ {
+ $relation = new Models\User\Admin;
+ $relationObject = new Object(array('name' => 'Luke'));
+ $relation->__object($relationObject);
+ return $relation;
+ }
+
public function testItShouldSaveAnRelatedObjectWhenAddingRelation()
{
$container = $this->container();
$model = new Models\User\Admin;
- $relation = new Models\User\Admin;
- $relationObject = (object) array('name' => 'Luke');
- $relation->__object($relationObject);
+ $relation = $this->relation();
+ $relationObject = $relation->__object();
+ $anotherRelation = $this->relation();
$relationship = $this->relationship();
$relationship->add($container, $model, $relation);
+ $relationship->add($container, $model, $anotherRelation);
$userData = $container->mapper('User')->data;
$this->assertSame($relationObject, current($userData));
@@ -56,7 +66,7 @@ public function testItShouldAddAnObjectIDToAnotherObjectsArray($args)
$modelObject = $model->__object();
$relationshipName = $this->relationship()->name();
- $this->assertSame(1, count($modelObject->{$relationshipName}));
+ $this->assertSame(2, count($modelObject->{$relationshipName}));
return array($container, $model, $relation);
}
@@ -72,7 +82,7 @@ public function testItShouldRemoveAnObjectIDFromAnotherObjectsArray($args)
$relationship->remove($container, $model, $relation);
$modelObject = $model->__object();
- $this->assertSame(0, count($modelObject->{$relationship->name()}));
+ $this->assertSame(1, count($modelObject->{$relationship->name()}));
}
/**
@@ -94,9 +104,9 @@ public function testItShouldThrowExceptionWhenRelatedObjectIDNotInArray()
$relationship = $this->relationship();
$model = new Models\User\Admin;
- $model->__object((object) array(
+ $model->__object(new Object(array(
$relationship->name() => array(),
- ));
+ )));
$relationship->remove(
$this->container(),
View
3  test/RelationshipOneToOneTest.php
@@ -1,4 +1,5 @@
<?php
+use Gignite\TheCure\Object;
use Gignite\TheCure\Mapper\Container;
use Gignite\TheCure\Relationships\OneToOne;
@@ -32,7 +33,7 @@ public function testItShouldSaveRelationWhenSetting()
$model = new Models\User\Admin;
- $relationObject = (object) array('name' => 'Luke');
+ $relationObject = new Object(array('name' => 'Luke'));
$relation = new Models\User\Admin;
$relation->__object($relationObject);

No commit comments for this range

Something went wrong with that request. Please try again.