diff --git a/src/Mapping/Mapping.php b/src/Mapping/Mapping.php index fbce27c..19ff88f 100644 --- a/src/Mapping/Mapping.php +++ b/src/Mapping/Mapping.php @@ -6,82 +6,45 @@ class Mapping { - /** - * @var string - */ + /** @var array */ + protected $requiredProperties = []; + /** @var string */ private $className = ''; - /** - * @var string - */ + /** @var string */ private $resourceUrlPattern = ''; - /** - * @var string - */ + /** @var string */ private $classAlias = ''; - /** - * @var array - */ + /** @var array*/ private $aliasedProperties = []; - /** - * @var array - */ + /** @var array */ private $hiddenProperties = []; - /** - * @var array - */ + /** @var array */ private $idProperties = []; - /** - * @var array - */ + /** @var array */ private $relationships = []; - /** - * @var array - */ + /** @var array */ private $metaData = []; - /** - * @var string - */ + /** @var string */ private $selfUrl = ''; - - /** - * @var array - */ + /** @var array */ private $otherUrls = []; - - /** - * @var array - */ + /** @var array */ private $relationshipSelfUrl = []; - - /** - * @var array - */ + /** @var array */ private $filterKeys = []; - - /** - * @var array - */ + /** @var array */ private $curies = []; - - /** - * @var array - */ + /** @var array */ private $properties = []; - - /** - * @var array - */ + /** @var array */ private $includedKeys = []; - - /** - * @var bool - */ + /** @var bool */ private $filteringIncluded = true; /** - * @param $className - * @param null $resourceUrlPattern - * @param array $idProperties + * @param string $className + * @param null $resourceUrlPattern + * @param array $idProperties */ public function __construct($className, $resourceUrlPattern = null, array $idProperties = []) { @@ -422,4 +385,20 @@ public function isFilteringIncludedResources() { return $this->filteringIncluded; } + + /** + * @param array $requiredProperties + */ + public function setRequiredProperties(array $requiredProperties) + { + $this->requiredProperties = $requiredProperties; + } + + /** + * @return array + */ + public function getRequiredProperties() + { + return $this->requiredProperties; + } } diff --git a/src/Mapping/MappingFactory.php b/src/Mapping/MappingFactory.php index 50a4664..6475270 100644 --- a/src/Mapping/MappingFactory.php +++ b/src/Mapping/MappingFactory.php @@ -23,6 +23,7 @@ class MappingFactory const CLASS_KEY = 'class'; const ALIAS_KEY = 'alias'; const ALIASED_PROPERTIES_KEY = 'aliased_properties'; + const REQUIRED_PROPERTIES_KEY = 'required_properties'; const HIDE_PROPERTIES_KEY = 'hide_properties'; const ID_PROPERTIES_KEY = 'id_properties'; const URLS_KEY = 'urls'; @@ -68,6 +69,7 @@ public static function fromClass($className) static::HIDE_PROPERTIES_KEY => $instance->getHideProperties(), static::ID_PROPERTIES_KEY => $instance->getIdProperties(), static::URLS_KEY => $instance->getUrls(), + static::REQUIRED_PROPERTIES_KEY => $instance->getRequiredProperties(), ]; if (\in_array(HalMapping::class, \class_implements($instance, true))) { @@ -102,6 +104,7 @@ public static function fromArray(array &$mappedClass) static::setRelationships($mappedClass, $mapping, $className); static::setCuries($mappedClass, $mapping); static::setProperties($mapping, $className); + static::setRequiredProperties($mappedClass, $mapping, $className); $otherUrls = static::getOtherUrls($mappedClass); if (!empty($otherUrls)) { @@ -300,4 +303,27 @@ protected static function getOtherUrls(array $mappedClass) return $mappedClass[static::URLS_KEY]; } + + /** + * @param array $mappedClass + * @param Mapping $mapping + * @param $className + */ + protected static function setRequiredProperties(array &$mappedClass, Mapping $mapping, $className) + { + if (false === empty($mappedClass[static::REQUIRED_PROPERTIES_KEY])) { + $mapping->setRequiredProperties($mappedClass[static::REQUIRED_PROPERTIES_KEY]); + foreach (\array_keys($mapping->getRequiredProperties()) as $propertyName) { + if (false === \in_array($propertyName, static::getClassProperties($className), true)) { + throw new MappingException( + \sprintf( + 'Could not add required property %s in class %s because it does not exist.', + $propertyName, + $className + ) + ); + } + } + } + } } diff --git a/src/Mappings/ApiMapping.php b/src/Mappings/ApiMapping.php index e399e6e..cb928f5 100644 --- a/src/Mappings/ApiMapping.php +++ b/src/Mappings/ApiMapping.php @@ -46,4 +46,11 @@ public function getIdProperties(); * @return array */ public function getUrls(); + + /** + * Returns an array of properties that are mandatory to be passed in when doing create or update. + * + * @return array + */ + public function getRequiredProperties(); } diff --git a/tests/Dummy/PostApiMapping.php b/tests/Dummy/PostApiMapping.php index 2d5844d..f8bc89a 100644 --- a/tests/Dummy/PostApiMapping.php +++ b/tests/Dummy/PostApiMapping.php @@ -90,4 +90,14 @@ public function getRelationships() ], ]; } + + /** + * Returns an array of properties that are mandatory to be passed in when doing create or update. + * + * @return array + */ + public function getRequiredProperties() + { + return []; + } }