Page.php
34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Navigation
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Page.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @see Zend_Navigation_Container
*/
require_once 'Zend/Navigation/Container.php';
/**
* Base class for Zend_Navigation_Page pages
*
* @category Zend
* @package Zend_Navigation
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_Navigation_Page extends Zend_Navigation_Container
{
/**
* Page label
*
* @var string|null
*/
protected $_label;
/**
* Page id
*
* @var string|null
*/
protected $_id;
/**
* Style class for this page (CSS)
*
* @var string|null
*/
protected $_class;
/**
* A more descriptive title for this page
*
* @var string|null
*/
protected $_title;
/**
* This page's target
*
* @var string|null
*/
protected $_target;
/**
* Forward links to other pages
*
* @link http://www.w3.org/TR/html4/struct/links.html#h-12.3.1
*
* @var array
*/
protected $_rel = array();
/**
* Reverse links to other pages
*
* @link http://www.w3.org/TR/html4/struct/links.html#h-12.3.1
*
* @var array
*/
protected $_rev = array();
/**
* Page order used by parent container
*
* @var int|null
*/
protected $_order;
/**
* ACL resource associated with this page
*
* @var string|Zend_Acl_Resource_Interface|null
*/
protected $_resource;
/**
* ACL privilege associated with this page
*
* @var string|null
*/
protected $_privilege;
/**
* Whether this page should be considered active
*
* @var bool
*/
protected $_active = false;
/**
* Whether this page should be considered visible
*
* @var bool
*/
protected $_visible = true;
/**
* Parent container
*
* @var Zend_Navigation_Container|null
*/
protected $_parent;
/**
* Custom page properties, used by __set(), __get() and __isset()
*
* @var array
*/
protected $_properties = array();
/**
* The type of page to use when it wasn't set
*
* @var string
*/
protected static $_defaultPageType;
// Initialization:
/**
* Factory for Zend_Navigation_Page classes
*
* A specific type to construct can be specified by specifying the key
* 'type' in $options. If type is 'uri' or 'mvc', the type will be resolved
* to Zend_Navigation_Page_Uri or Zend_Navigation_Page_Mvc. Any other value
* for 'type' will be considered the full name of the class to construct.
* A valid custom page class must extend Zend_Navigation_Page.
*
* If 'type' is not given, the type of page to construct will be determined
* by the following rules:
* - If $options contains either of the keys 'action', 'controller',
* 'module', or 'route', a Zend_Navigation_Page_Mvc page will be created.
* - If $options contains the key 'uri', a Zend_Navigation_Page_Uri page
* will be created.
*
* @param array|Zend_Config $options options used for creating page
* @return Zend_Navigation_Page a page instance
* @throws Zend_Navigation_Exception if $options is not array/Zend_Config
* @throws Zend_Exception if 'type' is specified and
* Zend_Loader is unable to load the
* class
* @throws Zend_Navigation_Exception if something goes wrong during
* instantiation of the page
* @throws Zend_Navigation_Exception if 'type' is given, and the specified
* type does not extend this class
* @throws Zend_Navigation_Exception if unable to determine which class
* to instantiate
*/
public static function factory($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (!is_array($options)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $options must be an array or Zend_Config');
}
if (isset($options['type'])) {
$type = $options['type'];
} elseif(self::getDefaultPageType()!= null) {
$type = self::getDefaultPageType();
}
if(isset($type)) {
if (is_string($type) && !empty($type)) {
switch (strtolower($type)) {
case 'mvc':
$type = 'Zend_Navigation_Page_Mvc';
break;
case 'uri':
$type = 'Zend_Navigation_Page_Uri';
break;
}
if (!class_exists($type)) {
require_once 'Zend/Loader.php';
@Zend_Loader::loadClass($type);
}
$page = new $type($options);
if (!$page instanceof Zend_Navigation_Page) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(sprintf(
'Invalid argument: Detected type "%s", which ' .
'is not an instance of Zend_Navigation_Page',
$type));
}
return $page;
}
}
$hasUri = isset($options['uri']);
$hasMvc = isset($options['action']) || isset($options['controller']) ||
isset($options['module']) || isset($options['route']);
if ($hasMvc) {
require_once 'Zend/Navigation/Page/Mvc.php';
return new Zend_Navigation_Page_Mvc($options);
} elseif ($hasUri) {
require_once 'Zend/Navigation/Page/Uri.php';
return new Zend_Navigation_Page_Uri($options);
} else {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: Unable to determine class to instantiate');
}
}
/**
* Page constructor
*
* @param array|Zend_Config $options [optional] page options. Default is
* null, which should set defaults.
* @throws Zend_Navigation_Exception if invalid options are given
*/
public function __construct($options = null)
{
if (is_array($options)) {
$this->setOptions($options);
} elseif ($options instanceof Zend_Config) {
$this->setConfig($options);
}
// do custom initialization
$this->_init();
}
/**
* Initializes page (used by subclasses)
*
* @return void
*/
protected function _init()
{
}
/**
* Sets page properties using a Zend_Config object
*
* @param Zend_Config $config config object to get properties from
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if invalid options are given
*/
public function setConfig(Zend_Config $config)
{
return $this->setOptions($config->toArray());
}
/**
* Sets page properties using options from an associative array
*
* Each key in the array corresponds to the according set*() method, and
* each word is separated by underscores, e.g. the option 'target'
* corresponds to setTarget(), and the option 'reset_params' corresponds to
* the method setResetParams().
*
* @param array $options associative array of options to set
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if invalid options are given
*/
public function setOptions(array $options)
{
foreach ($options as $key => $value) {
$this->set($key, $value);
}
return $this;
}
// Accessors:
/**
* Sets page label
*
* @param string $label new page label
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if empty/no string is given
*/
public function setLabel($label)
{
if (null !== $label && !is_string($label)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $label must be a string or null');
}
$this->_label = $label;
return $this;
}
/**
* Returns page label
*
* @return string page label or null
*/
public function getLabel()
{
return $this->_label;
}
/**
* Sets page id
*
* @param string|null $id [optional] id to set. Default is null,
* which sets no id.
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if not given string or null
*/
public function setId($id = null)
{
if (null !== $id && !is_string($id) && !is_numeric($id)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $id must be a string, number or null');
}
$this->_id = null === $id ? $id : (string) $id;
return $this;
}
/**
* Returns page id
*
* @return string|null page id or null
*/
public function getId()
{
return $this->_id;
}
/**
* Sets page CSS class
*
* @param string|null $class [optional] CSS class to set. Default
* is null, which sets no CSS class.
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if not given string or null
*/
public function setClass($class = null)
{
if (null !== $class && !is_string($class)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $class must be a string or null');
}
$this->_class = $class;
return $this;
}
/**
* Returns page class (CSS)
*
* @return string|null page's CSS class or null
*/
public function getClass()
{
return $this->_class;
}
/**
* Sets page title
*
* @param string $title [optional] page title. Default is
* null, which sets no title.
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if not given string or null
*/
public function setTitle($title = null)
{
if (null !== $title && !is_string($title)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $title must be a non-empty string');
}
$this->_title = $title;
return $this;
}
/**
* Returns page title
*
* @return string|null page title or null
*/
public function getTitle()
{
return $this->_title;
}
/**
* Sets page target
*
* @param string|null $target [optional] target to set. Default is
* null, which sets no target.
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if target is not string or null
*/
public function setTarget($target = null)
{
if (null !== $target && !is_string($target)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $target must be a string or null');
}
$this->_target = $target;
return $this;
}
/**
* Returns page target
*
* @return string|null page target or null
*/
public function getTarget()
{
return $this->_target;
}
/**
* Sets the page's forward links to other pages
*
* This method expects an associative array of forward links to other pages,
* where each element's key is the name of the relation (e.g. alternate,
* prev, next, help, etc), and the value is a mixed value that could somehow
* be considered a page.
*
* @param array|Zend_Config $relations [optional] an associative array of
* forward links to other pages
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function setRel($relations = null)
{
$this->_rel = array();
if (null !== $relations) {
if ($relations instanceof Zend_Config) {
$relations = $relations->toArray();
}
if (!is_array($relations)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $relations must be an ' .
'array or an instance of Zend_Config');
}
foreach ($relations as $name => $relation) {
if (is_string($name)) {
$this->_rel[$name] = $relation;
}
}
}
return $this;
}
/**
* Returns the page's forward links to other pages
*
* This method returns an associative array of forward links to other pages,
* where each element's key is the name of the relation (e.g. alternate,
* prev, next, help, etc), and the value is a mixed value that could somehow
* be considered a page.
*
* @param string $relation [optional] name of relation to return. If not
* given, all relations will be returned.
* @return array an array of relations. If $relation is not
* specified, all relations will be returned in
* an associative array.
*/
public function getRel($relation = null)
{
if (null !== $relation) {
return isset($this->_rel[$relation]) ?
$this->_rel[$relation] :
null;
}
return $this->_rel;
}
/**
* Sets the page's reverse links to other pages
*
* This method expects an associative array of reverse links to other pages,
* where each element's key is the name of the relation (e.g. alternate,
* prev, next, help, etc), and the value is a mixed value that could somehow
* be considered a page.
*
* @param array|Zend_Config $relations [optional] an associative array of
* reverse links to other pages
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function setRev($relations = null)
{
$this->_rev = array();
if (null !== $relations) {
if ($relations instanceof Zend_Config) {
$relations = $relations->toArray();
}
if (!is_array($relations)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $relations must be an ' .
'array or an instance of Zend_Config');
}
foreach ($relations as $name => $relation) {
if (is_string($name)) {
$this->_rev[$name] = $relation;
}
}
}
return $this;
}
/**
* Returns the page's reverse links to other pages
*
* This method returns an associative array of forward links to other pages,
* where each element's key is the name of the relation (e.g. alternate,
* prev, next, help, etc), and the value is a mixed value that could somehow
* be considered a page.
*
* @param string $relation [optional] name of relation to return. If not
* given, all relations will be returned.
* @return array an array of relations. If $relation is not
* specified, all relations will be returned in
* an associative array.
*/
public function getRev($relation = null)
{
if (null !== $relation) {
return isset($this->_rev[$relation]) ?
$this->_rev[$relation] :
null;
}
return $this->_rev;
}
/**
* Sets page order to use in parent container
*
* @param int $order [optional] page order in container.
* Default is null, which sets no
* specific order.
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if order is not integer or null
*/
public function setOrder($order = null)
{
if (is_string($order)) {
$temp = (int) $order;
if ($temp < 0 || $temp > 0 || $order == '0') {
$order = $temp;
}
}
if (null !== $order && !is_int($order)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $order must be an integer or null, ' .
'or a string that casts to an integer');
}
$this->_order = $order;
// notify parent, if any
if (isset($this->_parent)) {
$this->_parent->notifyOrderUpdated();
}
return $this;
}
/**
* Returns page order used in parent container
*
* @return int|null page order or null
*/
public function getOrder()
{
return $this->_order;
}
/**
* Sets ACL resource assoicated with this page
*
* @param string|Zend_Acl_Resource_Interface $resource [optional] resource
* to associate with
* page. Default is
* null, which sets no
* resource.
* @throws Zend_Navigation_Exception if $resource if
* invalid
* @return Zend_Navigation_Page fluent interface,
* returns self
*/
public function setResource($resource = null)
{
if (null === $resource || is_string($resource) ||
$resource instanceof Zend_Acl_Resource_Interface) {
$this->_resource = $resource;
} else {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $resource must be null, a string, ' .
' or an instance of Zend_Acl_Resource_Interface');
}
return $this;
}
/**
* Returns ACL resource assoicated with this page
*
* @return string|Zend_Acl_Resource_Interface|null ACL resource or null
*/
public function getResource()
{
return $this->_resource;
}
/**
* Sets ACL privilege associated with this page
*
* @param string|null $privilege [optional] ACL privilege to associate
* with this page. Default is null, which
* sets no privilege.
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function setPrivilege($privilege = null)
{
$this->_privilege = is_string($privilege) ? $privilege : null;
return $this;
}
/**
* Returns ACL privilege associated with this page
*
* @return string|null ACL privilege or null
*/
public function getPrivilege()
{
return $this->_privilege;
}
/**
* Sets whether page should be considered active or not
*
* @param bool $active [optional] whether page should be
* considered active or not. Default is true.
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function setActive($active = true)
{
$this->_active = (bool) $active;
return $this;
}
/**
* Returns whether page should be considered active or not
*
* @param bool $recursive [optional] whether page should be considered
* active if any child pages are active. Default is
* false.
* @return bool whether page should be considered active
*/
public function isActive($recursive = false)
{
if (!$this->_active && $recursive) {
foreach ($this->_pages as $page) {
if ($page->isActive(true)) {
return true;
}
}
return false;
}
return $this->_active;
}
/**
* Proxy to isActive()
*
* @param bool $recursive [optional] whether page should be considered
* active if any child pages are active. Default
* is false.
* @return bool whether page should be considered active
*/
public function getActive($recursive = false)
{
return $this->isActive($recursive);
}
/**
* Sets whether the page should be visible or not
*
* @param bool $visible [optional] whether page should be
* considered visible or not. Default is true.
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function setVisible($visible = true)
{
$this->_visible = (bool) $visible;
return $this;
}
/**
* Returns a boolean value indicating whether the page is visible
*
* @param bool $recursive [optional] whether page should be considered
* invisible if parent is invisible. Default is
* false.
* @return bool whether page should be considered visible
*/
public function isVisible($recursive = false)
{
if ($recursive && isset($this->_parent) &&
$this->_parent instanceof Zend_Navigation_Page) {
if (!$this->_parent->isVisible(true)) {
return false;
}
}
return $this->_visible;
}
/**
* Proxy to isVisible()
*
* Returns a boolean value indicating whether the page is visible
*
* @param bool $recursive [optional] whether page should be considered
* invisible if parent is invisible. Default is
* false.
* @return bool whether page should be considered visible
*/
public function getVisible($recursive = false)
{
return $this->isVisible($recursive);
}
/**
* Sets parent container
*
* @param Zend_Navigation_Container $parent [optional] new parent to set.
* Default is null which will set
* no parent.
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function setParent(Zend_Navigation_Container $parent = null)
{
if ($parent === $this) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'A page cannot have itself as a parent');
}
// return if the given parent already is parent
if ($parent === $this->_parent) {
return $this;
}
// remove from old parent
if (null !== $this->_parent) {
$this->_parent->removePage($this);
}
// set new parent
$this->_parent = $parent;
// add to parent if page and not already a child
if (null !== $this->_parent && !$this->_parent->hasPage($this, false)) {
$this->_parent->addPage($this);
}
return $this;
}
/**
* Returns parent container
*
* @return Zend_Navigation_Container|null parent container or null
*/
public function getParent()
{
return $this->_parent;
}
/**
* Sets the given property
*
* If the given property is native (id, class, title, etc), the matching
* set method will be used. Otherwise, it will be set as a custom property.
*
* @param string $property property name
* @param mixed $value value to set
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if property name is invalid
*/
public function set($property, $value)
{
if (!is_string($property) || empty($property)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $property must be a non-empty string');
}
$method = 'set' . self::_normalizePropertyName($property);
if ($method != 'setOptions' && $method != 'setConfig' &&
method_exists($this, $method)) {
$this->$method($value);
} else {
$this->_properties[$property] = $value;
}
return $this;
}
/**
* Returns the value of the given property
*
* If the given property is native (id, class, title, etc), the matching
* get method will be used. Otherwise, it will return the matching custom
* property, or null if not found.
*
* @param string $property property name
* @return mixed the property's value or null
* @throws Zend_Navigation_Exception if property name is invalid
*/
public function get($property)
{
if (!is_string($property) || empty($property)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(
'Invalid argument: $property must be a non-empty string');
}
$method = 'get' . self::_normalizePropertyName($property);
if (method_exists($this, $method)) {
return $this->$method();
} elseif (isset($this->_properties[$property])) {
return $this->_properties[$property];
}
return null;
}
// Magic overloads:
/**
* Sets a custom property
*
* Magic overload for enabling <code>$page->propname = $value</code>.
*
* @param string $name property name
* @param mixed $value value to set
* @return void
* @throws Zend_Navigation_Exception if property name is invalid
*/
public function __set($name, $value)
{
$this->set($name, $value);
}
/**
* Returns a property, or null if it doesn't exist
*
* Magic overload for enabling <code>$page->propname</code>.
*
* @param string $name property name
* @return mixed property value or null
* @throws Zend_Navigation_Exception if property name is invalid
*/
public function __get($name)
{
return $this->get($name);
}
/**
* Checks if a property is set
*
* Magic overload for enabling <code>isset($page->propname)</code>.
*
* Returns true if the property is native (id, class, title, etc), and
* true or false if it's a custom property (depending on whether the
* property actually is set).
*
* @param string $name property name
* @return bool whether the given property exists
*/
public function __isset($name)
{
$method = 'get' . self::_normalizePropertyName($name);
if (method_exists($this, $method)) {
return true;
}
return isset($this->_properties[$name]);
}
/**
* Unsets the given custom property
*
* Magic overload for enabling <code>unset($page->propname)</code>.
*
* @param string $name property name
* @return void
* @throws Zend_Navigation_Exception if the property is native
*/
public function __unset($name)
{
$method = 'set' . self::_normalizePropertyName($name);
if (method_exists($this, $method)) {
require_once 'Zend/Navigation/Exception.php';
throw new Zend_Navigation_Exception(sprintf(
'Unsetting native property "%s" is not allowed',
$name));
}
if (isset($this->_properties[$name])) {
unset($this->_properties[$name]);
}
}
/**
* Returns page label
*
* Magic overload for enabling <code>echo $page</code>.
*
* @return string page label
*/
public function __toString()
{
return $this->_label;
}
// Public methods:
/**
* Adds a forward relation to the page
*
* @param string $relation relation name (e.g. alternate, glossary,
* canonical, etc)
* @param mixed $value value to set for relation
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function addRel($relation, $value)
{
if (is_string($relation)) {
$this->_rel[$relation] = $value;
}
return $this;
}
/**
* Adds a reverse relation to the page
*
* @param string $relation relation name (e.g. alternate, glossary,
* canonical, etc)
* @param mixed $value value to set for relation
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function addRev($relation, $value)
{
if (is_string($relation)) {
$this->_rev[$relation] = $value;
}
return $this;
}
/**
* Removes a forward relation from the page
*
* @param string $relation name of relation to remove
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function removeRel($relation)
{
if (isset($this->_rel[$relation])) {
unset($this->_rel[$relation]);
}
return $this;
}
/**
* Removes a reverse relation from the page
*
* @param string $relation name of relation to remove
* @return Zend_Navigation_Page fluent interface, returns self
*/
public function removeRev($relation)
{
if (isset($this->_rev[$relation])) {
unset($this->_rev[$relation]);
}
return $this;
}
/**
* Returns an array containing the defined forward relations
*
* @return array defined forward relations
*/
public function getDefinedRel()
{
return array_keys($this->_rel);
}
/**
* Returns an array containing the defined reverse relations
*
* @return array defined reverse relations
*/
public function getDefinedRev()
{
return array_keys($this->_rev);
}
/**
* Returns custom properties as an array
*
* @return array an array containing custom properties
*/
public function getCustomProperties()
{
return $this->_properties;
}
/**
* Returns a hash code value for the page
*
* @return string a hash code value for this page
*/
public final function hashCode()
{
return spl_object_hash($this);
}
/**
* Returns an array representation of the page
*
* @return array associative array containing all page properties
*/
public function toArray()
{
return array_merge(
$this->getCustomProperties(),
array(
'label' => $this->getlabel(),
'id' => $this->getId(),
'class' => $this->getClass(),
'title' => $this->getTitle(),
'target' => $this->getTarget(),
'rel' => $this->getRel(),
'rev' => $this->getRev(),
'order' => $this->getOrder(),
'resource' => $this->getResource(),
'privilege' => $this->getPrivilege(),
'active' => $this->isActive(),
'visible' => $this->isVisible(),
'type' => get_class($this),
'pages' => parent::toArray()
));
}
// Internal methods:
/**
* Normalizes a property name
*
* @param string $property property name to normalize
* @return string normalized property name
*/
protected static function _normalizePropertyName($property)
{
return str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
}
public static function setDefaultPageType($type = null) {
if($type !== null && !is_string($type)) {
throw new Zend_Navigation_Exception(
'Cannot set default page type: type is no string but should be'
);
}
self::$_defaultPageType = $type;
}
public static function getDefaultPageType() {
return self::$_defaultPageType;
}
// Abstract methods:
/**
* Returns href for this page
*
* @return string the page's href
*/
abstract public function getHref();
}