Skip to content

Commit 31ba1a3

Browse files
committed
Adding methods to the entity to check for values
1 parent 2df2728 commit 31ba1a3

File tree

2 files changed

+158
-14
lines changed

2 files changed

+158
-14
lines changed

src/Datasource/EntityTrait.php

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,15 @@ public function getOriginalValues()
344344
*
345345
* ```
346346
* $entity = new Entity(['id' => 1, 'name' => null]);
347-
* $entity->has('id'); // true
348-
* $entity->has('name'); // false
349-
* $entity->has('last_name'); // false
347+
* $entity->hasProperty('id'); // true
348+
* $entity->hasProperty('name'); // false
349+
* $entity->hasProperty('last_name'); // false
350350
* ```
351351
*
352352
* You can check multiple properties by passing an array:
353353
*
354354
* ```
355-
* $entity->has(['name', 'last_name']);
355+
* $entity->hasProperty(['name', 'last_name']);
356356
* ```
357357
*
358358
* All properties must not be null to get a truthy result.
@@ -363,7 +363,7 @@ public function getOriginalValues()
363363
* @param string|array $property The property or properties to check.
364364
* @return bool
365365
*/
366-
public function has($property)
366+
public function hasProperty($property)
367367
{
368368
foreach ((array)$property as $prop) {
369369
if ($this->get($prop) === null) {
@@ -374,6 +374,87 @@ public function has($property)
374374
return true;
375375
}
376376

377+
/**
378+
* Returns whether this entity contains a property named $property
379+
* that contains a non-null value.
380+
*
381+
* ### Example:
382+
*
383+
* ```
384+
* $entity = new Entity(['id' => 1, 'name' => null]);
385+
* $entity->has('id'); // true
386+
* $entity->has('name'); // false
387+
* $entity->has('last_name'); // false
388+
* ```
389+
*
390+
* You can check multiple properties by passing an array:
391+
*
392+
* ```
393+
* $entity->has(['name', 'last_name']);
394+
* ```
395+
*
396+
* All properties must not be null to get a truthy result.
397+
*
398+
* When checking multiple properties. All properties must not be null
399+
* in order for true to be returned.
400+
*
401+
* @param string|array $property The property or properties to check.
402+
* @return bool
403+
*/
404+
public function has($property)
405+
{
406+
return $this->hasProperty($property);
407+
}
408+
409+
/**
410+
* Checks that a property is empty
411+
*
412+
* This is not working like the built in empty() of php. The method will
413+
* return true for:
414+
*
415+
* - '' (empty string)
416+
* - null
417+
* - []
418+
*
419+
* but false on any other case.
420+
*
421+
* @param string $property The property to check.
422+
* @return bool
423+
*/
424+
public function isEmpty($property)
425+
{
426+
$value = $this->get($property);
427+
if ($value === null
428+
|| (is_array($value) && empty($value)
429+
|| (is_string($value) && empty($value)))
430+
) {
431+
return true;
432+
}
433+
434+
return false;
435+
}
436+
437+
/**
438+
* Checks tha a property has a value.
439+
*
440+
* This method will return true for
441+
*
442+
* - 'foo' (non empty string)
443+
* - ['foo', 'bar']
444+
* - Object
445+
* - Integer, even 0 (Zero)
446+
* - Float
447+
*
448+
* false on any other case
449+
*
450+
* @param string $property The property to check.
451+
* @return bool
452+
*/
453+
public function hasValue($property)
454+
{
455+
return !$this->isEmpty($property);
456+
}
457+
377458
/**
378459
* Removes a property or list of properties from this entity
379460
*

tests/TestCase/ORM/EntityTest.php

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,22 +490,22 @@ public function testIndirectModification()
490490
public function testHas()
491491
{
492492
$entity = new Entity(['id' => 1, 'name' => 'Juan', 'foo' => null]);
493-
$this->assertTrue($entity->has('id'));
494-
$this->assertTrue($entity->has('name'));
495-
$this->assertFalse($entity->has('foo'));
496-
$this->assertFalse($entity->has('last_name'));
493+
$this->assertTrue($entity->hasProperty('id'));
494+
$this->assertTrue($entity->hasProperty('name'));
495+
$this->assertFalse($entity->hasProperty('foo'));
496+
$this->assertFalse($entity->hasProperty('last_name'));
497497

498-
$this->assertTrue($entity->has(['id']));
499-
$this->assertTrue($entity->has(['id', 'name']));
500-
$this->assertFalse($entity->has(['id', 'foo']));
501-
$this->assertFalse($entity->has(['id', 'nope']));
498+
$this->assertTrue($entity->hasProperty(['id']));
499+
$this->assertTrue($entity->hasProperty(['id', 'name']));
500+
$this->assertFalse($entity->hasProperty(['id', 'foo']));
501+
$this->assertFalse($entity->hasProperty(['id', 'nope']));
502502

503503
$entity = $this->getMockBuilder('\Cake\ORM\Entity')
504504
->setMethods(['_getThings'])
505505
->getMock();
506506
$entity->expects($this->once())->method('_getThings')
507507
->will($this->returnValue(0));
508-
$this->assertTrue($entity->has('things'));
508+
$this->assertTrue($entity->hasProperty('things'));
509509
}
510510

511511
/**
@@ -1573,4 +1573,67 @@ public function testIsDirtyFromClone()
15731573
$this->assertTrue($cloned->dirty('a'));
15741574
$this->assertTrue($cloned->dirty('b'));
15751575
}
1576+
1577+
/**
1578+
* Test the isEmpty() check
1579+
*
1580+
* @return void
1581+
*/
1582+
public function testIsEmpty()
1583+
{
1584+
$entity = new Entity([
1585+
'array' => ['foo' => 'bar'],
1586+
'emptyArray' => [],
1587+
'object' => new \stdClass(),
1588+
'string' => 'string',
1589+
'emptyString' => '',
1590+
'intZero' => 0,
1591+
'intNotZero' => 1,
1592+
'floatZero' => 0.0,
1593+
'floatNonZero' => 1.5,
1594+
'null' => null
1595+
]);
1596+
1597+
$this->assertFalse($entity->isEmpty('array'));
1598+
$this->assertTrue($entity->isEmpty('emptyArray'));
1599+
$this->assertFalse($entity->isEmpty('object'));
1600+
$this->assertFalse($entity->isEmpty('string'));
1601+
$this->assertTrue($entity->isEmpty('emptyString'));
1602+
$this->assertFalse($entity->isEmpty('intZero'));
1603+
$this->assertFalse($entity->isEmpty('intNotZero'));
1604+
$this->assertFalse($entity->isEmpty('floatZero'));
1605+
$this->assertFalse($entity->isEmpty('floatNonZero'));
1606+
$this->assertTrue($entity->isEmpty('null'));
1607+
}
1608+
1609+
/**
1610+
* Test hasValue()
1611+
*
1612+
* @return void
1613+
*/
1614+
public function testHasValue() {
1615+
$entity = new Entity([
1616+
'array' => ['foo' => 'bar'],
1617+
'emptyArray' => [],
1618+
'object' => new \stdClass(),
1619+
'string' => 'string',
1620+
'emptyString' => '',
1621+
'intZero' => 0,
1622+
'intNotZero' => 1,
1623+
'floatZero' => 0.0,
1624+
'floatNonZero' => 1.5,
1625+
'null' => null
1626+
]);
1627+
1628+
$this->assertTrue($entity->hasValue('array'));
1629+
$this->assertFalse($entity->hasValue('emptyArray'));
1630+
$this->assertTrue($entity->hasValue('object'));
1631+
$this->assertTrue($entity->hasValue('string'));
1632+
$this->assertFalse($entity->hasValue('emptyString'));
1633+
$this->assertTrue($entity->hasValue('intZero'));
1634+
$this->assertTrue($entity->hasValue('intNotZero'));
1635+
$this->assertTrue($entity->hasValue('floatZero'));
1636+
$this->assertTrue($entity->hasValue('floatNonZero'));
1637+
$this->assertFalse($entity->hasValue('null'));
1638+
}
15761639
}

0 commit comments

Comments
 (0)