44namespace hiqdev \php \billing \product \price ;
55
66use hiqdev \php \billing \product \AggregateInterface ;
7- use hiqdev \php \billing \product \behavior \BehaviorCollectionInterface ;
8- use hiqdev \php \billing \product \behavior \BehaviorInterface ;
97use hiqdev \php \billing \product \behavior \HasBehaviorsInterface ;
8+ use hiqdev \php \billing \product \behavior \PriceTypeBehaviorRegistry ;
109use hiqdev \php \billing \product \Exception \AggregateNotDefinedException ;
1110use hiqdev \php \billing \product \behavior \BehaviorPriceTypeDefinitionCollection ;
1211use hiqdev \php \billing \product \invoice \RepresentationCollection ;
2221use hiqdev \php \billing \product \TariffTypeDefinitionInterface ;
2322use hiqdev \php \billing \product \trait \HasLock ;
2423use hiqdev \php \billing \type \TypeInterface ;
24+ use function class_exists ;
2525
2626/**
2727 * @template TParentCollection
@@ -46,16 +46,13 @@ class PriceTypeDefinition implements PriceTypeDefinitionInterface
4646 */
4747 private RepresentationCollection $ representationCollection ;
4848
49- /**
50- * @var BehaviorPriceTypeDefinitionCollection<PriceTypeDefinition>
51- */
52- private BehaviorPriceTypeDefinitionCollection $ behaviorCollection ;
53-
5449 private ?AggregateInterface $ aggregate = null ;
5550
5651 /** @psalm-var TParentCollection */
5752 private readonly PriceTypeDefinitionCollectionInterface $ parent ;
5853
54+ private readonly PriceTypeBehaviorRegistry $ behaviorRegistry ;
55+
5956 /**
6057 * @param TParentCollection $parent
6158 */
@@ -66,7 +63,7 @@ public function __construct(
6663 ) {
6764 $ this ->parent = $ parent ;
6865 $ this ->representationCollection = new RepresentationCollection ($ this );
69- $ this ->behaviorCollection = new BehaviorPriceTypeDefinitionCollection ($ this , $ tariffType );
66+ $ this ->behaviorRegistry = new PriceTypeBehaviorRegistry ($ this , $ tariffType );
7067
7168 $ this ->init ();
7269 }
@@ -107,7 +104,7 @@ public function quantityFormatter(string $formatterClass, $fractionUnit = null):
107104 {
108105 $ this ->ensureNotLocked ();
109106
110- if (!\ class_exists ($ formatterClass )) {
107+ if (!class_exists ($ formatterClass )) {
111108 throw new InvalidQuantityFormatterException ("Formatter class $ formatterClass does not exist " );
112109 }
113110
@@ -181,29 +178,17 @@ public function withBehaviors()
181178 {
182179 $ this ->ensureNotLocked ();
183180
184- return $ this ->behaviorCollection ;
181+ return $ this ->behaviorRegistry -> withBehaviors () ;
185182 }
186183
187184 public function hasBehavior (string $ behaviorClassName ): bool
188185 {
189- foreach ($ this ->behaviorCollection as $ behavior ) {
190- if ($ behavior instanceof $ behaviorClassName ) {
191- return true ;
192- }
193- }
194-
195- return false ;
186+ return $ this ->behaviorRegistry ->hasBehavior ($ behaviorClassName );
196187 }
197188
198- public function findBehaviorByClass (string $ class ): ? BehaviorInterface
189+ public function findBehaviorByClass (string $ class )
199190 {
200- foreach ($ this ->behaviorCollection as $ behavior ) {
201- if ($ behavior instanceof $ class ) {
202- return $ behavior ;
203- }
204- }
205-
206- return null ;
191+ return $ this ->behaviorRegistry ->findBehaviorByClass ($ class );
207192 }
208193
209194 /**
@@ -242,7 +227,7 @@ public function getQuantityFormatterDefinition(): ?QuantityFormatterDefinition
242227 protected function afterLock (): void
243228 {
244229 $ this ->representationCollection ->lock ();
245- $ this ->behaviorCollection ->lock ();
230+ $ this ->behaviorRegistry ->lock ();
246231 }
247232
248233 public function getTariffTypeDefinition (): TariffTypeDefinitionInterface
0 commit comments