PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ORM/RulesChecker.php

https://github.com/binondord/cakephp
PHP | 92 lines | 32 code | 6 blank | 54 comment | 6 complexity | 44ea678345d3c5cc4d521aba3c86f759 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\ORM;
  16. use Cake\Datasource\RulesChecker as BaseRulesChecker;
  17. use Cake\ORM\Rule\ExistsIn;
  18. use Cake\ORM\Rule\IsUnique;
  19. /**
  20. * ORM flavoured rules checker.
  21. *
  22. * Adds ORM related features to the RulesChecker class.
  23. *
  24. * @see Cake\Datasource\RulesChecker
  25. */
  26. class RulesChecker extends BaseRulesChecker
  27. {
  28. /**
  29. * Returns a callable that can be used as a rule for checking the uniqueness of a value
  30. * in the table.
  31. *
  32. * ### Example:
  33. *
  34. * ```
  35. * $rules->add($rules->isUnique(['email'], 'The email should be unique'));
  36. * ```
  37. *
  38. * @param array $fields The list of fields to check for uniqueness.
  39. * @param string $message The error message to show in case the rule does not pass.
  40. * @return callable
  41. */
  42. public function isUnique(array $fields, $message = null)
  43. {
  44. if (!$message) {
  45. if ($this->_useI18n) {
  46. $message = __d('cake', 'This value is already in use');
  47. } else {
  48. $message = 'This value is already in use';
  49. }
  50. }
  51. $errorField = current($fields);
  52. return $this->_addError(new IsUnique($fields), '_isUnique', compact('errorField', 'message'));
  53. }
  54. /**
  55. * Returns a callable that can be used as a rule for checking that the values
  56. * extracted from the entity to check exist as the primary key in another table.
  57. *
  58. * This is useful for enforcing foreign key integrity checks.
  59. *
  60. * ### Example:
  61. *
  62. * ```
  63. * $rules->add($rules->existsIn('author_id', 'Authors', 'Invalid Author'));
  64. *
  65. * $rules->add($rules->existsIn('site_id', new SitesTable(), 'Invalid Site'));
  66. * ```
  67. *
  68. * @param string|array $field The field or list of fields to check for existence by
  69. * primary key lookup in the other table.
  70. * @param object|string $table The table name where the fields existence will be checked.
  71. * @param string $message The error message to show in case the rule does not pass.
  72. * @return callable
  73. */
  74. public function existsIn($field, $table, $message = null)
  75. {
  76. if (!$message) {
  77. if ($this->_useI18n) {
  78. $message = __d('cake', 'This value does not exist');
  79. } else {
  80. $message = 'This value does not exist';
  81. }
  82. }
  83. $errorField = is_string($field) ? $field : current($field);
  84. return $this->_addError(new ExistsIn($field, $table), '_existsIn', compact('errorField', 'message'));
  85. }
  86. }