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
This commit is contained in:
Victor Westerlund 2026-02-24 11:52:12 +01:00
parent dba0fe4ebb
commit 729663a113

View file

@ -34,6 +34,27 @@
return $query->num_rows === 1 ? new static($query->fetch_assoc()[Posts::ID->value]) : null; 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 * 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); 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 { 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; 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); set (?Post $post_parent) => $this->set(Posts::POST_PARENT->value, $post_parent ? $post_parent->id : 0, $post_parent);
} }
public string $guid { public string $guid {