From 729663a113140bf71c194ad59f905325439161b3 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Tue, 24 Feb 2026 11:52:12 +0100 Subject: [PATCH] feat: add `Post::clone()` method for shallow copying a `Post` (#18) This PR adds a new static method `Post::clone()` which takes a `Post` instance as an argument and creates a new shallow copy of that Post. The copied Post is then returned from this method. Reviewed-on: https://codeberg.org/vlw/wp/pulls/18 --- src/Posts/Post.php | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Posts/Post.php b/src/Posts/Post.php index 6ae2685..a36e929 100644 --- a/src/Posts/Post.php +++ b/src/Posts/Post.php @@ -34,6 +34,27 @@ return $query->num_rows === 1 ? new static($query->fetch_assoc()[Posts::ID->value]) : null; } + /** + * Create and return a shallow copy of a Post + * + * @param Post $post + * @return static + */ + public static function clone(Post $post): static { + $clone = Post::new($post->post_title); + + foreach (Posts::cases() as $column) { + // ID column has to be unique, skip it + if ($column === Posts::ID) { + continue; + } + + $clone->{$column->value} = $post->{$column->value}; + } + + return $clone; + } + /** * Create a new post * @@ -166,9 +187,14 @@ set (DateTimeImmutable $post_modified_gmt) => $this->set(Posts::POST_MODIFIED_GMT->value, $post_modified_gmt->format(static::DATETIME_FORMAT), $post_modified_gmt); } - public Post|false|null $post_parent { - get => $this->get(Posts::POST_PARENT->value) !== 0 ? new self($this->get(Posts::POST_PARENT->value)) : null; - set (Post|false|null $post_parent) => $this->set(Posts::POST_PARENT->value, $post_parent ? $post_parent->id : 0, $post_parent); + public string $post_content_filtered { + get => $this->get(Posts::POST_CONTENT_FILTERED->value); + set (string $post_content_filtered) => $this->set(Posts::POST_CONTENT_FILTERED->value, $post_content_filtered); + } + + public ?Post $post_parent { + get => $this->get(Posts::POST_PARENT->value) !== 0 ? new self($this->get(Posts::POST_PARENT->value)) : null; + set (?Post $post_parent) => $this->set(Posts::POST_PARENT->value, $post_parent ? $post_parent->id : 0, $post_parent); } public string $guid {