summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/orm/src/Contract/PropertyBehavior.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ipl/orm/src/Contract/PropertyBehavior.php')
-rw-r--r--vendor/ipl/orm/src/Contract/PropertyBehavior.php102
1 files changed, 102 insertions, 0 deletions
diff --git a/vendor/ipl/orm/src/Contract/PropertyBehavior.php b/vendor/ipl/orm/src/Contract/PropertyBehavior.php
new file mode 100644
index 0000000..c828458
--- /dev/null
+++ b/vendor/ipl/orm/src/Contract/PropertyBehavior.php
@@ -0,0 +1,102 @@
+<?php
+
+namespace ipl\Orm\Contract;
+
+use ipl\Orm\Model;
+use OutOfBoundsException;
+
+abstract class PropertyBehavior implements RetrieveBehavior, PersistBehavior
+{
+ /** @var array Property names of which the value should be processed */
+ protected $properties;
+
+ /**
+ * PropertyBehavior constructor
+ *
+ * @param array $properties Property names to process, as values
+ */
+ public function __construct(array $properties)
+ {
+ if (is_int(key($properties))) {
+ $this->properties = array_flip($properties);
+ } else {
+ $this->properties = $properties;
+ }
+ }
+
+ public function retrieve(Model $model)
+ {
+ foreach ($this->properties as $key => $ctx) {
+ if ($model->hasProperty($key)) {
+ $model[$key] = $this->fromDb($model[$key], $key, $ctx);
+ }
+ }
+ }
+
+ public function persist(Model $model)
+ {
+ foreach ($this->properties as $key => $ctx) {
+ try {
+ $model[$key] = $this->toDb($model[$key], $key, $ctx);
+ } catch (OutOfBoundsException $_) {
+ // pass
+ }
+ }
+ }
+
+ /**
+ * Transform the given value, just fetched from the database
+ *
+ * @param mixed $value
+ * @param string $key
+ *
+ * @return mixed
+ */
+ public function retrieveProperty($value, $key)
+ {
+ if (! isset($this->properties[$key])) {
+ return $value;
+ }
+
+ return $this->fromDb($value, $key, $this->properties[$key]);
+ }
+
+ /**
+ * Transform the given value, about to be persisted to the database
+ *
+ * @param mixed $value
+ * @param string $key
+ *
+ * @return mixed
+ */
+ public function persistProperty($value, $key)
+ {
+ if (! isset($this->properties[$key])) {
+ return $value;
+ }
+
+ return $this->toDb($value, $key, $this->properties[$key]);
+ }
+
+ /**
+ * Transform the given value which has just been fetched from the database
+ *
+ * @param mixed $value
+ * @param string $key
+ * @param mixed $context
+ *
+ * @return mixed
+ */
+ abstract public function fromDb($value, $key, $context);
+
+ /**
+ * Transform the given value which is about to be persisted to the database
+ *
+ * @param mixed $value
+ * @param string $key
+ * @param mixed $context
+ *
+ * @return mixed
+ */
+ abstract public function toDb($value, $key, $context);
+}