Spaces:
No application file
No application file
File size: 7,191 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
<?php
namespace Mautic\ApiBundle\Serializer\Driver;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use Metadata\ClassMetadata as BaseClassMetadata;
use Metadata\Driver\DriverInterface;
class ApiMetadataDriver implements DriverInterface
{
private ?ClassMetadata $metadata = null;
/**
* @var PropertyMetadata[]
*/
private $properties = [];
private string $groupPrefix = '';
/**
* @var null
*/
private $defaultVersion = '1.0';
/**
* @var null
*/
private $currentPropertyName;
/**
* @throws \ReflectionException
*/
public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata
{
if ($class->hasMethod('loadApiMetadata')) {
$this->metadata = new ClassMetadata($class->getName());
$class->getMethod('loadApiMetadata')->invoke(null, $this);
$metadata = $this->metadata;
$this->resetDefaults();
return $metadata;
} else {
return new ClassMetadata($class->getName());
}
}
private function resetDefaults(): void
{
$this->metadata = null;
$this->properties = [];
$this->defaultVersion = '1.0';
$this->groupPrefix = '';
}
/**
* Set the root (base key).
*
* @return $this
*/
public function setRoot($root)
{
$this->metadata->xmlRootName = $root;
return $this;
}
/**
* Set prefix for the List and Details groups.
*
* @return $this
*/
public function setGroupPrefix($name)
{
$this->groupPrefix = $name;
return $this;
}
/**
* Set the default version for the properties if different than 1.0.
*
* @return $this
*/
public function setDefaultVersion($version)
{
$this->defaultVersion = $version;
return $this;
}
/**
* Create a new property.
*
* @return $this
*/
public function createProperty($name)
{
if (!isset($this->properties[$name])) {
$this->properties[$name] = new PropertyMetadata($this->metadata->name, $name);
}
$this->currentPropertyName = $name;
return $this;
}
/**
* Add property and set default version and Details group.
*
* @param bool $useGetter
*
* @return $this
*/
public function addProperty($name, $serializedName = null, $useGetter = false)
{
if (empty($name)) {
return $this;
}
$this->createProperty($name);
if ($useGetter && !$this->properties[$name]->getter) {
$this->properties[$name]->getter = 'get'.ucfirst($name);
}
$this->properties[$name]->serializedName = $serializedName ?? $name;
if (null !== $this->defaultVersion) {
// Set the default version
$this->setSinceVersion($this->defaultVersion);
}
$this->addGroup($this->groupPrefix.'Details');
return $this;
}
/**
* Create properties.
*
* @param bool|false $addToListGroup
* @param bool|false $useGetter
*
* @return $this
*/
public function addProperties(array $properties, $addToListGroup = false, $useGetter = false)
{
foreach ($properties as $prop) {
if (!empty($prop)) {
$serializedName = null;
if (is_array($prop)) {
[$prop, $serializedName] = $prop;
}
$this->addProperty($prop, $serializedName, $useGetter);
if ($addToListGroup) {
$this->inListGroup();
}
}
}
return $this;
}
/**
* Create properties and add to the List group.
*
* @return $this
*/
public function addListProperties(array $properties)
{
$this->addProperties($properties, true);
return $this;
}
/**
* @return $this
*/
public function setSinceVersion($version, $property = null)
{
if (null === $property) {
$property = $this->getCurrentPropertyName();
}
$this->properties[$property]->sinceVersion = $version;
return $this;
}
/**
* @return $this
*/
public function setUntilVersion($version, $property = null)
{
if (null === $property) {
$property = $this->getCurrentPropertyName();
}
$this->properties[$property]->untilVersion = $version;
return $this;
}
/**
* @return $this
*/
public function setSerializedName($name, $property = null)
{
if (null === $property) {
$property = $this->getCurrentPropertyName();
}
$this->properties[$property]->serializedName = $name;
return $this;
}
/**
* Set the groups a property belongs to.
*
* @return $this
*/
public function setGroups($groups, $property = null)
{
if (!is_array($groups)) {
$groups = [$groups];
}
if (null === $property) {
$property = $this->getCurrentPropertyName();
}
$this->properties[$property]->groups = $groups;
return $this;
}
/**
* Add a group the property belongs to.
*
* @param mixed $property
*
* @return $this
*/
public function addGroup($group, $property = null)
{
if (true === $property) {
foreach ($this->properties as $prop => $metadata) {
$this->addGroup($group, $prop);
}
} else {
if (null === $property) {
$property = $this->getCurrentPropertyName();
}
$this->properties[$property]->groups[] = $group;
}
return $this;
}
/**
* Add property to the List group.
*
* @return $this
*/
public function inListGroup()
{
$this->properties[$this->currentPropertyName]->groups[] =
$this->groupPrefix.'List';
return $this;
}
/**
* Set max depth for the property if an association.
*
* @return $this
*/
public function setMaxDepth($depth, $property = null)
{
if (null === $property) {
$property = $this->getCurrentPropertyName();
}
$this->properties[$property]->maxDepth = (int) $depth;
return $this;
}
/**
* Push the properties into ClassMetadata.
*/
public function build(): void
{
foreach ($this->properties as $prop) {
$this->metadata->addPropertyMetadata($prop);
}
$this->currentPropertyName = null;
$this->properties = [];
}
/**
* @return string
*
* @throws \Exception
*/
protected function getCurrentPropertyName()
{
if (empty($this->currentPropertyName)) {
throw new \Exception('Current property is not set');
}
return $this->currentPropertyName;
}
}
|