Completed
Branch master (3b8125)
by
unknown
01:25
created

PropertyManager::getSerializedName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Util\Inflector;
7
use Exception;
8
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\MultilineCommentableInterface;
9
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface;
10
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\PropertyManager;
11
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Tools;
12
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Traits\MultilineCommentTrait;
13
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Traits\TemplateTrait;
14
use JMS\Serializer\Annotation\SerializedName;
15
use JMS\Serializer\Annotation\Type;
16
use JMS\Serializer\TypeParser;
17
use Symfony\Component\Validator\Constraints as Assert;
18
19
/**
20
 * PropertyManager
21
 *
22
 * @author Sławomir Kania <[email protected]>
23
 */
24
class PropertyManager implements RenderableInterface, MultilineCommentableInterface
25
{
26
27
    use MultilineCommentTrait;
28
    use TemplateTrait;
29
30
    /**
31
     * Property predefined types
32
     *
33
     * @var string
34
     */
35
    const TYPE_BOOLEAN = 'boolean';
36
    const TYPE_ARRAY_COLLECTION = 'Doctrine\Common\Collections\ArrayCollection';
37
38
    /**
39
     * Comment of property
40
     *
41
     * @Type("string")
42
     */
43
    private $comment = '';
44
45
    /**
46
     * Collection of Constraints
47
     *
48
     * @Type("Doctrine\Common\Collections\ArrayCollection<string>")
49
     */
50
    private $constraints = null;
51
52
    /**
53
     * Property name
54
     *
55
     * @Type("string")
56
     * @Assert\NotBlank(message="Property name can not be empty!")
57
     */
58
    private $name = '';
59
60
    /**
61
     * Property type
62
     *
63
     * @Type("string")
64
     * @Assert\NotBlank(message="Property type can not be empty!")
65
     */
66
    private $type = '';
67
68
    /**
69
     * Property serialized name, default is property name in snake format eg. for property $lastPost => last_post
70
     *
71
     * @Type("string")
72
     * @Assert\Type("string")
73
     */
74
    private $serializedName = '';
75
76
    /**
77
     * @Type("boolean")
78
     * @Assert\Type("boolean")
79
     */
80
    private $optional = false;
81
82
    /**
83
     * @Type("string")
84
     * @Assert\Type("string")
85
     * @SerializedName("property_manager_template_path")
86
     */
87
    private $propertyManagerTemplatePath = "";
88
89
    /**
90
     * @Type("string")
91
     * @Assert\Type("string")
92
     * @SerializedName("test_class_method_manager_template_path")
93
     */
94
    private $testClassMethodManagerTemplatePath = "";
95
96
    /**
97
     * @Type("string")
98
     * @Assert\Type("string")
99
     * @SerializedName("method_getter_boolean_interface_manager_template_path")
100
     */
101
    private $methodGetterBooleanInterfaceManageTemplatePath = "";
102
103
    /**
104
     * @Type("string")
105
     * @Assert\Type("string")
106
     * @SerializedName("method_getter_boolean_manager_template_path")
107
     */
108
    private $methodGetterBooleanManagerTemplatePath = "";
109
110
    /**
111
     * @Type("string")
112
     * @Assert\Type("string")
113
     * @SerializedName("method_getter_interface_manager_template_path")
114
     */
115
    private $methodGetterInterfaceManagerTemplatePath = "";
116
117
    /**
118
     * @Type("string")
119
     * @Assert\Type("string")
120
     * @SerializedName("method_getter_manager_template_path")
121
     */
122
    private $methodGetterManagerTemplatePath = "";
123
124
    /**
125
     * @Type("string")
126
     * @Assert\Type("string")
127
     * @SerializedName("method_setter_interface_manager_template_path")
128
     */
129
    private $methodSetterInterfaceManagerTemplatePath = "";
130
131
    /**
132
     * @Type("string")
133
     * @Assert\Type("string")
134
     * @SerializedName("method_setter_manager_template_path")
135
     */
136
    private $methodSetterManagerTemplatePath = "";
137
138
    /**
139
     * Constr.
140
     */
141
    public function __construct()
142
    {
143
        $this->constraints = new ArrayCollection();
144
    }
145
146
    /**
147
     * @Assert\IsTrue(message = "Invalid property name!")
148
     * @return boolean
149
     */
150
    public function isValidName()
151
    {
152
        if (false === Tools::isValidPropertyNameString($this->getName())) {
153
            return false;
154
        }
155
156
        return true;
157
    }
158
159
    /**
160
     * @Assert\IsTrue(message = "Invalid property type!")
161
     * @return boolean
162
     */
163
    public function isValidType()
164
    {
165
        if (false === Tools::isValidPropertyTypeString($this->getType())) {
166
            return false;
167
        }
168
169
        return true;
170
    }
171
172
    /**
173
     * @Assert\IsTrue(message = "Property has invalid validation constraint! Insert only constraint class with parameters eg. NotBlank() or Email(message = 'Invalid email')")
174
     */
175
    public function hasAllCallableConstraintIfHasAny()
176
    {
177
        if (false === $this->hasConstraints()) {
178
            return true;
179
        }
180
181
        foreach ($this->getConstraintAnnotationCollection() as $constraintAnnotation) {
182
            if (false === Tools::isCallableConstraintAnnotation($constraintAnnotation)) {
183
                return false;
184
            }
185
        }
186
187
        return true;
188
    }
189
190
    /**
191
     * Check is property boolean type
192
     *
193
     * @return boolean
194
     */
195
    public function isTypeBoolean()
196
    {
197
        return PropertyManager::TYPE_BOOLEAN == $this->getType();
198
    }
199
200
    /**
201
     * Check is property ArrayCollection type
202
     *
203
     * @return boolean
204
     */
205
    public function isTypeArrayCollection()
206
    {
207
        return PropertyManager::TYPE_ARRAY_COLLECTION == $this->getTypeName();
208
    }
209
210
    /**
211
     * Return property comment
212
     *
213
     * @return string
214
     */
215
    public function getComment()
216
    {
217
        return $this->comment;
218
    }
219
220
    /**
221
     * Set property comment
222
     *
223
     * @param string $comment
224
     * @return PropertyManager
225
     */
226
    public function setComment($comment)
227
    {
228
        $this->comment = $comment;
229
        return $this;
230
    }
231
232
    /**
233
     * Return constraints parts array
234
     *
235
     * @return ArrayCollection
236
     */
237
    public function getConstraints()
238
    {
239
        if (false === ($this->constraints instanceof ArrayCollection)) {
240
            return new ArrayCollection();
241
        }
242
243
        return $this->constraints;
244
    }
245
246
    /**
247
     * Check that property has constraints
248
     *
249
     * @return boolean
250
     */
251
    public function hasConstraints()
252
    {
253
        return (false === $this->getConstraints()->isEmpty());
254
    }
255
256
    /**
257
     * Return constraints names array
258
     *
259
     * @return ArrayCollection
260
     */
261
    public function getConstraintAnnotationCollection()
262
    {
263
        $constraintsFull = new ArrayCollection();
264
        foreach ($this->getConstraints() as $constraintPart) {
265
            $constraintsFull->add(sprintf("@\Symfony\Component\Validator\Constraints\%s", $constraintPart));
266
        }
267
268
        return $constraintsFull;
269
    }
270
271
    /**
272
     * Set constraints names array
273
     *
274
     * @param ArrayCollection $constraints
275
     * @return PropertyManager
276
     */
277
    public function setConstraints(ArrayCollection $constraints)
278
    {
279
        $this->constraints = $constraints;
280
        return $this;
281
    }
282
283
    /**
284
     * Return property name string
285
     *
286
     * @return string
287
     */
288
    public function getName()
289
    {
290
        return $this->name;
291
    }
292
293
    /**
294
     * Return prepared property name eg. name_and_surname => nameAndSurname
295
     *
296
     * @param string $propertyName
297
     * @return string
298
     */
299
    public function getPreparedName()
300
    {
301
        return Inflector::camelize($this->getName());
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Inflector\Inflector::camelize() has been deprecated.

This method has been deprecated.

Loading history...
302
    }
303
304
    /**
305
     * Set property name
306
     *
307
     * @param string $name
308
     * @return PropertyManager
309
     */
310
    public function setName($name)
311
    {
312
        $this->name = $name;
313
        return $this;
314
    }
315
316
    /**
317
     * Return property type string
318
     *
319
     * @return string
320
     */
321
    public function getType()
322
    {
323
        return $this->type;
324
    }
325
326
    /**
327
     * Return only type name without params eg. Doctrine\Common\Collections\ArrayCollection<AppBundle\Entity\Post> result Doctrine\Common\Collections\ArrayCollection
328
     *
329
     * @return string
330
     * @throws Exception
331
     */
332
    public function getTypeName()
333
    {
334
        $typeParser = new TypeParser();
335
        $result = $typeParser->parse($this->getType());
336
337
        if (false === array_key_exists("name", $result)) {
338
            throw new Exception("Invalid type parsing result");
339
        }
340
341
        return $result["name"];
342
    }
343
344
    /**
345
     * Set property type
346
     *
347
     * @param string $type
348
     * @return PropertyManager
349
     */
350
    public function setType($type)
351
    {
352
        $this->type = $type;
353
        return $this;
354
    }
355
356
    /**
357
     * Return custom serialized name
358
     *
359
     * @return string
360
     */
361
    public function getSerializedName()
362
    {
363
        return $this->serializedName;
364
    }
365
366
    /**
367
     * Set custom serialized name form property
368
     *
369
     * @param string $serializedName
370
     * @return PropertyManager
371
     */
372
    public function setSerializedName($serializedName)
373
    {
374
        $this->serializedName = $serializedName;
375
        return $this;
376
    }
377
378
    /**
379
     * Check that property has custom serialized name
380
     *
381
     * @return boolean
382
     */
383
    public function hasSerializedName()
384
    {
385
        return false === empty($this->serializedName);
386
    }
387
388
    /**
389
     * @return boolean
390
     */
391
    public function isOptional()
392
    {
393
        return $this->optional;
394
    }
395
396
    /**
397
     * @param boolean $optional
398
     */
399
    public function setOptional($optional)
400
    {
401
        $this->optional = $optional;
402
    }
403
404
    /**
405
     * Return set of tags used in template
406
     *
407
     * @return array
408
     */
409
    public function getTemplateTags()
410
    {
411
        return [
412
            self::TAG_COMMENT,
413
            self::TAG_CONSTRAINTS,
414
            self::TAG_JMS_PART,
415
            self::TAG_TYPE,
416
            self::TAG_NAME,
417
            self::TAG_MULTILINE_COMMENT,
418
        ];
419
    }
420
421
    /**
422
     * @return string
423
     */
424
    public function getPropertyManagerTemplatePath()
425
    {
426
        return $this->propertyManagerTemplatePath;
427
    }
428
429
    /**
430
     * @return string
431
     */
432
    public function getMethodGetterBooleanInterfaceManageTemplatePath()
433
    {
434
        return $this->methodGetterBooleanInterfaceManageTemplatePath;
435
    }
436
437
    /**
438
     * @return string
439
     */
440
    public function getMethodGetterBooleanManagerTemplatePath()
441
    {
442
        return $this->methodGetterBooleanManagerTemplatePath;
443
    }
444
445
    /**
446
     * @return string
447
     */
448
    public function getMethodGetterInterfaceManagerTemplatePath()
449
    {
450
        return $this->methodGetterInterfaceManagerTemplatePath;
451
    }
452
453
    /**
454
     * @return string
455
     */
456
    public function getMethodGetterManagerTemplatePath()
457
    {
458
        return $this->methodGetterManagerTemplatePath;
459
    }
460
461
    /**
462
     * @param string $propertyManagerTemplatePath
463
     * @return PropertyManager
464
     */
465
    public function setPropertyManagerTemplatePath($propertyManagerTemplatePath)
466
    {
467
        $this->propertyManagerTemplatePath = $propertyManagerTemplatePath;
468
        return $this;
469
    }
470
471
    /**
472
     * @param string $methodGetterBooleanInterfaceManageTemplatePath
473
     * @return PropertyManager
474
     */
475
    public function setMethodGetterBooleanInterfaceManageTemplatePath($methodGetterBooleanInterfaceManageTemplatePath)
476
    {
477
        $this->methodGetterBooleanInterfaceManageTemplatePath = $methodGetterBooleanInterfaceManageTemplatePath;
478
        return $this;
479
    }
480
481
    /**
482
     * @param string $methodGetterBooleanManagerTemplatePath
483
     * @return PropertyManager
484
     */
485
    public function setMethodGetterBooleanManagerTemplatePath($methodGetterBooleanManagerTemplatePath)
486
    {
487
        $this->methodGetterBooleanManagerTemplatePath = $methodGetterBooleanManagerTemplatePath;
488
        return $this;
489
    }
490
491
    /**
492
     * @param string $methodGetterInterfaceManagerTemplatePath
493
     * @return PropertyManager
494
     */
495
    public function setMethodGetterInterfaceManagerTemplatePath($methodGetterInterfaceManagerTemplatePath)
496
    {
497
        $this->methodGetterInterfaceManagerTemplatePath = $methodGetterInterfaceManagerTemplatePath;
498
        return $this;
499
    }
500
501
    /**
502
     * @param string $methodGetterManagerTemplatePath
503
     * @return PropertyManager
504
     */
505
    public function setMethodGetterManagerTemplatePath($methodGetterManagerTemplatePath)
506
    {
507
        $this->methodGetterManagerTemplatePath = $methodGetterManagerTemplatePath;
508
        return $this;
509
    }
510
511
    /**
512
     * @return string
513
     */
514
    public function getTestClassMethodManagerTemplatePath()
515
    {
516
        return $this->testClassMethodManagerTemplatePath;
517
    }
518
519
    /**
520
     * @param string $testClassMethodManagerTemplatePath
521
     * @return PropertyManager
522
     */
523
    public function setTestClassMethodManagerTemplatePath($testClassMethodManagerTemplatePath)
524
    {
525
        $this->testClassMethodManagerTemplatePath = $testClassMethodManagerTemplatePath;
526
        return $this;
527
    }
528
529
    /**
530
     * @return string
531
     */
532
    public function getMethodSetterInterfaceManagerTemplatePath()
533
    {
534
        return $this->methodSetterInterfaceManagerTemplatePath;
535
    }
536
537
    /**
538
     * @return string
539
     */
540
    public function getMethodSetterManagerTemplatePath()
541
    {
542
        return $this->methodSetterManagerTemplatePath;
543
    }
544
545
    /**
546
     * @param string $methodSetterInterfaceManagerTemplatePath
547
     * @return PropertyManager
548
     */
549
    public function setMethodSetterInterfaceManagerTemplatePath($methodSetterInterfaceManagerTemplatePath)
550
    {
551
        $this->methodSetterInterfaceManagerTemplatePath = $methodSetterInterfaceManagerTemplatePath;
552
        return $this;
553
    }
554
555
    /**
556
     * @param string $methodSetterManagerTemplatePath
557
     * @return PropertyManager
558
     */
559
    public function setMethodSetterManagerTemplatePath($methodSetterManagerTemplatePath)
560
    {
561
        $this->methodSetterManagerTemplatePath = $methodSetterManagerTemplatePath;
562
        return $this;
563
    }
564
565
    /**
566
     * @return boolean
567
     */
568
    public function isObjectType()
569
    {
570
        return Tools::isNamespaceValid($this->getType(), false) || class_exists($this->getTypeName());
571
    }
572
573
    /**
574
     * @return string
575
     * @throws Exception
576
     */
577
    public function getTypeNameAbsoluteIfIsObjectTypeOrThrowException()
578
    {
579
        if (false === $this->isObjectType()) {
580
            throw new Exception("This is not object type");
581
        }
582
583
        return "\\" . $this->getTypeName();
584
    }
585
}
586