Skip to content
Permalink
Browse files

guard instead of safe

  • Loading branch information...
lorenzo committed Dec 20, 2013
1 parent a545dee commit 7154cf009fa82e5e17598178db7146c68b71b670
Showing with 18 additions and 18 deletions.
  1. +9 −9 Cake/ORM/Entity.php
  2. +1 −1 Cake/ORM/ResultSet.php
  3. +8 −8 Cake/Test/TestCase/ORM/EntityTest.php
@@ -115,19 +115,19 @@ class Entity implements \ArrayAccess, \JsonSerializable {
* - useSetters: whether use internal setters for properties or not
* - markClean: whether to mark all properties as clean after setting them
* - markNew: whether this instance has not yet been persisted
* - safe: whether to prevent inaccessible properties from being set (default: false)
* - guard: whether to prevent inaccessible properties from being set (default: false)
*/
public function __construct(array $properties = [], array $options = []) {
$options += [
'useSetters' => true,
'markClean' => false,
'markNew' => null,
'safe' => false
'guard' => false
];
$this->_className = get_class($this);
$this->set($properties, [
'setter' => $options['useSetters'],
'safe' => $options['safe']
'guard' => $options['guard']
]);
if ($options['markClean']) {
@@ -213,20 +213,20 @@ public function __unset($property) {
*
* Mass assignment should be treated carefully when accepting user input, for this
* case you can tell this method to only set property that are marked as accessible
* by setting the `safe` options in the `$options` parameter
* by setting the `guard` options in the `$options` parameter
*
* ### Example:
*
* ``$entity->set('name', 'Andrew', ['safe' => true]);``
* ``$entity->set('name', 'Andrew', ['guard' => true]);``
*
* ``$entity->set(['name' => 'Andrew', 'id' => 1], ['safe' => true]);``
* ``$entity->set(['name' => 'Andrew', 'id' => 1], ['guard' => true]);``
*
* @param string|array $property the name of property to set or a list of
* properties with their respective values
* @param mixed|array $value the value to set to the property or an array if the
* first argument is also an array, in which case will be treated as $options
* @param array $options options to be used for setting the property. Allowed option
* keys are `setter` and `safe`
* keys are `setter` and `guard`
* @return \Cake\ORM\Entity
*/
public function set($property, $value = null, $options = []) {
@@ -236,10 +236,10 @@ public function set($property, $value = null, $options = []) {
$options = (array)$value;
}
$options += ['setter' => true, 'safe' => false];
$options += ['setter' => true, 'guard' => false];
foreach ($property as $p => $value) {
if ($options['safe'] === true && !$this->accessible($p)) {
if ($options['guard'] === true && !$this->accessible($p)) {
continue;
}
@@ -313,7 +313,7 @@ protected function _groupResult($row) {
'useSetters' => false,
'markClean' => true,
'markNew' => false,
'safe' => false
'guard' => false
];
foreach (array_reverse($this->_associationMap) as $alias => $assoc) {
if (!isset($results[$alias])) {
@@ -132,31 +132,31 @@ public function testConstructor() {
->getMock();
$entity->expects($this->at(0))
->method('set')
->with(['a' => 'b', 'c' => 'd'], ['setter' => true, 'safe' => false]);
->with(['a' => 'b', 'c' => 'd'], ['setter' => true, 'guard' => false]);
$entity->expects($this->at(1))
->method('set')
->with(['foo' => 'bar'], ['setter' => false, 'safe' => false]);
->with(['foo' => 'bar'], ['setter' => false, 'guard' => false]);
$entity->__construct(['a' => 'b', 'c' => 'd']);
$entity->__construct(['foo' => 'bar'], ['useSetters' => false]);
}
/**
* Tests that the constructor will set initial properties and pass the safe
* Tests that the constructor will set initial properties and pass the guard
* option along
*
* @return void
*/
public function testConstructorWithSafe() {
public function testConstructorWithGuard() {
$entity = $this->getMockBuilder('\Cake\ORM\Entity')
->setMethods(['set'])
->disableOriginalConstructor()
->getMock();
$entity->expects($this->once())
->method('set')
->with(['foo' => 'bar'], ['setter' => true, 'safe' => true]);
$entity->__construct(['foo' => 'bar'], ['safe' => true]);
->with(['foo' => 'bar'], ['setter' => true, 'guard' => true]);
$entity->__construct(['foo' => 'bar'], ['guard' => true]);
}
/**
@@ -886,7 +886,7 @@ public function testAccessibleWildcard() {
*/
public function testSetWithAccessible() {
$entity = new Entity(['foo' => 1, 'bar' => 2]);
$options = ['safe' => true];
$options = ['guard' => true];
$entity->accessible('foo', true);
$entity->set('bar', 3, $options);
$entity->set('foo', 4, $options);
@@ -905,7 +905,7 @@ public function testSetWithAccessible() {
*/
public function testSetWithAccessibleWithArray() {
$entity = new Entity(['foo' => 1, 'bar' => 2]);
$options = ['safe' => true];
$options = ['guard' => true];
$entity->accessible('foo', true);
$entity->set(['bar' => 3, 'foo' => 4], $options);
$this->assertEquals(2, $entity->get('bar'));

0 comments on commit 7154cf0

Please sign in to comment.
You can’t perform that action at this time.