Issues (3627)

app/bundles/CoreBundle/Entity/CommonEntity.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Entity;
13
14
use Doctrine\Common\Collections\Collection;
15
16
class CommonEntity
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $changes = [];
22
23
    /**
24
     * @var array
25
     */
26
    protected $pastChanges = [];
27
28
    /**
29
     * Wrapper function for isProperty methods.
30
     *
31
     * @param string $name
32
     * @param        $arguments
33
     *
34
     * @throws \InvalidArgumentException
35
     */
36
    public function __call($name, $arguments)
37
    {
38
        if (0 === strpos($name, 'is') && method_exists($this, 'get'.ucfirst($name))) {
39
            return $this->{'get'.ucfirst($name)}();
40
        } elseif ('getName' == $name && method_exists($this, 'getTitle')) {
41
            return $this->getTitle();
0 ignored issues
show
The method getTitle() does not exist on Mautic\CoreBundle\Entity\CommonEntity. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            return $this->/** @scrutinizer ignore-call */ getTitle();
Loading history...
42
        }
43
44
        throw new \InvalidArgumentException('Method '.$name.' not exists');
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function __toString()
51
    {
52
        $string = get_called_class();
53
        if (method_exists($this, 'getId')) {
54
            $string .= ' with ID #'.$this->getId();
55
        }
56
57
        return $string;
58
    }
59
60
    /**
61
     * @param string $prop
62
     * @param mixed  $val
63
     */
64
    protected function isChanged($prop, $val)
65
    {
66
        $getter  = (method_exists($this, $prop)) ? $prop : 'get'.ucfirst($prop);
67
        $current = $this->$getter();
68
        if ('category' == $prop) {
69
            $currentId = ($current) ? $current->getId() : '';
70
            $newId     = ($val) ? $val->getId() : null;
71
            if ($currentId != $newId) {
72
                $this->addChange($prop, [$currentId, $newId]);
73
            }
74
        } elseif ($current !== $val) {
75
            if ($current instanceof Collection || $val instanceof Collection) {
76
                if (!isset($this->changes[$prop])) {
77
                    $this->changes[$prop] = [
78
                        'added'   => [],
79
                        'removed' => [],
80
                    ];
81
                }
82
83
                if (is_object($val)) {
84
                    // Entity is getting added to the collection
85
                    $this->changes['added'][] = method_exists($val, 'getId') ? $val->getId() : (string) $val;
86
                } else {
87
                    // Entity is getting removed from the collection
88
                    $this->changes['removed'][] = $val;
89
                }
90
            } else {
91
                if ($current instanceof \DateTime) {
92
                    $current = $current->format('c');
93
                } elseif (is_object($current)) {
94
                    $current = (method_exists($current, 'getId')) ? $current->getId() : (string) $current;
95
                } elseif (('' === $current && null === $val) || (null === $current && '' === $val)) {
96
                    // Ingore empty conversion (but allow 0 to '' or null)
97
                    return;
98
                }
99
100
                if ($val instanceof \DateTime) {
101
                    $val = $val->format('c');
102
                } elseif (is_object($val)) {
103
                    $val = (method_exists($val, 'getId')) ? $val->getId() : (string) $val;
104
                }
105
106
                $this->addChange($prop, [$current, $val]);
107
            }
108
        }
109
    }
110
111
    /**
112
     * @param $key
113
     * @param $value
114
     */
115
    protected function addChange($key, $value)
116
    {
117
        if (isset($this->changes[$key]) && is_array($this->changes[$key]) && [0, 1] !== array_keys($this->changes[$key])) {
118
            $this->changes[$key] = array_merge($this->changes[$key], $value);
119
        } else {
120
            $this->changes[$key] = $value;
121
        }
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function getChanges($includePast = false)
128
    {
129
        if ($includePast && empty($this->changes) && !empty($this->pastChanges)) {
130
            return $this->pastChanges;
131
        }
132
133
        return $this->changes;
134
    }
135
136
    /**
137
     * Reset changes.
138
     */
139
    public function resetChanges()
140
    {
141
        $this->pastChanges = $this->changes;
142
        $this->changes     = [];
143
    }
144
145
    public function setChanges(array $changes)
146
    {
147
        $this->changes = $changes;
148
    }
149
}
150