11<?php
22/**
3- * File containing the BinaryFile Type class
3+ * File containing the BinaryFile Type class.
44 *
55 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
66 * @license For full copyright and license information view LICENSE file distributed with this source code.
7+ *
78 * @version //autogentag//
89 */
910
1011namespace Netgen \Bundle \EnhancedBinaryFileBundle \Core \FieldType \EnhancedBinaryFile ;
1112
1213use eZ \Publish \API \Repository \Values \ContentType \FieldDefinition ;
14+ use eZ \Publish \Core \FieldType \BinaryFile \Type as BinaryFileType ;
1315use eZ \Publish \Core \FieldType \ValidationError ;
1416use eZ \Publish \Core \MVC \ConfigResolverInterface ;
15- use eZ \Publish \SPI \IO \MimeTypeDetector ;
1617use eZ \Publish \SPI \FieldType \Value as SPIValue ;
17- use eZ \Publish \Core \ FieldType \ BinaryFile \ Type as BinaryFileType ;
18+ use eZ \Publish \SPI \ IO \ MimeTypeDetector ;
1819
1920class Type extends BinaryFileType
2021{
21-
2222 /**
23- * A settings whitelist used in validateFieldSettings method
23+ * A settings whitelist used in validateFieldSettings method.
2424 *
2525 * @var array
2626 */
2727 protected $ settingsSchema = array (
2828 'allowedTypes ' => array (
2929 'type ' => 'string ' ,
3030 'default ' => null ,
31- )
31+ ),
3232 );
3333
3434 /**
35- * @var \eZ\Publish\SPI\IO\MimeTypeDetector $mimeTypeDetector
35+ * @var \eZ\Publish\SPI\IO\MimeTypeDetector
3636 */
3737 protected $ mimeTypeDetector ;
3838
@@ -45,7 +45,7 @@ class Type extends BinaryFileType
4545 * @param \eZ\Publish\SPI\IO\MimeTypeDetector $mimeTypeDetector
4646 * @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
4747 */
48- public function __construct ( MimeTypeDetector $ mimeTypeDetector , ConfigResolverInterface $ configResolver )
48+ public function __construct (MimeTypeDetector $ mimeTypeDetector , ConfigResolverInterface $ configResolver )
4949 {
5050 $ this ->mimeTypeDetector = $ mimeTypeDetector ;
5151 $ this ->configResolver = $ configResolver ;
@@ -63,78 +63,62 @@ public function getEmptyValue()
6363 }
6464
6565 /**
66- * Creates a specific value of the derived class from $inputValue
67- *
68- * @param array $inputValue
69- *
70- * @return Value
71- */
72- protected function createValue ( array $ inputValue )
73- {
74- return new Value ( $ inputValue );
75- }
76-
77- /**
78- * Returns the field type identifier for this field type
66+ * Returns the field type identifier for this field type.
7967 *
8068 * @return string
8169 */
8270 public function getFieldTypeIdentifier ()
8371 {
84- return " enhancedezbinaryfile " ;
72+ return ' enhancedezbinaryfile ' ;
8573 }
8674
87-
8875 /**
89- * Validates a field based on the validators in the field definition
76+ * Validates a field based on the validators in the field definition.
9077 *
91- * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
9278 *
9379 * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
9480 * @param \eZ\Publish\Core\FieldType\BinaryBase\Value $fieldValue The field value for which an action is performed
9581 *
82+ * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
83+ *
9684 * @return \eZ\Publish\SPI\FieldType\ValidationError[]
9785 */
98- public function validate ( FieldDefinition $ fieldDefinition , SPIValue $ fieldValue )
86+ public function validate (FieldDefinition $ fieldDefinition , SPIValue $ fieldValue )
9987 {
10088 $ errors = array ();
10189
102- if ( $ this ->isEmptyValue ( $ fieldValue ) )
103- {
90+ if ($ this ->isEmptyValue ($ fieldValue )) {
10491 return $ errors ;
10592 }
10693
10794 $ fieldSettings = $ fieldDefinition ->getFieldSettings ();
108- $ allowedExtensions = explode ( '| ' , $ fieldSettings ['allowedTypes ' ] );
95+ $ allowedExtensions = explode ('| ' , $ fieldSettings ['allowedTypes ' ]);
10996
110- $ mimeType = $ this ->mimeTypeDetector ->getFromPath ( $ fieldValue ->inputUri );
97+ $ mimeType = $ this ->mimeTypeDetector ->getFromPath ($ fieldValue ->inputUri );
11198
112- foreach ( $ allowedExtensions as $ allowedExtension )
113- {
114- if ( $ this ->configResolver ->hasParameter ( "{$ allowedExtension }.Types " , 'mime ' ) )
115- {
116- $ allowedMimeTypes = $ this ->configResolver ->getParameter ( "{$ allowedExtension }.Types " , 'mime ' );
99+ foreach ($ allowedExtensions as $ allowedExtension ) {
100+ if ($ this ->configResolver ->hasParameter ("{$ allowedExtension }.Types " , 'mime ' )) {
101+ $ allowedMimeTypes = $ this ->configResolver ->getParameter ("{$ allowedExtension }.Types " , 'mime ' );
117102
118- if ( in_array ( $ mimeType , $ allowedMimeTypes ) )
119- {
120- return parent ::validate ( $ fieldDefinition , $ fieldValue );
103+ if (in_array ($ mimeType , $ allowedMimeTypes , true )) {
104+ return parent ::validate ($ fieldDefinition , $ fieldValue );
121105 }
122106 }
123107 }
124108
125109 return array (
126110 new ValidationError (
127- " This mimeType is not allowed %mimeType%. " ,
128- " These mimeTypes are not allowed %mimeType%. " ,
111+ ' This mimeType is not allowed %mimeType%. ' ,
112+ ' These mimeTypes are not allowed %mimeType%. ' ,
129113 array (
130- " mimeType " => $ mimeType ,
114+ ' mimeType ' => $ mimeType ,
131115 )
132- )
116+ ),
133117 );
134118 }
135119
136120 /**
137- * Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct
121+ * Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
138122 *
139123 * This method expects that given $fieldSettings are complete, for this purpose method
140124 * {@link self::applyDefaultSettings()} is provided.
@@ -143,19 +127,18 @@ public function validate( FieldDefinition $fieldDefinition, SPIValue $fieldValue
143127 *
144128 * @return \eZ\Publish\SPI\FieldType\ValidationError[]
145129 */
146- public function validateFieldSettings ( $ fieldSettings )
130+ public function validateFieldSettings ($ fieldSettings )
147131 {
148132 $ validationErrors = array ();
149133
150- if ( !is_array ( $ fieldSettings ) ) {
151- $ validationErrors [] = new ValidationError ( 'Field settings must be in form of an array ' );
134+ if (!is_array ($ fieldSettings) ) {
135+ $ validationErrors [] = new ValidationError ('Field settings must be in form of an array ' );
152136
153137 return $ validationErrors ;
154138 }
155139
156- foreach ( $ fieldSettings as $ name => $ value )
157- {
158- switch ( $ name ) {
140+ foreach ($ fieldSettings as $ name => $ value ) {
141+ switch ($ name ) {
159142 case 'allowedTypes ' :
160143 // Nothing to validate, just recognize this setting as known
161144 break ;
@@ -173,4 +156,16 @@ public function validateFieldSettings( $fieldSettings )
173156
174157 return $ validationErrors ;
175158 }
159+
160+ /**
161+ * Creates a specific value of the derived class from $inputValue.
162+ *
163+ * @param array $inputValue
164+ *
165+ * @return Value
166+ */
167+ protected function createValue (array $ inputValue )
168+ {
169+ return new Value ($ inputValue );
170+ }
176171}
0 commit comments