|
| 1 | +<?php |
| 2 | +namespace Sortable\Fixture; |
| 3 | + |
| 4 | +use Doctrine\ORM\Mapping as ORM; |
| 5 | +use Doctrine\Common\NotifyPropertyChanged; |
| 6 | +use Doctrine\Common\PropertyChangedListener; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author Charles J. C. Elling, 2017-07-31 |
| 10 | + * |
| 11 | + * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository") |
| 12 | + * @ORM\ChangeTrackingPolicy("NOTIFY") |
| 13 | + */ |
| 14 | +class NotifyNode extends AbstractNode implements NotifyPropertyChanged |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Listeners that want to be notified about property changes. |
| 18 | + * |
| 19 | + * @var PropertyChangedListener[] |
| 20 | + */ |
| 21 | + private $_propertyChangedListeners = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * Adds a listener that wants to be notified about property changes. |
| 25 | + * |
| 26 | + * @see \Doctrine\Common\NotifyPropertyChanged::addPropertyChangedListener() |
| 27 | + */ |
| 28 | + public function addPropertyChangedListener(PropertyChangedListener $listener) |
| 29 | + { |
| 30 | + $this->_propertyChangedListeners[] = $listener; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Notify property change event to listeners |
| 35 | + * |
| 36 | + * @param string $propName |
| 37 | + * @param mixed $oldValue |
| 38 | + * @param mixed $newValue |
| 39 | + */ |
| 40 | + protected function triggerPropertyChanged($propName, $oldValue, $newValue) |
| 41 | + { |
| 42 | + foreach ($this->_propertyChangedListeners as $listener) |
| 43 | + { |
| 44 | + $listener->propertyChanged($this, $propName, $oldValue, $newValue); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + protected function setProperty($property, $newValue) |
| 49 | + { |
| 50 | + $oldValue = $this->{$property}; |
| 51 | + if($oldValue !== $newValue) |
| 52 | + { |
| 53 | + $this->triggerPropertyChanged($property, $oldValue, $newValue); |
| 54 | + $this->{$property} = $newValue; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function setName($name) |
| 59 | + { |
| 60 | + $this->setProperty('name', $name); |
| 61 | + } |
| 62 | + |
| 63 | + public function setPath($path) |
| 64 | + { |
| 65 | + $this->setProperty('path', $path); |
| 66 | + } |
| 67 | + |
| 68 | + public function setPosition($position) |
| 69 | + { |
| 70 | + $this->setProperty('position', $position); |
| 71 | + } |
| 72 | +} |
| 73 | + |
0 commit comments