From 61dd7c7108ebd28ee76a852c2571d599a519b11e Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Mon, 15 Mar 2010 16:42:19 -0400 Subject: [PATCH] - Added Validators - Added new hooks --- lib/ActiveMongo.php | 3 +- lib/Events.php | 2 + samples/blog/App.php => lib/Validators.php | 116 ++++++++++++++--------------- samples/blog/App.php | 2 +- samples/blog/Post.php | 12 ++- 5 files changed, 72 insertions(+), 63 deletions(-) copy samples/blog/App.php => lib/Validators.php (58%) diff --git a/lib/ActiveMongo.php b/lib/ActiveMongo.php index 76b5da5..697753c 100644 --- a/lib/ActiveMongo.php +++ b/lib/ActiveMongo.php @@ -350,6 +350,7 @@ abstract class ActiveMongo extends ActiveMongo_Events implements Iterator $this->findReferences($object); + $this->triggerEvent('before_validate_'.($update?'update':'creation'), array(&$object)); $this->triggerEvent('before_validate', array(&$object)); foreach ($object as $key => $value) { @@ -411,6 +412,7 @@ abstract class ActiveMongo extends ActiveMongo_Events implements Iterator return array(); } + $this->triggerEvent('after_validate_'.($update?'update':'creation'), array(&$object)); $this->triggerEvent('after_validate', array(&$document)); return $document; @@ -681,7 +683,6 @@ abstract class ActiveMongo extends ActiveMongo_Events implements Iterator */ final function reset() { - $this->_count = 0; $this->_cursor = null; $this->setResult(array()); } diff --git a/lib/Events.php b/lib/Events.php index 09c4afa..b5e7613 100644 --- a/lib/Events.php +++ b/lib/Events.php @@ -35,6 +35,8 @@ +---------------------------------------------------------------------------------+ */ +require_once dirname(__FILE__)."/Validators.php"; + /** * This class manages the Events and Filterings * diff --git a/samples/blog/App.php b/lib/Validators.php similarity index 58% copy from samples/blog/App.php copy to lib/Validators.php index 027d00f..7849e80 100644 --- a/samples/blog/App.php +++ b/lib/Validators.php @@ -35,71 +35,69 @@ +---------------------------------------------------------------------------------+ */ -require "../../lib/ActiveMongo.php"; -require "Post.php"; -require "Author.php"; +// Valid Presence Of {{{ +ActiveMongo_Events::addEvent("before_validate_creation", function ($class, $obj) { + if (isset($class::$validates_presence_of)) { + foreach ((Array)$class::$validates_presence_of as $property) { + if (!isset($obj[$property])) { + throw new FilterException("Missing required property {$property}"); + } + } + } +}); -ActiveMongo::connect("activemongo_blog"); +ActiveMongo_Events::addEvent("before_validate_update", function ($class, $obj) { + if (isset($class::$validates_presence_of)) { + foreach ((Array)$class::$validates_presence_of as $property) { + if (isset($obj['$unset'][$property])) { + throw new FilterException("Cannot delete required property {$property}"); + } + } + } +}); +// }}} -/* delete collections */ -PostModel::drop(); -AuthorModel::drop(); +// Valid Size Of / Valid Length Of {{{ +ActiveMongo_Events::addEvent("before_validate", function ($class, $obj) { + $validates = array(); -/* This should be done just once */ -ActiveMongo::install(); + if (isset($class::$validates_size_of)) { + $validates = $class::$validates_size_of; + } else if (isset($class::$validates_length_of)) { + $validates = $class::$validates_length_of; + } -/* Create a new author - * The property country is not defined - * as an AuthorModel property, but it will - * be saved. - */ -$author = new AuthorModel; -$author->username = "crodas"; -$author->name = "Cesar Rodas"; -$author->country = "PY"; -$author->save(); - -/* Add one blog post */ -$post = new PostModel; -$post->uri = "/hello-world"; -$post->title = "Hello World"; -$post->author = $author->getID(); -/* add one comment */ -$post->add_comment("testing", "root@foo.com", "testing comment"); -$post->save(); - -/* add another comment */ -$post->add_comment("testing", "root@foo.com", "cool post"); -$post->save(); + foreach ($validates as $property) { + $name = $property[0]; -for ($i=0; $i < 1000; $i++) { - /* Add another post */ - $post = new PostModel; - $post->uri = "/".uniqid(); - $post->title = "Yet another post ($i)"; - $post->author = $author->getID(); - $post->save(); -} + if (isset($obj[$name])) { + $prop = $obj[$name]; + } -/* Clean up the current the resultset */ -/* same as $post = null; $post = new Post Model */ -/* but more efficient */ -$post->reset(); -$post->author = $author->getID(); -foreach ($post->find() as $bp) { - var_dump("Author: ".$bp->author_name); -} + if (isset($obj['$set'][$name])) { + $prop = $obj['$set'][$name]; + } -$author->name = "cesar d. rodas"; -$author->save(); + if (isset($prop)) { + if (isset($property['min']) && strlen($prop) < $property['min']) { + throw new FilterException("{$name} length is too short"); + } + if (isset($property['is']) && strlen($prop) != $property['is']) { + throw new FilterException("{$name} length is different than expected"); + } + if (isset($property['max']) && strlen($prop) > $property['max']) { + throw new FilterException("{$name} length is too large"); + } + } + } +}); +// }}} -var_dump("Author profile has been updated"); - -/** - * List our blog posts in the correct order - * (descending by Timestamp). +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 */ -foreach ($post->listing_page() as $bp) { - var_dump(array("Author" => $bp->author_name, "Title"=>$bp->title)); -} - diff --git a/samples/blog/App.php b/samples/blog/App.php index 027d00f..64dd883 100644 --- a/samples/blog/App.php +++ b/samples/blog/App.php @@ -74,7 +74,7 @@ $post->save(); for ($i=0; $i < 1000; $i++) { /* Add another post */ - $post = new PostModel; + $post->reset(); /* reet the post object */ $post->uri = "/".uniqid(); $post->title = "Yet another post ($i)"; $post->author = $author->getID(); diff --git a/samples/blog/Post.php b/samples/blog/Post.php index 29dafe0..93eb605 100644 --- a/samples/blog/Post.php +++ b/samples/blog/Post.php @@ -36,10 +36,18 @@ */ -PostModel::addEvent('before_create', function($obj) { print "Attempt to create {$obj['title']}\n"; }); - class PostModel extends ActiveMongo { + static $validates_presence_of = array( + 'title', + 'uri', + 'author', + ); + + static $validates_length_of = array( + array('title', 'min' => 5, 'max' => 30), + ); + public $title; public $uri; public $author; -- 2.11.4.GIT