Last active
October 2, 2015 14:30
-
-
Save cdrubin/e0cc50cb24e532b20663 to your computer and use it in GitHub Desktop.
Benchmark Mongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// depends on: | |
// | |
// benchmarkeducation/matchmaker | |
// xmarcos/dot-container | |
// from composer | |
// for convenient use of same connection to return collections | |
// | |
class BenchmarkMongo { | |
private static $database = 'testin_i_am'; | |
private static $connection; | |
public static function collection( $collection_name ) { | |
if ( !isset( $connection ) ) { | |
$client = new MongoClient(); | |
$database_name = self::$database; | |
self::$connection = $client->$database_name; | |
} | |
return new BenchmarkCollection( self::$connection, $collection_name ); | |
} | |
} | |
// to make sure that cursors return model objects | |
// | |
class BenchmarkCursor implements Iterator { | |
protected $collection; | |
protected $model_name; | |
public $raw; | |
public function __construct( $collection, $model_name, $cursor ) { | |
$this->collection = $collection; | |
$this->model_name = $model_name; | |
$this->raw = $cursor; | |
} | |
public function wrap( $array ) { | |
$model = new $this->model_name( $this->collection ); | |
$model->init( $array ); | |
return $model; | |
} | |
public function current() { | |
return $this->wrap( $this->raw->current() );; | |
} | |
public function key() { | |
return $this->raw->key(); | |
} | |
public function rewind() { | |
return $this->raw->rewind(); | |
} | |
public function valid() { | |
return $this->raw->valid(); | |
} | |
public function next() { | |
return $this->wrap( $this->raw->next() ); | |
} | |
} | |
// to make sure that find and findOne return model objects | |
// | |
class BenchmarkCollection { | |
protected $model_name; | |
public $raw; | |
public function __construct( $connection, $collection_name ) { | |
$this->raw = $connection->$collection_name; | |
$this->model_name = ucwords( $collection_name ); | |
} | |
public function find( $query ) { | |
$cursor = $this->raw->find( $query ); | |
$benchmark_cursor = new BenchmarkCursor( $this, $this->model_name, $cursor ); | |
return $benchmark_cursor; | |
} | |
public function findOne( $query ) { | |
$result = $this->raw->findOne( $query ); | |
$model = new $this->model_name( $this ); | |
$model->init( $result ); | |
return $model; | |
} | |
} | |
// base model class with auto-validation on init and save | |
// | |
class BenchmarkModel { | |
protected $data = []; | |
protected $collection; | |
public function __construct( $collection ) { | |
$this->collection = $collection; | |
} | |
public function init( $data ) { | |
if ( $this->validate( $data ) ) { | |
$this->data = $data; | |
return TRUE; | |
} | |
else { | |
return FALSE; | |
} | |
} | |
public function validate( $data ) { | |
return matchmaker\catches( $data, $this->schema ); | |
} | |
public function save() { | |
if ( $this->validate( $this->data ) ) { | |
$this->collection->raw->save( $this->data ); | |
return TRUE; | |
} | |
else { | |
return FALSE; | |
} | |
} | |
public function get( $location ) { | |
$data = new xmarcos\Dot\Container( $this->data ); | |
return $data->get( $location ); | |
} | |
public function set( $location, $value ) { | |
$data = new xmarcos\Dot\Container( $this->data ); | |
$data->set( $location, $value ); | |
echo ( "set val is now: " . $data->get( $location ) ); | |
$this->data = $data->all(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// example model | |
// | |
class User extends BenchmarkModel { | |
protected $schema = [ | |
'_id' => ':string', | |
'type' => 'user', | |
'firstname' => ':string contains(hello)', | |
'assignments' => [ | |
'*' => [ | |
'priority' => '1', | |
'title' => ':string' | |
] | |
] | |
]; | |
} | |
$user_collection = BenchmarkMongo::collection( 'user' ); | |
$user = new User( $user_collection ); | |
$user->init( [ | |
'_id' => '234567aab', | |
'type' => 'user', | |
'firstname' => 'goo hello', | |
'assignments' => [ | |
[ | |
'priority' => '1', | |
'title' => 'moo' | |
], | |
[ | |
'priority' => '1', | |
'title' => 'cow' | |
] | |
], | |
//'nuts' => 'it' | |
] ); | |
$user->save(); | |
// see exception when adding 'nuts' as a property because it does not exist in schema | |
$users = $user_collection->find( [] ); | |
foreach( $users as $user ) { | |
echo $user->get( 'firstname' ); | |
echo $user->get( 'assignments.0.title' ); | |
//$new_name = $user->get( 'firstname' ) . ' returns hello'; | |
//$user->set( 'firstname', $new_name ); | |
//$user->save(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment