DisjunctExclusionStrategy.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\Serializer\Exclusion;
  18. use JMS\Serializer\Context;
  19. use JMS\Serializer\Metadata\ClassMetadata;
  20. use JMS\Serializer\Metadata\PropertyMetadata;
  21. use PhpCollection\Sequence;
  22. use PhpCollection\SequenceInterface;
  23. /**
  24. * Disjunct Exclusion Strategy.
  25. *
  26. * This strategy is short-circuiting and will skip a class, or property as soon as one of the delegates skips it.
  27. *
  28. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  29. */
  30. class DisjunctExclusionStrategy implements ExclusionStrategyInterface
  31. {
  32. /** @var \PhpCollection\SequenceInterface */
  33. private $delegates;
  34. /**
  35. * @param ExclusionStrategyInterface[]|SequenceInterface $delegates
  36. */
  37. public function __construct($delegates)
  38. {
  39. if ( ! $delegates instanceof SequenceInterface) {
  40. $delegates = new Sequence($delegates);
  41. }
  42. $this->delegates = $delegates;
  43. }
  44. public function addStrategy(ExclusionStrategyInterface $strategy)
  45. {
  46. $this->delegates->add($strategy);
  47. }
  48. /**
  49. * Whether the class should be skipped.
  50. *
  51. * @param ClassMetadata $metadata
  52. * @param Context $navigatorContext
  53. *
  54. * @return boolean
  55. */
  56. public function shouldSkipClass(ClassMetadata $metadata, Context $context)
  57. {
  58. foreach ($this->delegates as $delegate) {
  59. /** @var $delegate ExclusionStrategyInterface */
  60. if ($delegate->shouldSkipClass($metadata, $context)) {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. /**
  67. * Whether the property should be skipped.
  68. *
  69. * @param PropertyMetadata $property
  70. * @param Context $navigatorContext
  71. *
  72. * @return boolean
  73. */
  74. public function shouldSkipProperty(PropertyMetadata $property, Context $context)
  75. {
  76. foreach ($this->delegates as $delegate) {
  77. /** @var $delegate ExclusionStrategyInterface */
  78. if ($delegate->shouldSkipProperty($property, $context)) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. }