Skip to content

Commit

Permalink
Feature/model validation (#45)
Browse files Browse the repository at this point in the history
* Add model validation event

* change version

* add model validation event

* Add groups to ModelValidationEvent.php

Co-authored-by: nbarbey-os-alter <nbarbey@openstudio.fr>
  • Loading branch information
NicolasBarbey and nbarbey-os-alter authored Jan 10, 2022
1 parent 22eea70 commit 63583ba
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.17</version>
<version>1.0.18</version>
<authors>
<author>
<name>Vincent Lopes-Vicente</name>
Expand Down
124 changes: 124 additions & 0 deletions Events/ModelValidationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace OpenApi\Events;

use OpenApi\Model\Api\BaseApiModel;
use OpenApi\Model\Api\ModelFactory;
use Thelia\Core\Event\ActionEvent;

class ModelValidationEvent extends ActionEvent
{

const MODEL_VALIDATION_EVENT_PREFIX = 'open_api_model_validation_';

/** @var BaseApiModel $model */
protected $model;

/** @var ModelFactory */
protected $modelFactory;

protected $propertyPatchPrefix;

/** @var array $violations */
protected $violations;

protected $groups;

/**
* @param BaseApiModel $model
* @param ModelFactory $modelFactory
* @param $groups
* @param string $propertyPatchPrefix
* @param array $violations
*/
public function __construct(BaseApiModel $model, ModelFactory $modelFactory, $groups, string $propertyPatchPrefix = "", array $violations = [])
{
$this->model = $model;
$this->modelFactory = $modelFactory;
$this->propertyPatchPrefix = $propertyPatchPrefix;
$this->violations = $violations;
$this->groups = $groups;
}


/**
* @return BaseApiModel
*/
public function getModel(): BaseApiModel
{
return $this->model;
}

/**
* @param BaseApiModel $model
*/
public function setModel(BaseApiModel $model): void
{
$this->model = $model;
}

/**
* @return array
*/
public function getViolations()
{
return $this->violations;
}

/**
* @param array $violations
*/
public function setViolations($violations): void
{
$this->violations = $violations;
}

/**
* @return ModelFactory
*/
public function getModelFactory(): ModelFactory
{
return $this->modelFactory;
}

/**
* @param ModelFactory $modelFactory
*/
public function setModelFactory(ModelFactory $modelFactory): void
{
$this->modelFactory = $modelFactory;
}

/**
* @return string
*/
public function getPropertyPatchPrefix(): string
{
return $this->propertyPatchPrefix;
}

/**
* @param string $propertyPatchPrefix
*/
public function setPropertyPatchPrefix(string $propertyPatchPrefix): void
{
$this->propertyPatchPrefix = $propertyPatchPrefix;
}

/**
* @return mixed
*/
public function getGroups()
{
return $this->groups;
}

/**
* @param mixed $groups
*/
public function setGroups($groups): void
{
$this->groups = $groups;
}

}
6 changes: 5 additions & 1 deletion Model/Api/BaseApiModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OpenApi\Model\Api;

use OpenApi\Events\ModelExtendDataEvent;
use OpenApi\Events\ModelValidationEvent;
use OpenApi\Exception\OpenApiException;
use OpenApi\Normalizer\ModelApiNormalizer;
use OpenApi\OpenApi;
Expand Down Expand Up @@ -114,7 +115,10 @@ public function getViolations($groups, $recursively = true, $propertyPatchPrefix
}
}

return $violations;
$event = new ModelValidationEvent($this, $modelFactory, $groups, $propertyPatchPrefix);
$this->dispatcher->dispatch(ModelValidationEvent::MODEL_VALIDATION_EVENT_PREFIX.$this->snakeCaseName(), $event);

return array_merge($violations, $event->getViolations());
}

public function jsonSerialize()
Expand Down

0 comments on commit 63583ba

Please sign in to comment.