original_string: public static function buildFrom(StreamEvents $events): Aggregate {
          $aggregate = new static;
          $events->each(function (StreamEvent $event) use ($aggregate) {
              $aggregate->apply($event->event());
              if ( ! $event->version()->equals($aggregate->aggregateVersion())) {
                  throw new UnexpectedAggregateVersionWhenBuildingFromEvents("When building from stream events aggregate {$aggregate->aggregateId()->toString()} expected version {$event->version()->toInt()} but got {$aggregate->aggregateVersion()->toInt()}.");
              }
          });
          return $aggregate;
      }
code: public static function buildFrom(StreamEvents $events): Aggregate {
          $aggregate = new static;
          $events->each(function (StreamEvent $event) use ($aggregate) {
              $aggregate->apply($event->event());
              if ( ! $event->version()->equals($aggregate->aggregateVersion())) {
                  throw new UnexpectedAggregateVersionWhenBuildingFromEvents("When building from stream events aggregate {$aggregate->aggregateId()->toString()} expected version {$event->version()->toInt()} but got {$aggregate->aggregateVersion()->toInt()}.");
              }
          });
          return $aggregate;
      }
code_tokens: public static function buildFrom ( StreamEvents $ events ) : Aggregate { $ aggregate = new static ; $ events -> each ( function ( StreamEvent $ event ) use ( $ aggregate ) { $ aggregate -> apply ( $ event -> event ( ) ) ; if ( ! $ event -> version ( ) -> equals ( $ aggregate -> aggregateVersion ( ) ) ) { throw new UnexpectedAggregateVersionWhenBuildingFromEvents ( "When building from stream events aggregate {$aggregate->aggregateId()->toString()} expected version {$event->version()->toInt()} but got {$aggregate->aggregateVersion()->toInt()}." ) ; } } ) ; return $ aggregate ; }
docstring: reconstruct the aggregate state from domain events
  
  @param StreamEvents $events
  @return Aggregate
docstring_tokens: reconstruct the aggregate state from domain events