-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add isUniqueChangelog rule. Fix OrcidSource token index issue.
- Loading branch information
Showing
4 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* COmanage Registry Env Sources Table | ||
* | ||
* Portions licensed to the University Corporation for Advanced Internet | ||
* Development, Inc. ("UCAID") under one or more contributor license agreements. | ||
* See the NOTICE file distributed with this work for additional information | ||
* regarding copyright ownership. | ||
* | ||
* UCAID licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with the | ||
* License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @link https://www.internet2.edu/comanage COmanage Project | ||
* @package registry | ||
* @since COmanage Registry v5.2.0 | ||
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
*/ | ||
namespace App\Lib\Rules; | ||
|
||
use Cake\Datasource\EntityInterface; | ||
use Cake\Utility\Hash; | ||
use Cake\Utility\Inflector; | ||
|
||
/** | ||
* Checks that a list of fields from an entity are unique in the table | ||
*/ | ||
class IsUniqueChangelog | ||
{ | ||
/** | ||
* The list of fields to check | ||
* | ||
* @var array<string> | ||
*/ | ||
protected $_fields; | ||
|
||
/** | ||
* The unique check options | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
protected $_options = [ | ||
'allowMultipleNulls' => false, | ||
]; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* ### Options | ||
* | ||
* - `allowMultipleNulls` Allows any field to have multiple null values. Defaults to false. | ||
* | ||
* @param array<string> $fields The list of fields to check uniqueness for | ||
* @param array<string, mixed> $options The options for unique checks. | ||
*/ | ||
public function __construct(array $fields, array $options = []) | ||
{ | ||
$this->_fields = $fields; | ||
$this->_options = $options + $this->_options; | ||
} | ||
|
||
/** | ||
* Performs the uniqueness check | ||
* | ||
* @param \Cake\Datasource\EntityInterface $entity The entity from where to extract the fields | ||
* where the `repository` key is required. | ||
* @param array<string, mixed> $options Options passed to the check, | ||
* @return bool | ||
*/ | ||
public function __invoke(EntityInterface $entity, array $options): bool | ||
{ | ||
if (!$entity->extract($this->_fields, true)) { | ||
return true; | ||
} | ||
|
||
$fields = $entity->extract($this->_fields); | ||
if ($this->_options['allowMultipleNulls'] && array_filter($fields, 'is_null')) { | ||
return true; | ||
} | ||
|
||
$alias = $options['repository']->getAlias(); | ||
$conditions = $this->_alias($alias, $fields); | ||
// Changelog | ||
$ascParentfk = Inflector::singularize($options['repository']->getTable()) . '_id'; | ||
$conditions["$alias.deleted !="] = true; | ||
$conditions["$alias.$ascParentfk IS"] = null; | ||
if ($entity->isNew() === false) { | ||
$keys = (array)$options['repository']->getPrimaryKey(); | ||
$keys = $this->_alias($alias, $entity->extract($keys)); | ||
if (Hash::filter($keys)) { | ||
$conditions['NOT'] = $keys; | ||
} | ||
} | ||
|
||
return !$options['repository']->exists($conditions); | ||
} | ||
|
||
/** | ||
* Add a model alias to all the keys in a set of conditions. | ||
* | ||
* @param string $alias The alias to add. | ||
* @param array $conditions The conditions to alias. | ||
* @return array<string, mixed> | ||
*/ | ||
protected function _alias(string $alias, array $conditions): array | ||
{ | ||
$aliased = []; | ||
foreach ($conditions as $key => $value) { | ||
$aliased["$alias.$key IS"] = $value; | ||
} | ||
|
||
return $aliased; | ||
} | ||
} |