mirror of
https://codeberg.org/vlw/wp.git
synced 2026-02-26 12:01:57 +01:00
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:
parent
dba0fe4ebb
commit
25535b59d6
1 changed files with 29 additions and 3 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue