1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrinePHPCRAdminBundle\Description; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
17
|
|
|
use PHPCR\PathNotFoundException; |
18
|
|
|
use PHPCR\Util\PathHelper; |
19
|
|
|
use Symfony\Cmf\Component\Resource\Description\Description; |
20
|
|
|
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface; |
21
|
|
|
use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource; |
22
|
|
|
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* A description enhancer to add the position/sorting value of children on a parent. |
26
|
|
|
* |
27
|
|
|
* @author Maximilian Berghoff <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class PositionEnhancer implements DescriptionEnhancerInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var \PHPCR\SessionInterface |
33
|
|
|
*/ |
34
|
|
|
private $session; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $sessionName |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ManagerRegistry $manager, $sessionName) |
40
|
|
|
{ |
41
|
|
|
$this->session = $manager->getConnection($sessionName); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function enhance(Description $description) |
48
|
|
|
{ |
49
|
|
|
$nodePath = $description->getResource()->getPath(); |
50
|
|
|
$nodeName = PathHelper::getNodeName($nodePath); |
51
|
|
|
$parentPath = PathHelper::getParentPath($nodePath); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$parentNode = $this->session->getNode($parentPath); |
55
|
|
|
} catch (PathNotFoundException $exception) { |
56
|
|
|
return false; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$nodeIterator = $parentNode->getNodes(); |
60
|
|
|
$nodeIterator->rewind(); |
61
|
|
|
$counter = 0; |
62
|
|
|
while ($nodeIterator->valid()) { |
63
|
|
|
++$counter; |
64
|
|
|
if ($nodeIterator->key() === $nodeName) { |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
$nodeIterator->next(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$description->set('position', $counter); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function supports(PuliResource $resource) |
77
|
|
|
{ |
78
|
|
|
if (!$resource instanceof CmfResource) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
$parentNode = $this->session->getNode(PathHelper::getParentPath($resource->getPath())); |
|
|
|
|
84
|
|
|
} catch (PathNotFoundException $exception) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Todo: check for non orderable type |
89
|
|
|
|
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.