77use PHPCR \PropertyType ;
88use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
99use Symfony \Component \Serializer \Exception \InvalidArgumentException ;
10+ use PHPCR \PropertyInterface ;
1011
1112/**
1213 * Normalizer for PHPCR Nodes
1516 */
1617class NodeNormalizer implements NormalizerInterface, DenormalizerInterface
1718{
19+ protected $ allowBinary ;
20+ protected $ notes = array ();
21+
22+ public function __construct ($ allowBinary = false )
23+ {
24+ $ this ->allowBinary = $ allowBinary ;
25+ }
26+
27+ public function getNotes ()
28+ {
29+ return $ this ->notes ;
30+ }
31+
1832 /**
1933 * {@inheritDoc}
2034 */
@@ -23,6 +37,10 @@ public function normalize($node, $format = null, array $context = array())
2337 $ res = array ();
2438
2539 foreach ($ node ->getProperties () as $ property ) {
40+ if (false === $ this ->isPropertyEditable ($ property )) {
41+ continue ;
42+ }
43+
2644 $ propertyType = $ property ->getType ();
2745 $ propertyValue = $ property ->getValue ();
2846 $ propertyName = $ property ->getName ();
@@ -49,6 +67,12 @@ public function supportsNormalization($data, $format = null)
4967 */
5068 public function denormalize ($ data , $ class , $ format = null , array $ context = array ())
5169 {
70+ if (!$ data ) {
71+ throw new \InvalidArgumentException (
72+ 'Editor returned nothing .. nodes must have at least one property (i.e. the jcr:primaryType property) '
73+ );
74+ }
75+
5276 if (!isset ($ context ['node ' ])) {
5377 throw new \InvalidArgumentException (sprintf (
5478 'You must provide the PHPCR node instance to update in the context using the "node" key. '
@@ -61,7 +85,12 @@ public function denormalize($data, $class, $format = null, array $context = arra
6185
6286 // Update / remove existing properties
6387 foreach ($ node ->getProperties () as $ property ) {
88+ if (false === $ this ->isPropertyEditable ($ property )) {
89+ continue ;
90+ }
91+
6492 try {
93+
6594 if (!isset ($ data [$ property ->getName ()])) {
6695 $ property ->remove ();
6796 continue ;
@@ -139,4 +168,24 @@ private function normalizeDatum($value)
139168
140169 return $ value ;
141170 }
171+
172+ /**
173+ * Return false if property type is not editable
174+ *
175+ * (e.g. property type is binary)
176+ *
177+ * @return boolean
178+ */
179+ private function isPropertyEditable (PropertyInterface $ property )
180+ {
181+ // do not serialize binary objects
182+ if (false === $ this ->allowBinary && PropertyType::BINARY == $ property ->getType ()) {
183+ $ this ->notes [] = sprintf (
184+ 'Binary property "%s" has been omitted ' , $ property ->getName ()
185+ );
186+ return false ;
187+ }
188+
189+ return true ;
190+ }
142191}
0 commit comments