Skip to content
Permalink
Browse files

Working on the entity getters and setters

  • Loading branch information...
burzum committed Jan 21, 2017
1 parent 5722d8b commit 3a7c3a94d38f8391bd01a693ee046545d9e2d9f2
Showing with 80 additions and 28 deletions.
  1. +49 −28 src/Datasource/EntityTrait.php
  2. +31 −0 tests/TestCase/ORM/EntityTest.php
@@ -422,7 +422,7 @@ public function hiddenProperties($properties = null)
*
* @param null|array $properties Either an array of properties to treat as virtual or null to get properties
* @param bool $merge Merge the new properties with the existing. By default false.
* @return array|self
* @return self
*/
public function setHiddenProperties(array $properties, $merge = false)
{
@@ -860,13 +860,17 @@ public function setErrors(array $fields, $overwrite = false)
* $entity->errors('salary', ['must be numeric', 'must be a positive number']);
* ```
*
* @param string|array|null $field The field to get errors for, or the array of errors to set.
* @param string|array|null $errors The errors to be set for $field
* @param string $field The field to get errors for, or the array of errors to set.
* @param string|array $errors The errors to be set for $field
* @param bool $overwrite Whether or not to overwrite pre-existing errors for $field
* @return self
*/
public function setError($field, array $errors, $overwrite = false)
public function setError($field, $errors, $overwrite = false)
{
if (is_string($errors)) {
$errors = [$errors];
}
return $this->setErrors([$field => $errors], $overwrite);
}
@@ -1082,6 +1086,10 @@ public function invalid($field = null, $value = null, $overwrite = false)
*/
public function accessible($property, $set = null)
{
if ($set === null) {
return $this->isAccessible($property, $set);
}
return $this->setAccess($property, $set);
}
@@ -1104,30 +1112,19 @@ public function accessible($property, $set = null)
* $entity->accessible('*', false); // Mark all properties as protected
* ```
*
* When called without the second param it will return whether or not the property
* can be set.
*
* ### Example:
*
* ```
* $entity->accessible('id'); // Returns whether it can be set or not
* ```
*
* @param string|array $property single or list of properties to change its accessibility
* @param bool|null $set true marks the property as accessible, false will
* @param bool $set true marks the property as accessible, false will
* mark it as protected.
* @return self|bool
* @return self
*/
public function setAccess($property, $set = null)
public function setAccess($property, $set)
{
if ($set === null) {
$value = isset($this->_accessible[$property]) ?
$this->_accessible[$property] :
null;
return ($value === null && !empty($this->_accessible['*'])) || $value;
}
if ($property === '*') {
$this->_accessible = array_map(function ($p) use ($set) {
return (bool)$set;
@@ -1145,19 +1142,38 @@ public function setAccess($property, $set = null)
}
/**
* Returns the alias of the repository from which this entity came from.
* Checks if a property is accessible
*
* If called with no arguments, it returns the alias of the repository
* this entity came from if it is known.
* @param string $property Property name to check
* @return bool
*/
public function isAccessible($property)
{
$value = isset($this->_accessible[$property]) ?
$this->_accessible[$property] :
null;
return ($value === null && !empty($this->_accessible['*'])) || $value;
}
/**
* Returns the alias of the repository from which this entity came from.
*
* @param string|null $alias the alias of the repository
* @return string|self
*/
public function getSource($alias = null)
public function getSource()
{
return $this->_registryAlias;
}
/**
* Sets the source alias
*
* @param string $alias the alias of the repository
* @return self
*/
public function setSource($alias)
{
if ($alias === null) {
return $this->_registryAlias;
}
$this->_registryAlias = $alias;
return $this;
@@ -1169,13 +1185,18 @@ public function getSource($alias = null)
* If called with no arguments, it returns the alias of the repository
* this entity came from if it is known.
*
* @deprecated Use setSource() instead.
* @param string|null $alias the alias of the repository
* @return string|self
*/
public function source($alias = null)
{
return $this->getSource($alias);
if (is_null($alias)) {
return $this->getSource($alias);
}
$this->setSource($alias);
return $this;
}
/**
@@ -1079,6 +1079,26 @@ public function testErrors()
$this->assertEquals($errors, $entity->errors());
}
/**
* Tests error getters and setters
*
* @return void
*/
public function testGetAndSetErrors()
{
$entity = new Entity();
$this->assertEmpty($entity->getErrors());
$entity->setError('foo', 'bar');
$this->assertEquals(['bar'], $entity->errors('foo'));
$expected = [
'foo' => ['bar']
];
$result = $entity->getErrors();
$this->assertEquals($expected, $result);
}
/**
* Tests that it is possible to get errors for nested entities
*
@@ -1380,6 +1400,17 @@ public function testSource()
$this->assertEquals('foos', $entity->source());
}
/**
* Test the source getter
*/
public function testGetAndSetSource()
{
$entity = new Entity();
$this->assertNull($entity->getSource());
$entity->setSource('foos');
$this->assertEquals('foos', $entity->getSource());
}
/**
* Provides empty values
*

0 comments on commit 3a7c3a9

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