Completed
Pull Request — master (#14)
by
unknown
08:12
created

PropertyManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SimpleEntityGeneratorBundle\Lib\Items;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Util\Inflector;
7
use Exception;
8
use SimpleEntityGeneratorBundle\Lib\Interfaces\MultilineCommentableInterface;
9
use SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface;
10
use SimpleEntityGeneratorBundle\Lib\Tools;
11
use SimpleEntityGeneratorBundle\Lib\Traits\MultilineCommentTrait;
12
use SimpleEntityGeneratorBundle\Lib\Traits\TemplateTrait;
13
use JMS\Serializer\Annotation\SerializedName;
14
use JMS\Serializer\Annotation\Type;
15
use JMS\Serializer\TypeParser;
16
use Symfony\Component\Validator\Constraints as Assert;
17
18
/**
19
 * PropertyManager
20
 *
21
 * @author Sławomir Kania <[email protected]>
22
 */
23
class PropertyManager implements RenderableInterface, MultilineCommentableInterface
24
{
25
26
    use MultilineCommentTrait;
27
    use TemplateTrait;
28
29
    /**
30
     * Property predefined types
31
     *
32
     * @var string
33
     */
34
    const TYPE_BOOLEAN = 'boolean';
35
    const TYPE_ARRAY_COLLECTION = 'Doctrine\Common\Collections\ArrayCollection';
36
37
    /**
38
     * Comment of property
39
     *
40
     * @Type("string")
41
     */
42
    private $comment = '';
43
44
    /**
45
     * Collection of Constraints
46
     *
47
     * @Type("Doctrine\Common\Collections\ArrayCollection<string>")
48
     */
49
    private $constraints = null;
50
51
    /**
52
     * Property name
53
     *
54
     * @Type("string")
55
     * @Assert\NotBlank(message="Property name can not be empty!")
56
     */
57
    private $name = '';
58
59
    /**
60
     * Property type
61
     *
62
     * @Type("string")
63
     * @Assert\NotBlank(message="Property type can not be empty!")
64
     */
65
    private $type = '';
66
67
    /**
68
     * Property serialized name, default is property name in snake format eg. for property $lastPost => last_post
69
     *
70
     * @Type("string")
71
     * @Assert\Type("string")
72
     */
73
    private $serializedName = '';
74
75
    /**
76
     * @Type("boolean")
77
     * @Assert\Type("boolean")
78
     */
79
    private $optional = false;
80
81
    /**
82
     * @Type("string")
83
     * @Assert\Type("string")
84
     * @SerializedName("property_manager_template_path")
85
     */
86
    private $propertyManagerTemplatePath = "";
87
88
    /**
89
     * @Type("string")
90
     * @Assert\Type("string")
91
     * @SerializedName("test_class_method_manager_template_path")
92
     */
93
    private $testClassMethodManagerTemplatePath = "";
94
95
    /**
96
     * @Type("string")
97
     * @Assert\Type("string")
98
     * @SerializedName("method_getter_boolean_interface_manager_template_path")
99
     */
100
    private $methodGetterBooleanInterfaceManageTemplatePath = "";
101
102
    /**
103
     * @Type("string")
104
     * @Assert\Type("string")
105
     * @SerializedName("method_getter_boolean_manager_template_path")
106
     */
107
    private $methodGetterBooleanManagerTemplatePath = "";
108
109
    /**
110
     * @Type("string")
111
     * @Assert\Type("string")
112
     * @SerializedName("method_getter_interface_manager_template_path")
113
     */
114
    private $methodGetterInterfaceManagerTemplatePath = "";
115
116
    /**
117
     * @Type("string")
118
     * @Assert\Type("string")
119
     * @SerializedName("method_getter_manager_template_path")
120
     */
121
    private $methodGetterManagerTemplatePath = "";
122
123
    /**
124
     * @Type("string")
125
     * @Assert\Type("string")
126
     * @SerializedName("method_setter_interface_manager_template_path")
127
     */
128
    private $methodSetterInterfaceManagerTemplatePath = "";
129
130
    /**
131
     * @Type("string")
132
     * @Assert\Type("string")
133
     * @SerializedName("method_setter_manager_template_path")
134
     */
135
    private $methodSetterManagerTemplatePath = "";
136
137
    /**
138
     * Constr.
139
     */
140
    public function __construct()
141
    {
142
        $this->constraints = new ArrayCollection();
143
    }
144
145
    /**
146
     * @Assert\IsTrue(message = "Invalid property name!")
147
     * @return boolean
148
     */
149
    public function isValidName()
150
    {
151
        if (false === Tools::isValidPropertyNameString($this->getName())) {
152
            return false;
153
        }
154
155
        return true;
156
    }
157
158
    /**
159
     * @Assert\IsTrue(message = "Invalid property type!")
160
     * @return boolean
161
     */
162
    public function isValidType()
163
    {
164
        if (false === Tools::isValidPropertyTypeString($this->getType())) {
165
            return false;
166
        }
167
168
        return true;
169
    }
170
171
    /**
172
     * @Assert\IsTrue(message = "Property has invalid validation constraint! Insert only constraint class with parameters eg. NotBlank() or Email(message = 'Invalid email')")
173
     */
174
    public function hasAllCallableConstraintIfHasAny()
175
    {
176
        if (false === $this->hasConstraints()) {
177
            return true;
178
        }
179
180
        foreach ($this->getConstraintAnnotationCollection() as $constraintAnnotation) {
181
            if (false === Tools::isCallableConstraintAnnotation($constraintAnnotation)) {
182
                return false;
183
            }
184
        }
185
186
        return true;
187
    }
188
189
    /**
190
     * Check is property boolean type
191
     *
192
     * @return boolean
193
     */
194
    public function isTypeBoolean()
195
    {
196
        return PropertyManager::TYPE_BOOLEAN == $this->getType();
197
    }
198
199
    /**
200
     * Check is property ArrayCollection type
201
     *
202
     * @return boolean
203
     */
204
    public function isTypeArrayCollection()
205
    {
206
        return PropertyManager::TYPE_ARRAY_COLLECTION == $this->getTypeName();
207
    }
208
209
    /**
210
     * Return property comment
211
     *
212
     * @return string
213
     */
214
    public function getComment()
215
    {
216
        return $this->comment;
217
    }
218
219
    /**
220
     * Set property comment
221
     *
222
     * @param string $comment
223
     * @return PropertyManager
224
     */
225
    public function setComment($comment)
226
    {
227
        $this->comment = $comment;
228
        return $this;
229
    }
230
231
    /**
232
     * Return constraints parts array
233
     *
234
     * @return ArrayCollection
235
     */
236
    public function getConstraints()
237
    {
238
        if (false === ($this->constraints instanceof ArrayCollection)) {
239
            return new ArrayCollection();
240
        }
241
242
        return $this->constraints;
243
    }
244
245
    /**
246
     * Check that property has constraints
247
     *
248
     * @return boolean
249
     */
250
    public function hasConstraints()
251
    {
252
        return (false === $this->getConstraints()->isEmpty());
253
    }
254
255
    /**
256
     * Return constraints names array
257
     *
258
     * @return ArrayCollection
259
     */
260
    public function getConstraintAnnotationCollection()
261
    {
262
        $constraintsFull = new ArrayCollection();
263
        foreach ($this->getConstraints() as $constraintPart) {
264
            $constraintsFull->add(sprintf("@\Symfony\Component\Validator\Constraints\%s", $constraintPart));
265
        }
266
267
        return $constraintsFull;
268
    }
269
270
    /**
271
     * Set constraints names array
272
     *
273
     * @param ArrayCollection $constraints
274
     * @return PropertyManager
275
     */
276
    public function setConstraints(ArrayCollection $constraints)
277
    {
278
        $this->constraints = $constraints;
279
        return $this;
280
    }
281
282
    /**
283
     * Return property name string
284
     *
285
     * @return string
286
     */
287
    public function getName()
288
    {
289
        return $this->name;
290
    }
291
292
    /**
293
     * Return prepared property name eg. name_and_surname => nameAndSurname
294
     *
295
     * @return string
296
     */
297
    public function getPreparedName()
298
    {
299
        return Inflector::camelize($this->getName());
300
    }
301
302
    /**
303
     * Set property name
304
     *
305
     * @param string $name
306
     * @return PropertyManager
307
     */
308
    public function setName($name)
309
    {
310
        $this->name = $name;
311
        return $this;
312
    }
313
314
    /**
315
     * Return property type string
316
     *
317
     * @return string
318
     */
319
    public function getType()
320
    {
321
        return $this->type;
322
    }
323
324
    /**
325
     * Return only type name without params eg. Doctrine\Common\Collections\ArrayCollection<AppBundle\Entity\Post> result Doctrine\Common\Collections\ArrayCollection
326
     *
327
     * @return string
328
     * @throws Exception
329
     */
330
    public function getTypeName()
331
    {
332
        $typeParser = new TypeParser();
333
        $result = $typeParser->parse($this->getType());
334
335
        if (false === array_key_exists("name", $result)) {
336
            throw new Exception("Invalid type parsing result");
337
        }
338
339
        return $result["name"];
340
    }
341
342
    /**
343
     * Set property type
344
     *
345
     * @param string $type
346
     * @return PropertyManager
347
     */
348
    public function setType($type)
349
    {
350
        $this->type = $type;
351
        return $this;
352
    }
353
354
    /**
355
     * Return custom serialized name
356
     *
357
     * @return string
358
     */
359
    public function getSerializedName()
360
    {
361
        return $this->serializedName;
362
    }
363
364
    /**
365
     * Set custom serialized name form property
366
     *
367
     * @param string $serializedName
368
     * @return PropertyManager
369
     */
370
    public function setSerializedName($serializedName)
371
    {
372
        $this->serializedName = $serializedName;
373
        return $this;
374
    }
375
376
    /**
377
     * Check that property has custom serialized name
378
     *
379
     * @return boolean
380
     */
381
    public function hasSerializedName()
382
    {
383
        return false === empty($this->serializedName);
384
    }
385
386
    /**
387
     * @return boolean
388
     */
389
    public function isOptional()
390
    {
391
        return $this->optional;
392
    }
393
394
    /**
395
     * @param boolean $optional
396
     */
397
    public function setOptional($optional)
398
    {
399
        $this->optional = $optional;
400
    }
401
402
    /**
403
     * Return set of tags used in template
404
     *
405
     * @return array
406
     */
407
    public function getTemplateTags()
408
    {
409
        return [
410
            self::TAG_COMMENT,
411
            self::TAG_CONSTRAINTS,
412
            self::TAG_JMS_PART,
413
            self::TAG_TYPE,
414
            self::TAG_NAME,
415
            self::TAG_MULTILINE_COMMENT,
416
        ];
417
    }
418
419
    /**
420
     * @return string
421
     */
422
    public function getPropertyManagerTemplatePath()
423
    {
424
        return $this->propertyManagerTemplatePath;
425
    }
426
427
    /**
428
     * @return string
429
     */
430
    public function getMethodGetterBooleanInterfaceManageTemplatePath()
431
    {
432
        return $this->methodGetterBooleanInterfaceManageTemplatePath;
433
    }
434
435
    /**
436
     * @return string
437
     */
438
    public function getMethodGetterBooleanManagerTemplatePath()
439
    {
440
        return $this->methodGetterBooleanManagerTemplatePath;
441
    }
442
443
    /**
444
     * @return string
445
     */
446
    public function getMethodGetterInterfaceManagerTemplatePath()
447
    {
448
        return $this->methodGetterInterfaceManagerTemplatePath;
449
    }
450
451
    /**
452
     * @return string
453
     */
454
    public function getMethodGetterManagerTemplatePath()
455
    {
456
        return $this->methodGetterManagerTemplatePath;
457
    }
458
459
    /**
460
     * @param string $propertyManagerTemplatePath
461
     * @return PropertyManager
462
     */
463
    public function setPropertyManagerTemplatePath($propertyManagerTemplatePath)
464
    {
465
        $this->propertyManagerTemplatePath = $propertyManagerTemplatePath;
466
        return $this;
467
    }
468
469
    /**
470
     * @param string $methodGetterBooleanInterfaceManageTemplatePath
471
     * @return PropertyManager
472
     */
473
    public function setMethodGetterBooleanInterfaceManageTemplatePath($methodGetterBooleanInterfaceManageTemplatePath)
474
    {
475
        $this->methodGetterBooleanInterfaceManageTemplatePath = $methodGetterBooleanInterfaceManageTemplatePath;
476
        return $this;
477
    }
478
479
    /**
480
     * @param string $methodGetterBooleanManagerTemplatePath
481
     * @return PropertyManager
482
     */
483
    public function setMethodGetterBooleanManagerTemplatePath($methodGetterBooleanManagerTemplatePath)
484
    {
485
        $this->methodGetterBooleanManagerTemplatePath = $methodGetterBooleanManagerTemplatePath;
486
        return $this;
487
    }
488
489
    /**
490
     * @param string $methodGetterInterfaceManagerTemplatePath
491
     * @return PropertyManager
492
     */
493
    public function setMethodGetterInterfaceManagerTemplatePath($methodGetterInterfaceManagerTemplatePath)
494
    {
495
        $this->methodGetterInterfaceManagerTemplatePath = $methodGetterInterfaceManagerTemplatePath;
496
        return $this;
497
    }
498
499
    /**
500
     * @param string $methodGetterManagerTemplatePath
501
     * @return PropertyManager
502
     */
503
    public function setMethodGetterManagerTemplatePath($methodGetterManagerTemplatePath)
504
    {
505
        $this->methodGetterManagerTemplatePath = $methodGetterManagerTemplatePath;
506
        return $this;
507
    }
508
509
    /**
510
     * @return string
511
     */
512
    public function getTestClassMethodManagerTemplatePath()
513
    {
514
        return $this->testClassMethodManagerTemplatePath;
515
    }
516
517
    /**
518
     * @param string $testClassMethodManagerTemplatePath
519
     * @return PropertyManager
520
     */
521
    public function setTestClassMethodManagerTemplatePath($testClassMethodManagerTemplatePath)
522
    {
523
        $this->testClassMethodManagerTemplatePath = $testClassMethodManagerTemplatePath;
524
        return $this;
525
    }
526
527
    /**
528
     * @return string
529
     */
530
    public function getMethodSetterInterfaceManagerTemplatePath()
531
    {
532
        return $this->methodSetterInterfaceManagerTemplatePath;
533
    }
534
535
    /**
536
     * @return string
537
     */
538
    public function getMethodSetterManagerTemplatePath()
539
    {
540
        return $this->methodSetterManagerTemplatePath;
541
    }
542
543
    /**
544
     * @param string $methodSetterInterfaceManagerTemplatePath
545
     * @return PropertyManager
546
     */
547
    public function setMethodSetterInterfaceManagerTemplatePath($methodSetterInterfaceManagerTemplatePath)
548
    {
549
        $this->methodSetterInterfaceManagerTemplatePath = $methodSetterInterfaceManagerTemplatePath;
550
        return $this;
551
    }
552
553
    /**
554
     * @param string $methodSetterManagerTemplatePath
555
     * @return PropertyManager
556
     */
557
    public function setMethodSetterManagerTemplatePath($methodSetterManagerTemplatePath)
558
    {
559
        $this->methodSetterManagerTemplatePath = $methodSetterManagerTemplatePath;
560
        return $this;
561
    }
562
563
    /**
564
     * @return boolean
565
     */
566
    public function isObjectType()
567
    {
568
        return Tools::isNamespaceValid($this->getType(), false) || class_exists($this->getTypeName());
569
    }
570
571
    /**
572
     * @return string
573
     * @throws Exception
574
     */
575
    public function getTypeNameAbsoluteIfIsObjectTypeOrThrowException()
576
    {
577
        if (false === $this->isObjectType()) {
578
            throw new Exception("This is not object type");
579
        }
580
581
        return "\\" . $this->getTypeName();
582
    }
583
}
584