Skip to content

Commit

Permalink
Added Phalcon\Validation for Phalcon\Mvc\Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag committed Jan 12, 2017
1 parent 27eb58f commit 8bf6b84
Show file tree
Hide file tree
Showing 12 changed files with 1,026 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Added the ability to set custom Resultset class returned by find() [#12166](https://github.com/phalcon/cphalcon/issues/12166)
- Added the ability to clear appended and prepended title elements (Phalcon\Tag::appendTitle, Phalcon\Tag::prependTitle). Now you can use array to add multiple titles. For more details check [#12238](https://github.com/phalcon/cphalcon/issues/12238).
- Added the ability to specify what empty means in the 'allowEmpty' option of the validators. Now it accepts as well an array specifying what's empty, for example ['', false]
- Added the ability to use `Phalcon\Validation` with `Phalcon\Mvc\Collection`, deprecated `Phalcon\Mvc\Collection::validationHasFailed`

# [3.0.4](https://github.com/phalcon/cphalcon/releases/tag/v3.0.4) (XXXX-XX-XX)
- Fixed Isnull check is not correct when the model field defaults to an empty string. [#12507](https://github.com/phalcon/cphalcon/issues/12507)
Expand Down
121 changes: 113 additions & 8 deletions phalcon/mvc/collection.zep
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use Phalcon\Mvc\Collection\BehaviorInterface;
use Phalcon\Mvc\Collection\Exception;
use Phalcon\Mvc\Model\MessageInterface;
use Phalcon\Mvc\Model\Message as Message;
use Phalcon\ValidationInterface;

/**
* Phalcon\Mvc\Collection
Expand All @@ -49,6 +50,8 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec

protected _operationMade = 0;

protected _dirtyState = 1;

protected _connection;

protected _errorMessages = [];
Expand All @@ -67,6 +70,12 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec

const OP_DELETE = 3;

const DIRTY_STATE_PERSISTENT = 0;

const DIRTY_STATE_TRANSIENT = 1;

const DIRTY_STATE_DETACHED = 2;

/**
* Phalcon\Mvc\Collection constructor
*/
Expand Down Expand Up @@ -203,7 +212,8 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
"_dependencyInjector": true,
"_source": true,
"_operationMade": true,
"_errorMessages": true
"_errorMessages": true,
"_dirtyState": true
];
let self::_reserved = reserved;
}
Expand Down Expand Up @@ -633,6 +643,7 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
* {
* public function validation()
* {
* // Old, deprecated syntax, use new one below
* $this->validate(
* new ExclusionIn(
* [
Expand All @@ -648,15 +659,71 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
* }
* }
*</code>
*
*<code>
* use Phalcon\Validation\Validator\ExclusionIn as ExclusionIn;
* use Phalcon\Validation;
*
* class Subscriptors extends \Phalcon\Mvc\Collection
* {
* public function validation()
* {
* $validator = new Validation();
* $validator->add("status",
* new ExclusionIn(
* [
* "domain" => ["A", "I"]
* ]
* )
* );
*
* return $this->validate($validator);
* }
* }
*</code>
*/
protected function validate(<Model\ValidatorInterface> validator) -> void
protected function validate(var validator)
{
var message;
var messages, message;

if validator instanceof Model\ValidatorInterface {
if validator->validate(this) === false {
for message in validator->getMessages() {
let this->_errorMessages[] = message;
}
}
} elseif validator instanceof ValidationInterface {
let messages = validator->validate(null, this);

// Call the validation, if it returns not the boolean
// we append the messages to the current object
if typeof messages != "boolean" {

messages->rewind();

// for message in iterator(messages) {
while messages->valid() {

if validator->validate(this) === false {
for message in validator->getMessages() {
let this->_errorMessages[] = message;
let message = messages->current();

this->appendMessage(
new Message(
message->getMessage(),
message->getField(),
message->getType()
)
);

messages->next();
}

// If there is a message, it returns false otherwise true
return !count(messages);
}

return messages;
} else {
throw new Exception("You should pass Phalcon\\Mvc\\Model\\ValidatorInterface or Phalcon\\ValidationInterface object");
}
}

Expand Down Expand Up @@ -685,6 +752,9 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
* }
* }
*</code>
*
* @deprecated 3.1.0
* @see \Phalcon\Validation
*/
public function validationHasFailed() -> boolean
{
Expand Down Expand Up @@ -759,7 +829,7 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
*/
protected function _exists(collection) -> boolean
{
var id, mongoId;
var id, mongoId, exists;

if !fetch id, this->_id {
return false;
Expand All @@ -780,10 +850,25 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
}
}

/**
* If we already know if the document exists we don't check it
*/
if !this->_dirtyState {
return true;
}

/**
* Perform the count using the function provided by the driver
*/
return collection->count(["_id": mongoId]) > 0;
let exists = collection->count(["_id": mongoId]) > 0;

if exists {
let this->_dirtyState = self::DIRTY_STATE_PERSISTENT;
} else {
let this->_dirtyState = self::DIRTY_STATE_TRANSIENT;
}

return exists;
}

/**
Expand Down Expand Up @@ -917,6 +1002,8 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
if fetch id, data["_id"] {
let this->_id = id;
}
} else {
let this->_dirtyState = self::DIRTY_STATE_PERSISTENT;
}
}
}
Expand Down Expand Up @@ -1468,6 +1555,7 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
if !disableEvents {
this->fireEvent("afterDelete");
}
let this->_dirtyState = self::DIRTY_STATE_DETACHED;
}
} else {
let success = false;
Expand All @@ -1476,6 +1564,23 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
return success;
}

/**
* Sets the dirty state of the object using one of the DIRTY_STATE_* constants
*/
public function setDirtyState(int dirtyState) -> <CollectionInterface>
{
let this->_dirtyState = dirtyState;
return this;
}

/**
* Returns one of the DIRTY_STATE_* constants telling if the document exists in the collection or not
*/
public function getDirtyState() -> int
{
return this->_dirtyState;
}

/**
* Sets up a behavior in a collection
*/
Expand Down
12 changes: 12 additions & 0 deletions phalcon/mvc/collectioninterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ interface CollectionInterface
*/
public function getConnection();

/**
* Sets the dirty state of the object using one of the DIRTY_STATE_* constants
*/
public function setDirtyState(int dirtyState) -> <\Phalcon\Mvc\CollectionInterface>;

/**
* Returns one of the DIRTY_STATE_* constants telling if the record exists in the database or not
*
* @return int
*/
public function getDirtyState() -> int;

/**
* Returns a cloned collection
*/
Expand Down
2 changes: 1 addition & 1 deletion phalcon/mvc/micro.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ class Micro extends Injectable implements \ArrayAccess
}

/**
* Appends a afterBiding middleware to be called after model binding
* Appends a afterBinding middleware to be called after model binding
*
* @param callable handler
* @return \Phalcon\Mvc\Micro
Expand Down
Loading

0 comments on commit 8bf6b84

Please sign in to comment.