-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslationEntityAwareTrait.php
More file actions
67 lines (57 loc) · 1.63 KB
/
TranslationEntityAwareTrait.php
File metadata and controls
67 lines (57 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
# -*- coding: utf-8 -*-
namespace Translationmanager\Module;
use BadMethodCallException;
use Translationmanager\Exception\UnexpectedEntityException;
use Translationmanager\Module\Mlp\Processor\PostSaver;
use Translationmanager\Translation;
use WC_Product;
use WP_Post;
/**
* Class TranslationEntityCapableTrait
*
* @author Guido Scialfa <dev@guidoscialfa.com>
*/
trait TranslationEntityAwareTrait
{
/**
* Retrieve the Post Entity from Translation Data
*
* @param Translation $translation
*
* @return WP_Post
* @throws UnexpectedEntityException
*/
protected function post(Translation $translation)
{
$post = $translation->get_meta(
PostSaver::SAVED_POST_KEY,
ModuleIntegrator::POST_DATA_NAMESPACE
);
if (!$post instanceof WP_Post) {
throw UnexpectedEntityException::forPostValue(WP_Post::class, '');
}
return $post;
}
/**
* Retrieve the Product Entity from Translated Post
*
* @param Translation $translation
*
* @return WC_Product
* @throws BadMethodCallException
* @throws UnexpectedEntityException
*/
protected function product(Translation $translation)
{
if (!function_exists('wc_get_product')) {
throw new BadMethodCallException('Function wc_get_product does not exists.');
}
$post = $this->post($translation);
$product = wc_get_product($post);
if (!$product instanceof WC_Product) {
throw UnexpectedEntityException::forPostValue(WC_Product::class, '');
}
return $product;
}
}