PluginProbe
QuadMenu – Mega Menu / 3.3.8
QuadMenu – Mega Menu v3.3.8
3.3.8 3.3.7 3.3.6 trunk 1.8.4 1.8.5 1.8.7 1.8.8 1.8.9 1.9.0 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 88 releases
quadmenu / vendor / franmastromarino / wp-orm / src / Factory / AbstractFactory.php

AbstractFactory.php in QuadMenu – Mega Menu 3.3.8, at vendor/franmastromarino/wp-orm/src/Factory/AbstractFactory.php

57 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace QuadLayers\WP_Orm\Factory;
4
5 use QuadLayers\WP_Orm\Entity\EntityInterface;
6
7 use function QuadLayers\WP_Orm\V2\Helpers\getObjectSchema;
8 use function QuadLayers\WP_Orm\V2\Helpers\getSanitizedData;
9
10 abstract class AbstractFactory
11 {
12 /**
13 * @var string
14 */
15 private $entityClass;
16
17 public function __construct(string $entityClass)
18 {
19 $this->entityClass = $entityClass;
20 }
21
22 public function create(array $data): EntityInterface
23 {
24 // Create a new instance of the entity
25 $entity = new $this->entityClass();
26
27 // Get the default values of the entity, new static() is used to get the defaults of the child class
28 $entityDefaults = $entity->getDefaults();
29
30 $entitySchema = getObjectSchema($entityDefaults);
31
32 $sanitizedData = getSanitizedData($data, $entitySchema);
33
34 // Loop through each data item
35 foreach ($sanitizedData as $propertyName => $value) {
36 // Set the value of the property
37 $entity->set($propertyName, $value);
38 }
39
40 // Get private properties of the entity
41 $entityPrivates = $entity::PRIVATE_PROPERTIES;
42
43 // Check if the entity has private properties and update them
44 if (count($entityPrivates) > 0) {
45 // Loop through each private property
46 foreach ($entityPrivates as $propertyName) {
47 if (array_key_exists($propertyName, $data)) {
48 // Set the value of the property
49 $entity->set($propertyName, $data[$propertyName]);
50 }
51 }
52 }
53
54 return $entity;
55 }
56 }
57