Skip to content

Commit

Permalink
T13114 message interfaces (#13534)
Browse files Browse the repository at this point in the history
* [#13114] - Added messages component and relevant interface

* [#13114] - Added message exception

* [#13114] - Added messages collection (renamed from group)

* [#13114] - Removed message objects from validation

* [#13114] - Removed the group class in favor of the messages one

* [#13114] - Replace of Group with Messages object

* [#13114] - Removed model messageinterface; Refactored model message; Corrected use reference

* [#13114] - Corrected class extend

* [#13114] - Test corrections

* [#13114] - Corrected methods/objects

* [#13114] - More test corrections

* [#13114] - Fixing tests

* [#13114] - Corrected order in adding messages with a model

* [#13114] - Correcting more tests

* [#13114] - Removed the Mvc\Model\Message class - using the Messages\Message

* [#13114] - Updated the changelog

* [#13114] - Added JsonSerializable interface in Message and Messages

* [#13114] - Corrected noobie typo

* [#13114] - More noobie typos

* [#13114] - Changed the variable type

* [#13114] - Forgot the implements :/

* [#13114] - Corrected test

* [#13114] - Changed the var type

* [#13114] - Array initialization that was missing
  • Loading branch information
niden authored and sergeyklay committed Oct 19, 2018
1 parent ca61578 commit f1331fc
Show file tree
Hide file tree
Showing 62 changed files with 498 additions and 599 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added `Phalcon\Mvc\Router\RouteInterface::convert` so that calling `Phalcon\Mvc\Router\Group::add` will return an instance that has `convert` method [#13380](https://github.com/phalcon/cphalcon/issues/13380)
- Added `Phalcon\Mvc\ModelInterface::getModelsMetaData` [#13070](https://github.com/phalcon/cphalcon/issues/13402)
- Added `paginate()` method as a proxy of `getPaginate`. Added `previous` in the paginator object same as `before`. After 4.0 is released we will deprecate `getPaginate()`, `before` and `total_items` [#13492](https://github.com/phalcon/cphalcon/issues/13492)
- Added `Phalcon\Messages\MessageInterface`, `Phalcon\Messages\Message`, `Phalcon\Messages\Exception` and `Phalcon\Messages\Messages` to handle all messages for the application (model/validation) [#13114](https://github.com/phalcon/cphalcon/issues/13114)
- Added `getHandlerSuffix()`, `setHandlerSuffix()` in Dispatcher, `getTaskSuffix()`, `setTaskSuffix()` in the CLI Dispatcher [#13468](https://github.com/phalcon/cphalcon/issues/13468)
- Added ability to set a custom template for the Flash Messenger. [#13445](https://github.com/phalcon/cphalcon/issues/13445)

Expand All @@ -20,6 +21,8 @@
- Changed `Phalcon\Mvc\Model\Query\Builder::addFrom` to remove third parameter `$with` [#13109](https://github.com/phalcon/cphalcon/pull/13109)
- `Phalcon\Forms\Form::clear` will no longer call `Phalcon\Forms\Element::clear`, instead it will clear/set default value itself, and `Phalcon\Forms\Element::clear` will now call `Phalcon\Forms\Form::clear` if it's assigned to the form, otherwise it will just clear itself. [#13500](https://github.com/phalcon/cphalcon/pull/13500)
- `Phalcon\Forms\Form::getValue` will now also try to get the value by calling `Tag::getValue` or element's `getDefault` method before returning `null`, and `Phalcon\Forms\Element::getValue` calls `Tag::getDefault` only if it's not added to the form. [#13500](https://github.com/phalcon/cphalcon/pull/13500)
- Changed `Phalcon\Mvc\Model` to use the `Phalcon\Messages\Message` object for its messages [#13114](https://github.com/phalcon/cphalcon/issues/13114)
- Changed `Phalcon\Validation\*` to use the `Phalcon\Messages\Message` object for its messages [#13114](https://github.com/phalcon/cphalcon/issues/13114)

## Removed
- PHP < 7.0 no longer supported
Expand All @@ -33,3 +36,7 @@
- Removed deprecated `Phalcon\Security::getSslVersionNumber`
- Removed `Phalcon\Http\RequestInterface::isSoapRequested` in favor of `Phalcon\Http\Request::isSoap`
- Removed `Phalcon\Http\RequestInterface::isSecureRequest` in favor of `Phalcon\Http\RequestInterface::isSecure`
- Removed `Phalcon\Validation\MessageInterface` and `Phalcon\Mvc\Model\MessageInterface` in favor of `Phalcon\Messages\MessageInterface`
- Removed `Phalcon\Validation\Message` and `Phalcon\Mvc\Model\Message` in favor of `Phalcon\Messages\Message`
- Removed `Phalcon\Validation\Message\Group` in favor of `Phalcon\Messages\Messages`

13 changes: 6 additions & 7 deletions phalcon/forms/element.zep
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ namespace Phalcon\Forms;

use Phalcon\Tag;
use Phalcon\Forms\Exception;
use Phalcon\Validation\Message;
use Phalcon\Validation\MessageInterface;
use Phalcon\Validation\Message\Group;
use Phalcon\Messages\MessageInterface;
use Phalcon\Messages\Messages;
use Phalcon\Validation\ValidatorInterface;

/**
Expand Down Expand Up @@ -65,7 +64,7 @@ abstract class Element implements ElementInterface

let this->_name = name;
let this->_attributes = attributes;
let this->_messages = new Group();
let this->_messages = new Messages();
}

/**
Expand Down Expand Up @@ -444,7 +443,7 @@ abstract class Element implements ElementInterface
* Returns the messages that belongs to the element
* The element needs to be attached to a form
*/
public function getMessages() -> <Group>
public function getMessages() -> <Messages>
{
return this->_messages;
}
Expand All @@ -460,9 +459,9 @@ abstract class Element implements ElementInterface
/**
* Sets the validation messages related to the element
*/
public function setMessages(<Group> group) -> <ElementInterface>
public function setMessages(<Messages> messages) -> <ElementInterface>
{
let this->_messages = group;
let this->_messages = messages;
return this;
}

Expand Down
8 changes: 4 additions & 4 deletions phalcon/forms/elementinterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
namespace Phalcon\Forms;

use Phalcon\Forms\Form;
use Phalcon\Validation\MessageInterface;
use Phalcon\Messages\MessageInterface;
use Phalcon\Messages\Messages;
use Phalcon\Validation\ValidatorInterface;
use Phalcon\Validation\Message\Group;

/**
* Phalcon\Forms\Element
Expand Down Expand Up @@ -169,7 +169,7 @@ interface ElementInterface
* Returns the messages that belongs to the element
* The element needs to be attached to a form
*/
public function getMessages() -> <Group>;
public function getMessages() -> <Messages>;

/**
* Checks whether there are messages attached to the element
Expand All @@ -179,7 +179,7 @@ interface ElementInterface
/**
* Sets the validation messages related to the element
*/
public function setMessages(<Group> group) -> <ElementInterface>;
public function setMessages(<Messages> messages) -> <ElementInterface>;

/**
* Appends a message to the internal message list
Expand Down
26 changes: 13 additions & 13 deletions phalcon/forms/form.zep
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

namespace Phalcon\Forms;

use Phalcon\Tag;
use Phalcon\Validation;
use Phalcon\ValidationInterface;
use Phalcon\Di\Injectable;
use Phalcon\DiInterface;
use Phalcon\FilterInterface;
use Phalcon\Di\Injectable;
use Phalcon\Forms\Exception;
use Phalcon\Forms\ElementInterface;
use Phalcon\Validation\Message\Group;
use Phalcon\Messages\Messages;
use Phalcon\Tag;
use Phalcon\Validation;
use Phalcon\ValidationInterface;

/**
* Phalcon\Forms\Form
Expand Down Expand Up @@ -362,15 +362,15 @@ class Form extends Injectable implements \Countable, \Iterator
* <code>
* if ($form->isValid($_POST) == false) {
* // Get messages separated by the item name
* // $messages is an array of Group object
* // $messages is an array of Messages object
* $messages = $form->getMessages(true);
*
* foreach ($messages as $message) {
* echo $message, "<br>";
* }
*
* // Default behavior.
* // $messages is a Group object
* // $messages is a Messages object
* $messages = $form->getMessages();
*
* foreach ($messages as $message) {
Expand All @@ -379,13 +379,13 @@ class Form extends Injectable implements \Countable, \Iterator
* }
* </code>
*/
public function getMessages(boolean byItemName = false) -> <Group> | array
public function getMessages(boolean byItemName = false) -> <Messages> | array
{
var messages, messagesByItem, elementMessage, fieldName;

let messages = this->_messages;

if typeof messages == "object" && messages instanceof Group {
if typeof messages == "object" && messages instanceof Messages {
/**
* @deprecated This part of code is for backward compatibility, it should be removed in next major version
*/
Expand All @@ -401,26 +401,26 @@ class Form extends Injectable implements \Countable, \Iterator
let messagesByItem[fieldName] = [];
}

let messagesByItem[fieldName][] = new Group([elementMessage]);
let messagesByItem[fieldName][] = new Messages([elementMessage]);
messages->next();
}
return messagesByItem;
}
return messages;
}

return new Group();
return new Messages();
}

/**
* Returns the messages generated for a specific element
*/
public function getMessagesFor(string! name) -> <Group>
public function getMessagesFor(string! name) -> <Messages>
{
if this->has(name) {
return this->get(name)->getMessages();
}
return new Group();
return new Messages();
}

/**
Expand Down
30 changes: 30 additions & 0 deletions phalcon/messages/exception.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-present Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Phalcon Team <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Messages;

/**
* Phalcon\Validation\Exception
*
* Exceptions thrown in Phalcon\Messages\* classes will use this class
*
*/
class Exception extends \Phalcon\Exception
{

}
90 changes: 51 additions & 39 deletions phalcon/validation/message.zep → phalcon/messages/message.zep
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2017 Phalcon Team (https://phalconphp.com) |
| Copyright (c) 2011-present Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
Expand All @@ -12,21 +12,20 @@
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
| Authors: Phalcon Team <[email protected]> |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Validation;
namespace Phalcon\Messages;

use Phalcon\Validation\MessageInterface;
use Phalcon\Messages\MessageInterface;

/**
* Phalcon\Validation\Message
* Phalcon\Messages\Message
*
* Encapsulates validation info generated in the validation process
* Stores a message from various components
*/
class Message implements MessageInterface
class Message implements MessageInterface, \JsonSerializable
{

protected _type;
Expand All @@ -38,7 +37,7 @@ class Message implements MessageInterface
protected _code;

/**
* Phalcon\Validation\Message constructor
* Phalcon\Messages\Message constructor
*/
public function __construct(string! message, var field = null, string type = null, int code = null)
{
Expand All @@ -49,73 +48,86 @@ class Message implements MessageInterface
}

/**
* Sets message type
* Returns the message code
*/
public function setType(string! type) -> <Message>
public function getCode() -> int
{
let this->_type = type;
return this;
return this->_code;
}

/**
* Returns message type
* Returns field name related to message
*
* @return mixed
*/
public function getType() -> string
public function getField()
{
return this->_type;
return this->_field;
}

/**
* Sets verbose message
* Returns verbose message
*/
public function setMessage(string! message) -> <Message>
public function getMessage() -> string
{
let this->_message = message;
return this;
return this->_message;
}

/**
* Returns verbose message
* Returns message type
*/
public function getMessage() -> string
public function getType() -> string
{
return this->_message;
return this->_type;
}

/**
* Serializes the object for json_encode
*/
public function jsonSerialize() -> array
{
return [
"field": this->_field,
"message": this->_message,
"type": this->_type,
"code": this->_code
];
}

/**
* Sets field name related to message
* Sets code for the message
*/
public function setField(var field) -> <Message>
public function setCode(int code) -> <Message>
{
let this->_field = field;
let this->_code = code;
return this;
}

/**
* Returns field name related to message
*
* @return mixed
* Sets field name related to message
*/
public function getField()
public function setField(var field) -> <Message>
{
return this->_field;
let this->_field = field;
return this;
}

/**
* Sets code for the message
* Sets verbose message
*/
public function setCode(int code) -> <Message>
public function setMessage(string! message) -> <Message>
{
let this->_code = code;
let this->_message = message;
return this;
}

/**
* Returns the message code
* Sets message type
*/
public function getCode() -> int
public function setType(string! type) -> <Message>
{
return this->_code;
let this->_type = type;
return this;
}

/**
Expand All @@ -127,10 +139,10 @@ class Message implements MessageInterface
}

/**
* Magic __set_state helps to recover messages from serialization
* Magic __set_state helps to re-build messages variable exporting
*/
public static function __set_state(array! message) -> <Message>
{
return new self(message["_message"], message["_field"], message["_type"]);
return new self(message["_message"], message["_field"], message["_type"], message["_code"]);
}
}
Loading

0 comments on commit f1331fc

Please sign in to comment.