Completed
Push — master ( 60cb50...a9988a )
by WEBEWEB
01:18
created

DataTablesEntityHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isCompatible() 0 12 4
A newSerializer() 0 3 1
A jsonSerialize() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\JQuery\DataTablesBundle\Helper;
13
14
use JsonSerializable;
15
use Symfony\Component\Serializer\Encoder\JsonEncoder;
16
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
17
use Symfony\Component\Serializer\Serializer;
18
use WBW\Bundle\JQuery\DataTablesBundle\Entity\DataTablesEntityInterface;
19
20
/**
21
 * DataTables entity helper.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\JQuery\DataTablesBundle\Helper
25
 */
26
class DataTablesEntityHelper {
27
28
    /**
29
     * Determines if an entity is compatible.
30
     *
31
     * @param DataTablesEntityInterface|object $entity The entity.
32
     * @return bool Returns true in case of success, false otherwise.
33
     */
34
    public static function isCompatible($entity) {
35
36
        if (true === ($entity instanceof DataTablesEntityInterface)) {
37
            return true;
38
        }
39
40
        if (true === is_object($entity) && true === method_exists($entity, "getId")) {
41
            return true;
42
        }
43
44
        return false;
45
    }
46
47
    /**
48
     * Creates a serializer.
49
     *
50
     * @return Serializer Returns the serializer.
51
     */
52
    public static function newSerializer() {
53
        return new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
54
    }
55
56
    /**
57
     * Serialize an entity.
58
     *
59
     * @param object $entity The entity.
60
     * @return string Returns the serialized entity.
61
     */
62
    public static function jsonSerialize($entity) {
63
64
        $data = true === ($entity instanceof JsonSerializable) ? $entity->jsonSerialize() : $entity;
65
66
        return static::newSerializer()->serialize($data, "json");
67
    }
68
}
69