Skip to content

Commit

Permalink
Adding methods to the entity to check for values
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed May 30, 2017
1 parent 2df2728 commit 31ba1a3
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 14 deletions.
91 changes: 86 additions & 5 deletions src/Datasource/EntityTrait.php
Expand Up @@ -344,15 +344,15 @@ public function getOriginalValues()
*
* ```
* $entity = new Entity(['id' => 1, 'name' => null]);
* $entity->has('id'); // true
* $entity->has('name'); // false
* $entity->has('last_name'); // false
* $entity->hasProperty('id'); // true
* $entity->hasProperty('name'); // false
* $entity->hasProperty('last_name'); // false
* ```
*
* You can check multiple properties by passing an array:
*
* ```
* $entity->has(['name', 'last_name']);
* $entity->hasProperty(['name', 'last_name']);
* ```
*
* All properties must not be null to get a truthy result.
Expand All @@ -363,7 +363,7 @@ public function getOriginalValues()
* @param string|array $property The property or properties to check.
* @return bool
*/
public function has($property)
public function hasProperty($property)
{
foreach ((array)$property as $prop) {
if ($this->get($prop) === null) {
Expand All @@ -374,6 +374,87 @@ public function has($property)
return true;
}

/**
* Returns whether this entity contains a property named $property
* that contains a non-null value.
*
* ### Example:
*
* ```
* $entity = new Entity(['id' => 1, 'name' => null]);
* $entity->has('id'); // true
* $entity->has('name'); // false
* $entity->has('last_name'); // false
* ```
*
* You can check multiple properties by passing an array:
*
* ```
* $entity->has(['name', 'last_name']);
* ```
*
* All properties must not be null to get a truthy result.
*
* When checking multiple properties. All properties must not be null
* in order for true to be returned.
*
* @param string|array $property The property or properties to check.
* @return bool
*/
public function has($property)
{
return $this->hasProperty($property);
}

/**
* Checks that a property is empty
*
* This is not working like the built in empty() of php. The method will
* return true for:
*
* - '' (empty string)
* - null
* - []
*
* but false on any other case.
*
* @param string $property The property to check.
* @return bool
*/
public function isEmpty($property)
{
$value = $this->get($property);
if ($value === null
|| (is_array($value) && empty($value)
|| (is_string($value) && empty($value)))
) {
return true;
}

return false;
}

/**
* Checks tha a property has a value.
*
* This method will return true for
*
* - 'foo' (non empty string)
* - ['foo', 'bar']
* - Object
* - Integer, even 0 (Zero)
* - Float
*
* false on any other case
*
* @param string $property The property to check.
* @return bool
*/
public function hasValue($property)
{
return !$this->isEmpty($property);
}

/**
* Removes a property or list of properties from this entity
*
Expand Down
81 changes: 72 additions & 9 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -490,22 +490,22 @@ public function testIndirectModification()
public function testHas()
{
$entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
$this->assertTrue($entity->has('id'));
$this->assertTrue($entity->has('name'));
$this->assertFalse($entity->has('foo'));
$this->assertFalse($entity->has('last_name'));
$this->assertTrue($entity->hasProperty('id'));
$this->assertTrue($entity->hasProperty('name'));
$this->assertFalse($entity->hasProperty('foo'));
$this->assertFalse($entity->hasProperty('last_name'));

$this->assertTrue($entity->has(['id']));
$this->assertTrue($entity->has(['id', 'name']));
$this->assertFalse($entity->has(['id', 'foo']));
$this->assertFalse($entity->has(['id', 'nope']));
$this->assertTrue($entity->hasProperty(['id']));
$this->assertTrue($entity->hasProperty(['id', 'name']));
$this->assertFalse($entity->hasProperty(['id', 'foo']));
$this->assertFalse($entity->hasProperty(['id', 'nope']));

$entity = $this->getMockBuilder('\Cake\ORM\Entity')
->setMethods(['_getThings'])
->getMock();
$entity->expects($this->once())->method('_getThings')
->will($this->returnValue(0));
$this->assertTrue($entity->has('things'));
$this->assertTrue($entity->hasProperty('things'));
}

/**
Expand Down Expand Up @@ -1573,4 +1573,67 @@ public function testIsDirtyFromClone()
$this->assertTrue($cloned->dirty('a'));
$this->assertTrue($cloned->dirty('b'));
}

/**
* Test the isEmpty() check
*
* @return void
*/
public function testIsEmpty()
{
$entity = new Entity([
'array' => ['foo' => 'bar'],
'emptyArray' => [],
'object' => new \stdClass(),
'string' => 'string',
'emptyString' => '',
'intZero' => 0,
'intNotZero' => 1,
'floatZero' => 0.0,
'floatNonZero' => 1.5,
'null' => null
]);

$this->assertFalse($entity->isEmpty('array'));
$this->assertTrue($entity->isEmpty('emptyArray'));
$this->assertFalse($entity->isEmpty('object'));
$this->assertFalse($entity->isEmpty('string'));
$this->assertTrue($entity->isEmpty('emptyString'));
$this->assertFalse($entity->isEmpty('intZero'));
$this->assertFalse($entity->isEmpty('intNotZero'));
$this->assertFalse($entity->isEmpty('floatZero'));
$this->assertFalse($entity->isEmpty('floatNonZero'));
$this->assertTrue($entity->isEmpty('null'));
}

/**
* Test hasValue()
*
* @return void
*/
public function testHasValue() {
$entity = new Entity([
'array' => ['foo' => 'bar'],
'emptyArray' => [],
'object' => new \stdClass(),
'string' => 'string',
'emptyString' => '',
'intZero' => 0,
'intNotZero' => 1,
'floatZero' => 0.0,
'floatNonZero' => 1.5,
'null' => null
]);

$this->assertTrue($entity->hasValue('array'));
$this->assertFalse($entity->hasValue('emptyArray'));
$this->assertTrue($entity->hasValue('object'));
$this->assertTrue($entity->hasValue('string'));
$this->assertFalse($entity->hasValue('emptyString'));
$this->assertTrue($entity->hasValue('intZero'));
$this->assertTrue($entity->hasValue('intNotZero'));
$this->assertTrue($entity->hasValue('floatZero'));
$this->assertTrue($entity->hasValue('floatNonZero'));
$this->assertFalse($entity->hasValue('null'));
}
}

0 comments on commit 31ba1a3

Please sign in to comment.