summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/orm/src/Contract
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ipl/orm/src/Contract')
-rw-r--r--vendor/ipl/orm/src/Contract/PersistBehavior.php18
-rw-r--r--vendor/ipl/orm/src/Contract/PropertyBehavior.php102
-rw-r--r--vendor/ipl/orm/src/Contract/QueryAwareBehavior.php18
-rw-r--r--vendor/ipl/orm/src/Contract/RetrieveBehavior.php18
-rw-r--r--vendor/ipl/orm/src/Contract/RewriteColumnBehavior.php39
-rw-r--r--vendor/ipl/orm/src/Contract/RewriteFilterBehavior.php25
-rw-r--r--vendor/ipl/orm/src/Contract/RewritePathBehavior.php20
7 files changed, 240 insertions, 0 deletions
diff --git a/vendor/ipl/orm/src/Contract/PersistBehavior.php b/vendor/ipl/orm/src/Contract/PersistBehavior.php
new file mode 100644
index 0000000..a6db05d
--- /dev/null
+++ b/vendor/ipl/orm/src/Contract/PersistBehavior.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace ipl\Orm\Contract;
+
+use ipl\Orm\Behavior;
+use ipl\Orm\Model;
+
+interface PersistBehavior extends Behavior
+{
+ /**
+ * Apply this behavior on the given model
+ *
+ * Called when the model is persisted in the database.
+ *
+ * @param Model $model
+ */
+ public function persist(Model $model);
+}
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);
+}
diff --git a/vendor/ipl/orm/src/Contract/QueryAwareBehavior.php b/vendor/ipl/orm/src/Contract/QueryAwareBehavior.php
new file mode 100644
index 0000000..b67bf51
--- /dev/null
+++ b/vendor/ipl/orm/src/Contract/QueryAwareBehavior.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace ipl\Orm\Contract;
+
+use ipl\Orm\Behavior;
+use ipl\Orm\Query;
+
+interface QueryAwareBehavior extends Behavior
+{
+ /**
+ * Set the query
+ *
+ * @param Query $query
+ *
+ * @return $this
+ */
+ public function setQuery(Query $query);
+}
diff --git a/vendor/ipl/orm/src/Contract/RetrieveBehavior.php b/vendor/ipl/orm/src/Contract/RetrieveBehavior.php
new file mode 100644
index 0000000..884d074
--- /dev/null
+++ b/vendor/ipl/orm/src/Contract/RetrieveBehavior.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace ipl\Orm\Contract;
+
+use ipl\Orm\Behavior;
+use ipl\Orm\Model;
+
+interface RetrieveBehavior extends Behavior
+{
+ /**
+ * Apply this behavior on the given model
+ *
+ * Called when the model is fetched from the database.
+ *
+ * @param Model $model
+ */
+ public function retrieve(Model $model);
+}
diff --git a/vendor/ipl/orm/src/Contract/RewriteColumnBehavior.php b/vendor/ipl/orm/src/Contract/RewriteColumnBehavior.php
new file mode 100644
index 0000000..b6f545b
--- /dev/null
+++ b/vendor/ipl/orm/src/Contract/RewriteColumnBehavior.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace ipl\Orm\Contract;
+
+use ipl\Orm\ColumnDefinition;
+
+interface RewriteColumnBehavior extends RewriteFilterBehavior
+{
+ /**
+ * Rewrite the given column
+ *
+ * The result must be returned otherwise (NULL is returned) the original column is kept as is.
+ *
+ * @param mixed $column
+ * @param ?string $relation The absolute path of the model. For reference only, don't include it in the result
+ *
+ * @return mixed
+ */
+ public function rewriteColumn($column, ?string $relation = null);
+
+ /**
+ * Get whether {@see rewriteColumn} might return an otherwise unknown column or expression
+ *
+ * @param string $name
+ *
+ * @return bool
+ */
+ public function isSelectableColumn(string $name): bool;
+
+ /**
+ * Rewrite the given column definition
+ *
+ * @param ColumnDefinition $def
+ * @param string $relation The absolute path of the model. For reference only, don't include it in the result
+ *
+ * @return void
+ */
+ public function rewriteColumnDefinition(ColumnDefinition $def, string $relation): void;
+}
diff --git a/vendor/ipl/orm/src/Contract/RewriteFilterBehavior.php b/vendor/ipl/orm/src/Contract/RewriteFilterBehavior.php
new file mode 100644
index 0000000..af6de5b
--- /dev/null
+++ b/vendor/ipl/orm/src/Contract/RewriteFilterBehavior.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace ipl\Orm\Contract;
+
+use ipl\Orm\Behavior;
+use ipl\Stdlib\Filter;
+
+interface RewriteFilterBehavior extends Behavior
+{
+ /**
+ * Rewrite the given filter condition
+ *
+ * The condition can either be adjusted directly or replaced by an entirely new rule. The result must be
+ * returned otherwise (NULL is returned) processing continues normally. (Isn't restarted)
+ *
+ * If a result is returned, it is required to append the given absolute path of the model to the column.
+ * Processing of the condition will be restarted, hence the column has to be an absolute path again.
+ *
+ * @param Filter\Condition $condition
+ * @param string $relation The absolute path (with a trailing dot) of the model
+ *
+ * @return Filter\Rule|null
+ */
+ public function rewriteCondition(Filter\Condition $condition, $relation = null);
+}
diff --git a/vendor/ipl/orm/src/Contract/RewritePathBehavior.php b/vendor/ipl/orm/src/Contract/RewritePathBehavior.php
new file mode 100644
index 0000000..b5b0385
--- /dev/null
+++ b/vendor/ipl/orm/src/Contract/RewritePathBehavior.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace ipl\Orm\Contract;
+
+use ipl\Orm\Behavior;
+
+interface RewritePathBehavior extends Behavior
+{
+ /**
+ * Rewrite the given relation path
+ *
+ * The result must be returned otherwise (NULL is returned) the original path is kept as is.
+ *
+ * @param string $path
+ * @param ?string $relation The absolute path of the model. For reference only, don't include it in the result
+ *
+ * @return ?string
+ */
+ public function rewritePath(string $path, ?string $relation = null): ?string;
+}