24
24
*/
25
25
class PropertyAccessor implements PropertyAccessorInterface
26
26
{
27
+ /**
28
+ * @internal
29
+ */
27
30
const VALUE = 0 ;
31
+
32
+ /**
33
+ * @internal
34
+ */
28
35
const IS_REF = 1 ;
36
+
37
+ /**
38
+ * @internal
39
+ */
29
40
const IS_REF_CHAINED = 2 ;
41
+
42
+ /**
43
+ * @internal
44
+ */
30
45
const ACCESS_HAS_PROPERTY = 0 ;
46
+
47
+ /**
48
+ * @internal
49
+ */
31
50
const ACCESS_TYPE = 1 ;
51
+
52
+ /**
53
+ * @internal
54
+ */
32
55
const ACCESS_NAME = 2 ;
56
+
57
+ /**
58
+ * @internal
59
+ */
33
60
const ACCESS_REF = 3 ;
61
+
62
+ /**
63
+ * @internal
64
+ */
34
65
const ACCESS_ADDER = 4 ;
66
+
67
+ /**
68
+ * @internal
69
+ */
35
70
const ACCESS_REMOVER = 5 ;
71
+
72
+ /**
73
+ * @internal
74
+ */
36
75
const ACCESS_TYPE_METHOD = 0 ;
76
+
77
+ /**
78
+ * @internal
79
+ */
37
80
const ACCESS_TYPE_PROPERTY = 1 ;
81
+
82
+ /**
83
+ * @internal
84
+ */
38
85
const ACCESS_TYPE_MAGIC = 2 ;
86
+
87
+ /**
88
+ * @internal
89
+ */
39
90
const ACCESS_TYPE_ADDER_AND_REMOVER = 3 ;
91
+
92
+ /**
93
+ * @internal
94
+ */
40
95
const ACCESS_TYPE_NOT_FOUND = 4 ;
41
96
42
97
/**
@@ -62,6 +117,9 @@ class PropertyAccessor implements PropertyAccessorInterface
62
117
/**
63
118
* Should not be used by application code. Use
64
119
* {@link PropertyAccess::createPropertyAccessor()} instead.
120
+ *
121
+ * @param bool $magicCall
122
+ * @param bool $throwExceptionOnInvalidIndex
65
123
*/
66
124
public function __construct ($ magicCall = false , $ throwExceptionOnInvalidIndex = false )
67
125
{
@@ -365,7 +423,7 @@ private function &readProperty(&$object, $property)
365
423
}
366
424
} elseif (!$ access [self ::ACCESS_HAS_PROPERTY ] && property_exists ($ object , $ property )) {
367
425
// Needed to support \stdClass instances. We need to explicitly
368
- // exclude $classHasProperty , otherwise if in the previous clause
426
+ // exclude $access[self::ACCESS_HAS_PROPERTY] , otherwise if
369
427
// a *protected* property was found on the class, property_exists()
370
428
// returns true, consequently the following line will result in a
371
429
// fatal error.
@@ -411,7 +469,6 @@ private function getReadAccessInfo($object, $property)
411
469
$ getsetter = lcfirst ($ camelProp ); // jQuery style, e.g. read: last(), write: last($item)
412
470
$ isser = 'is ' .$ camelProp ;
413
471
$ hasser = 'has ' .$ camelProp ;
414
- $ classHasProperty = $ reflClass ->hasProperty ($ property );
415
472
416
473
if ($ reflClass ->hasMethod ($ getter ) && $ reflClass ->getMethod ($ getter )->isPublic ()) {
417
474
$ access [self ::ACCESS_TYPE ] = self ::ACCESS_TYPE_METHOD ;
@@ -429,13 +486,10 @@ private function getReadAccessInfo($object, $property)
429
486
$ access [self ::ACCESS_TYPE ] = self ::ACCESS_TYPE_PROPERTY ;
430
487
$ access [self ::ACCESS_NAME ] = $ property ;
431
488
$ access [self ::ACCESS_REF ] = false ;
432
- } elseif ($ classHasProperty && $ reflClass ->getProperty ($ property )->isPublic ()) {
489
+ } elseif ($ access [ self :: ACCESS_HAS_PROPERTY ] && $ reflClass ->getProperty ($ property )->isPublic ()) {
433
490
$ access [self ::ACCESS_TYPE ] = self ::ACCESS_TYPE_PROPERTY ;
434
491
$ access [self ::ACCESS_NAME ] = $ property ;
435
492
$ access [self ::ACCESS_REF ] = true ;
436
-
437
- $ result [self ::VALUE ] = &$ object ->$ property ;
438
- $ result [self ::IS_REF ] = true ;
439
493
} elseif ($ this ->magicCall && $ reflClass ->hasMethod ('__call ' ) && $ reflClass ->getMethod ('__call ' )->isPublic ()) {
440
494
// we call the getter and hope the __call do the job
441
495
$ access [self ::ACCESS_TYPE ] = self ::ACCESS_TYPE_MAGIC ;
@@ -506,7 +560,7 @@ private function writeProperty(&$object, $property, $value)
506
560
$ this ->writeCollection ($ object , $ property , $ value , $ access [self ::ACCESS_ADDER ], $ access [self ::ACCESS_REMOVER ]);
507
561
} elseif (!$ access [self ::ACCESS_HAS_PROPERTY ] && property_exists ($ object , $ property )) {
508
562
// Needed to support \stdClass instances. We need to explicitly
509
- // exclude $classHasProperty , otherwise if in the previous clause
563
+ // exclude $access[self::ACCESS_HAS_PROPERTY] , otherwise if
510
564
// a *protected* property was found on the class, property_exists()
511
565
// returns true, consequently the following line will result in a
512
566
// fatal error.
@@ -579,7 +633,6 @@ private function writeCollection($object, $property, $collection, $addMethod, $r
579
633
private function getWriteAccessInfo ($ object , $ property , $ value )
580
634
{
581
635
$ key = get_class ($ object ).':: ' .$ property ;
582
- $ guessedAdders = '' ;
583
636
584
637
if (isset ($ this ->writePropertyCache [$ key ])) {
585
638
$ access = $ this ->writePropertyCache [$ key ];
@@ -594,25 +647,17 @@ private function getWriteAccessInfo($object, $property, $value)
594
647
if (is_array ($ value ) || $ value instanceof \Traversable) {
595
648
$ methods = $ this ->findAdderAndRemover ($ reflClass , $ singulars );
596
649
597
- if (null === $ methods ) {
598
- // It is sufficient to include only the adders in the error
599
- // message. If the user implements the adder but not the remover,
600
- // an exception will be thrown in findAdderAndRemover() that
601
- // the remover has to be implemented as well.
602
- $ guessedAdders = '"add ' .implode ('()", "add ' , $ singulars ).'()", ' ;
603
- } else {
650
+ if (null !== $ methods ) {
604
651
$ access [self ::ACCESS_TYPE ] = self ::ACCESS_TYPE_ADDER_AND_REMOVER ;
605
652
$ access [self ::ACCESS_ADDER ] = $ methods [0 ];
606
653
$ access [self ::ACCESS_REMOVER ] = $ methods [1 ];
607
654
}
608
655
}
609
656
610
657
if (!isset ($ access [self ::ACCESS_TYPE ])) {
611
- $ setter = 'set ' .$ this -> camelize ( $ property ) ;
658
+ $ setter = 'set ' .$ camelized ;
612
659
$ getsetter = lcfirst ($ camelized ); // jQuery style, e.g. read: last(), write: last($item)
613
660
614
- $ classHasProperty = $ reflClass ->hasProperty ($ property );
615
-
616
661
if ($ this ->isMethodAccessible ($ reflClass , $ setter , 1 )) {
617
662
$ access [self ::ACCESS_TYPE ] = self ::ACCESS_TYPE_METHOD ;
618
663
$ access [self ::ACCESS_NAME ] = $ setter ;
@@ -622,7 +667,7 @@ private function getWriteAccessInfo($object, $property, $value)
622
667
} elseif ($ this ->isMethodAccessible ($ reflClass , '__set ' , 2 )) {
623
668
$ access [self ::ACCESS_TYPE ] = self ::ACCESS_TYPE_PROPERTY ;
624
669
$ access [self ::ACCESS_NAME ] = $ property ;
625
- } elseif ($ classHasProperty && $ reflClass ->getProperty ($ property )->isPublic ()) {
670
+ } elseif ($ access [ self :: ACCESS_HAS_PROPERTY ] && $ reflClass ->getProperty ($ property )->isPublic ()) {
626
671
$ access [self ::ACCESS_TYPE ] = self ::ACCESS_TYPE_PROPERTY ;
627
672
$ access [self ::ACCESS_NAME ] = $ property ;
628
673
} elseif ($ this ->magicCall && $ this ->isMethodAccessible ($ reflClass , '__call ' , 2 )) {
0 commit comments