Completed
Push — master ( ca81b9...863650 )
by Joschi
03:44
created

ProcessingInstructionsTrait::setDirtyState()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Object\Traits;
38
use Apparat\Object\Domain\Model\Object\ObjectInterface;
39
use Apparat\Object\Domain\Model\Properties\GenericPropertiesInterface;
40
use Apparat\Object\Domain\Model\Properties\ProcessingInstructions;
41
42
/**
43
 * Processing instructions trait
44
 *
45
 * @package Apparat\Object
46
 * @subpackage Apparat\Object\Domain
47
 * @property array $collectionStates
48
 */
49
trait ProcessingInstructionsTrait
50
{
51
    /**
52
     * Processing instructions
53
     *
54
     * @var GenericPropertiesInterface
55
     */
56
    protected $processingInstructions;
57
58
    /**
59
     * Set the processing instruction collection
60
     *
61
     * @param GenericPropertiesInterface $processingInstructions Processing instruction collection
62
     * @param bool $overwrite Overwrite the existing collection (if present)
63
     */
64 19
    protected function setProcessingInstructions(GenericPropertiesInterface $processingInstructions, $overwrite = false)
65
    {
66 19
        $this->processingInstructions = $processingInstructions;
67 19
        $processingInstructionsState = spl_object_hash($this->processingInstructions);
68
69
        // If the domain property collection state has changed
70 19
        if (!$overwrite
71 19
            && !empty($this->collectionStates[ProcessingInstructions::COLLECTION])
72 19
            && ($processingInstructionsState !== $this->collectionStates[ProcessingInstructions::COLLECTION])
73
        ) {
74
            // Flag this object as dirty
75 1
            $this->setDirtyState();
76
        }
77
78 19
        $this->collectionStates[ProcessingInstructions::COLLECTION] = $processingInstructionsState;
79 19
    }
80
81
    /**
82
     * Get a processing instruction
83
     *
84
     * @param string $procInst Processing instruction name
85
     * @return mixed Processing instruction
86
     */
87
    public function getProcessingInstruction($procInst)
88
    {
89
        return $this->processingInstructions->getProperty($procInst);
90
    }
91
92
    /**
93
     * Set a processing instruction
94
     *
95
     * @param string $procInst Processing instruction name
96
     * @param mixed $value Processing instruction
97
     * @return ObjectInterface Self reference
98
     */
99 1
    public function setProcessingInstruction($procInst, $value)
100
    {
101 1
        $this->setProcessingInstructions($this->processingInstructions->setProperty($procInst, $value));
102 1
        return $this;
103
    }
104
105
    /**
106
     * Set the object state to dirty
107
     */
108
    abstract protected function setDirtyState();
109
}
110