Skip to content

Commit d547688

Browse files
authored
Merge pull request #96 from ltb-project/95-module-with-functions-to-check-for-mandatory-attributes
Add module for mandatory attributes check
2 parents 94b49bc + c809be2 commit d547688

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

src/Ltb/Attributes.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Ltb;
4+
5+
class Attributes {
6+
public static function isMandatoryAttribute($config, $operation) {
7+
if (!isset($config['mandatory']) || !is_array($config['mandatory'])) {
8+
return false;
9+
}
10+
11+
return in_array('all', $config['mandatory']) || in_array($operation, $config['mandatory']);
12+
}
13+
14+
public static function mandatoryAttributeHasValue($value) {
15+
if (is_array($value)) {
16+
foreach ($value as $item) {
17+
if ($item !== null && $item !== '') {
18+
return true;
19+
}
20+
}
21+
22+
return false;
23+
}
24+
25+
return $value !== null && $value !== '';
26+
}
27+
28+
public static function findMissingMandatoryAttributes($operation, $attributes_map, $entry_attributes, $items = null) {
29+
$missing = array();
30+
31+
foreach ($attributes_map as $item => $config) {
32+
if ($items !== null && !in_array($item, $items, true)) {
33+
continue;
34+
}
35+
36+
if (!self::isMandatoryAttribute($config, $operation)) {
37+
continue;
38+
}
39+
40+
$attribute = $config['attribute'] ?? null;
41+
if (!$attribute || !self::mandatoryAttributeHasValue($entry_attributes[$attribute] ?? null)) {
42+
$missing[] = $item;
43+
}
44+
}
45+
46+
return $missing;
47+
}
48+
}

tests/Ltb/AttributesTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
require __DIR__ . '/../../vendor/autoload.php';
4+
use PHPUnit\Framework\TestCase;
5+
6+
class MandatoryAttributesTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
7+
{
8+
private function getAttributesMap()
9+
{
10+
return array(
11+
'firstname' => array('attribute' => 'givenname', 'mandatory' => array('all')),
12+
'lastname' => array('attribute' => 'sn', 'mandatory' => array('create')),
13+
'fullname' => array('attribute' => 'cn', 'mandatory' => array('update')),
14+
'title' => array('attribute' => 'title'),
15+
);
16+
}
17+
18+
public function testCreateRequiresMandatoryValues()
19+
{
20+
$missing = (new \Ltb\Attributes)->findMissingMandatoryAttributes(
21+
'create',
22+
$this->getAttributesMap(),
23+
array(
24+
'givenname' => array('Alice'),
25+
'cn' => 'Alice Doe',
26+
)
27+
);
28+
29+
$this->assertSame(array('lastname'), $missing);
30+
}
31+
32+
public function testCreateAcceptsMandatoryMacroValue()
33+
{
34+
$missing = (new \Ltb\Attributes)->findMissingMandatoryAttributes(
35+
'create',
36+
$this->getAttributesMap(),
37+
array(
38+
'givenname' => array('Alice'),
39+
'sn' => 'Doe',
40+
)
41+
);
42+
43+
$this->assertSame(array(), $missing);
44+
}
45+
46+
public function testUpdateChecksOnlyProposedMandatoryAttributes()
47+
{
48+
$missing = (new \Ltb\Attributes)->findMissingMandatoryAttributes(
49+
'update',
50+
$this->getAttributesMap(),
51+
array(
52+
'givenname' => array(),
53+
'cn' => array(),
54+
),
55+
array('fullname')
56+
);
57+
58+
$this->assertSame(array('fullname'), $missing);
59+
}
60+
61+
public function testUpdateIgnoresMandatoryAttributesNotProposedForChange()
62+
{
63+
$missing = (new \Ltb\Attributes)->findMissingMandatoryAttributes(
64+
'update',
65+
$this->getAttributesMap(),
66+
array(
67+
'givenname' => array(),
68+
'cn' => array(),
69+
),
70+
array('title')
71+
);
72+
73+
$this->assertSame(array(), $missing);
74+
}
75+
}

0 commit comments

Comments
 (0)