|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sonata\DoctrinePHPCRAdminBundle\Description; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use PHPCR\PathNotFoundException; |
|
7
|
|
|
use PHPCR\Util\PathHelper; |
|
8
|
|
|
use Symfony\Cmf\Component\Resource\Description\Description; |
|
9
|
|
|
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface; |
|
10
|
|
|
use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource; |
|
11
|
|
|
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A description enhancer to add the position/sorting value of children on a parent. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Maximilian Berghoff <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class PositionEnhancer implements DescriptionEnhancerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \PHPCR\SessionInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $session; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param ManagerRegistry $manager |
|
27
|
|
|
* @param $sessionName |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(ManagerRegistry $manager, $sessionName) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->session = $manager->getConnection($sessionName); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function enhance(Description $description) |
|
38
|
|
|
{ |
|
39
|
|
|
$nodePath = $description->getResource()->getPath(); |
|
40
|
|
|
$nodeName = PathHelper::getNodeName($nodePath); |
|
41
|
|
|
$parentPath = PathHelper::getParentPath($nodePath); |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
|
|
$parentNode = $this->session->getNode($parentPath); |
|
45
|
|
|
} catch (PathNotFoundException $exception) { |
|
46
|
|
|
return false; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$nodeIterator = $parentNode->getNodes(); |
|
50
|
|
|
$nodeIterator->rewind(); |
|
51
|
|
|
$counter = 0; |
|
52
|
|
|
while ($nodeIterator->valid()) { |
|
53
|
|
|
++$counter; |
|
54
|
|
|
if ($nodeIterator->key() === $nodeName) { |
|
55
|
|
|
break; |
|
56
|
|
|
} |
|
57
|
|
|
$nodeIterator->next(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$description->set('position', $counter); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function supports(PuliResource $resource) |
|
67
|
|
|
{ |
|
68
|
|
|
if (!$resource instanceof CmfResource) { |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$parentNode = $this->session->getNode(PathHelper::getParentPath($resource->getPath())); |
|
|
|
|
|
|
74
|
|
|
} catch (PathNotFoundException $exception) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Todo: check for non orderable type |
|
79
|
|
|
|
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.