Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 89 additions & 4 deletions src/Attribute/Complex.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,24 @@ public function patch($operation, $value, Model &$object, Path $path = null, $re
}
}

if (empty($matchedIndexes)) {
return;
}

$attributeNames = $path?->getAttributePath()?->getAttributeNames() ?? [];
$modified = false;

if (empty($matchedIndexes)) {
if ($operation === 'add') {
$newElement = $this->createElementFromFilter($filterNode, $attributeNames, $value);

if ($newElement !== null) {
$currentValues[] = $newElement;
$modified = true;
}
}

if (!$modified) {
return;
}
}

foreach ($matchedIndexes as $index) {
if (empty($attributeNames)) {
if ($operation === 'remove') {
Expand Down Expand Up @@ -638,4 +649,78 @@ private function restoreStructure(array $original, array $normalized): array

return $result;
}

private function createElementFromFilter(AstFilter $filter, array $attributePath, mixed $value): ?array
{
$base = $this->extractAssignmentsFromFilter($filter);

if (!empty($attributePath)) {
$base = $this->setNestedValue($base, $attributePath, $value);
} elseif (is_array($value)) {
$base = array_replace_recursive($base, $this->normalizeElement($value));
} else {
return null;
}

return $this->normalizeElement($base);
}

private function extractAssignmentsFromFilter(AstFilter $filter): array
{
if ($filter instanceof ComparisonExpression) {
if (strtolower($filter->operator) !== 'eq') {
return [];
}

$attributeNames = $filter->attributePath->getAttributeNames();

if (empty($attributeNames)) {
return [];
}

return $this->setNestedValue([], $attributeNames, $filter->compareValue);
}

if ($filter instanceof Conjunction) {
$result = [];

foreach ($filter->getFactors() as $factor) {
$result = array_replace_recursive($result, $this->extractAssignmentsFromFilter($factor));
}

return $result;
}

if ($filter instanceof AstValuePath) {
$nested = $this->extractAssignmentsFromFilter($filter->getFilter());

$attributeNames = $filter->getAttributePath()->getAttributeNames();

return $this->setNestedValue([], $attributeNames, $nested);
}

return [];
}

private function setNestedValue(array $array, array $path, mixed $value): array
{
if (empty($path)) {
return is_array($value) ? array_replace_recursive($array, $value) : $array;
}

$segment = array_shift($path);

if (!array_key_exists($segment, $array) || !is_array($array[$segment])) {
$array[$segment] = [];
}

if (empty($path)) {
$array[$segment] = $value;
return $array;
}

$array[$segment] = $this->setNestedValue($array[$segment], $path, $value);

return $array;
}
}
82 changes: 82 additions & 0 deletions tests/EmailValuePathAddTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace ArieTimmerman\Laravel\SCIMServer\Tests;

use ArieTimmerman\Laravel\SCIMServer\Attribute\Complex;
use ArieTimmerman\Laravel\SCIMServer\Parser\Parser;
use ArieTimmerman\Laravel\SCIMServer\Parser\Path;
use ArieTimmerman\Laravel\SCIMServer\Tests\Model\User;
use Illuminate\Database\Eloquent\Model;

class EmailValuePathAddTest extends TestCase
{
public function testAddOperationUpdatesEmailValue(): void
{
$user = User::query()->firstOrFail();

$payload = [
'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
'Operations' => [
[
'op' => 'Add',
'path' => 'emails[type eq "other"].value',
'value' => '[email protected]',
],
],
];

$response = $this->patchJson('/scim/v2/Users/' . $user->id, $payload);

$response->assertStatus(200);

$user->refresh();

$this->assertSame('[email protected]', $user->email);
}

public function testAddOperationCreatesNewElementWhenFilterMatchesNone(): void
{
$model = new class extends Model {
protected $table = 'users';
public $timestamps = false;
};

$model->emails = [
['value' => '[email protected]', 'type' => 'work'],
];

$attribute = $this->makeEmailAttribute();

$path = Parser::parse('emails[type eq "other"].value');
$path->shiftValuePathAttributes();

$attribute->patch('add', '[email protected]', $model, $path);

$this->assertCount(2, $model->emails);
$this->assertSame('[email protected]', $model->emails[0]['value']);
$this->assertSame('other', $model->emails[1]['type']);
$this->assertSame('[email protected]', $model->emails[1]['value']);
}

private function makeEmailAttribute(): Complex
{
return new class('emails') extends Complex {
public function __construct($name)
{
parent::__construct($name);
$this->setMultiValued(true);
}

protected function doRead(&$object, $attributes = [])
{
return $object->{$this->name} ?? [];
}

public function replace($value, Model &$object, Path $path = null, $removeIfNotSet = false)
{
$object->{$this->name} = $value;
$this->dirty = true;
}
};
}
}