Skip to content

Commit

Permalink
Support for getRequiredProperties feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil Portugues Caldero committed Aug 11, 2016
1 parent 0379e11 commit 4e27435
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 58 deletions.
95 changes: 37 additions & 58 deletions src/Mapping/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
{
Expand Down Expand Up @@ -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;
}
}
26 changes: 26 additions & 0 deletions src/Mapping/MappingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
)
);
}
}
}
}
}
7 changes: 7 additions & 0 deletions src/Mappings/ApiMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
10 changes: 10 additions & 0 deletions tests/Dummy/PostApiMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
}

0 comments on commit 4e27435

Please sign in to comment.