SluggableBehavior.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Behavior for binding management.
  5. *
  6. * Behavior to simplify manipulating a model's bindings when doing a find operation
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  9. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * For full copyright and license information, please see the LICENSE.txt
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  16. * @link https://cakephp.org CakePHP(tm) Project
  17. * @since 1.2.0
  18. * @license https://opensource.org/licenses/mit-license.php MIT License
  19. */
  20. namespace TestApp\Model\Behavior;
  21. use Cake\Event\EventInterface;
  22. use Cake\ORM\Behavior;
  23. use Cake\ORM\Query;
  24. use Cake\Utility\Text;
  25. class SluggableBehavior extends Behavior
  26. {
  27. public function beforeFind(EventInterface $event, Query $query, array $options = []): Query
  28. {
  29. $query->where(['slug' => 'test']);
  30. return $query;
  31. }
  32. public function findNoSlug(Query $query, array $options = []): Query
  33. {
  34. $query->where(['slug IS' => null]);
  35. return $query;
  36. }
  37. public function slugify(string $value): string
  38. {
  39. return Text::slug($value);
  40. }
  41. }