forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectStateService.php
845 lines (738 loc) · 31.8 KB
/
ObjectStateService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
<?php
/**
* File containing the eZ\Publish\API\Repository\ObjectStateService class.
*
* @copyright Copyright (C) 1999-2014 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
* @package eZ\Publish\API\Repository
*/
namespace eZ\Publish\Core\Repository;
use eZ\Publish\API\Repository\ObjectStateService as ObjectStateServiceInterface;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\SPI\Persistence\Content\ObjectState\Handler;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectState as APIObjectState;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup as APIObjectStateGroup;
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectState;
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectStateGroup;
use eZ\Publish\SPI\Persistence\Content\ObjectState as SPIObjectState;
use eZ\Publish\SPI\Persistence\Content\ObjectState\Group as SPIObjectStateGroup;
use eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use Exception;
/**
* ObjectStateService service
*
* @example Examples/objectstates.php tbd.
*
* @package eZ\Publish\Core\Repository
*/
class ObjectStateService implements ObjectStateServiceInterface
{
/**
* @var \eZ\Publish\API\Repository\Repository
*/
protected $repository;
/**
* @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler
*/
protected $objectStateHandler;
/**
* @var array
*/
protected $settings;
/**
* Setups service with reference to repository object that created it & corresponding handler
*
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler
* @param array $settings
*/
public function __construct( RepositoryInterface $repository, Handler $objectStateHandler, array $settings = array() )
{
$this->repository = $repository;
$this->objectStateHandler = $objectStateHandler;
// Union makes sure default settings are ignored if provided in argument
$this->settings = $settings + array(
//'defaultSetting' => array(),
);
}
/**
* Creates a new object state group
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state group
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
*/
public function createObjectStateGroup( ObjectStateGroupCreateStruct $objectStateGroupCreateStruct )
{
if ( $this->repository->hasAccess( 'state', 'administrate' ) !== true )
throw new UnauthorizedException( 'state', 'administrate' );
$inputStruct = $this->buildCreateInputStruct(
$objectStateGroupCreateStruct->identifier,
$objectStateGroupCreateStruct->defaultLanguageCode,
$objectStateGroupCreateStruct->names,
$objectStateGroupCreateStruct->descriptions
);
try
{
$this->objectStateHandler->loadGroupByIdentifier( $inputStruct->identifier );
throw new InvalidArgumentException(
"objectStateGroupCreateStruct",
"Object state group with provided identifier already exists"
);
}
catch ( APINotFoundException $e )
{
// Do nothing
}
$this->repository->beginTransaction();
try
{
$spiObjectStateGroup = $this->objectStateHandler->createGroup( $inputStruct );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainObjectStateGroupObject( $spiObjectStateGroup );
}
/**
* Loads a object state group
*
* @param mixed $objectStateGroupId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the group was not found
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
*/
public function loadObjectStateGroup( $objectStateGroupId )
{
$spiObjectStateGroup = $this->objectStateHandler->loadGroup( $objectStateGroupId );
return $this->buildDomainObjectStateGroupObject( $spiObjectStateGroup );
}
/**
* Loads all object state groups
*
* @param int $offset
* @param int $limit
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup[]
*/
public function loadObjectStateGroups( $offset = 0, $limit = -1 )
{
$spiObjectStateGroups = $this->objectStateHandler->loadAllGroups( $offset, $limit );
$objectStateGroups = array();
foreach ( $spiObjectStateGroups as $spiObjectStateGroup )
{
$objectStateGroups[] = $this->buildDomainObjectStateGroupObject( $spiObjectStateGroup );
}
return $objectStateGroups;
}
/**
* This method returns the ordered list of object states of a group
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[]
*/
public function loadObjectStates( APIObjectStateGroup $objectStateGroup )
{
$spiObjectStates = $this->objectStateHandler->loadObjectStates( $objectStateGroup->id );
$objectStates = array();
foreach ( $spiObjectStates as $spiObjectState )
{
$objectStates[] = $this->buildDomainObjectStateObject( $spiObjectState, $objectStateGroup );
}
return $objectStates;
}
/**
* Updates an object state group
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state group
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
*/
public function updateObjectStateGroup( APIObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct )
{
if ( $this->repository->hasAccess( 'state', 'administrate' ) !== true )
throw new UnauthorizedException( 'state', 'administrate' );
$loadedObjectStateGroup = $this->loadObjectStateGroup( $objectStateGroup->id );
$inputStruct = $this->buildObjectStateGroupUpdateInputStruct(
$loadedObjectStateGroup,
$objectStateGroupUpdateStruct->identifier,
$objectStateGroupUpdateStruct->defaultLanguageCode,
$objectStateGroupUpdateStruct->names,
$objectStateGroupUpdateStruct->descriptions
);
if ( $objectStateGroupUpdateStruct->identifier !== null )
{
try
{
$existingObjectStateGroup = $this->objectStateHandler->loadGroupByIdentifier( $inputStruct->identifier );
if ( $existingObjectStateGroup->id != $loadedObjectStateGroup->id )
{
throw new InvalidArgumentException(
"objectStateGroupUpdateStruct",
"Object state group with provided identifier already exists"
);
}
}
catch ( APINotFoundException $e )
{
// Do nothing
}
}
$this->repository->beginTransaction();
try
{
$spiObjectStateGroup = $this->objectStateHandler->updateGroup(
$loadedObjectStateGroup->id,
$inputStruct
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainObjectStateGroupObject( $spiObjectStateGroup );
}
/**
* Deletes a object state group including all states and links to content
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state group
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
*/
public function deleteObjectStateGroup( APIObjectStateGroup $objectStateGroup )
{
if ( $this->repository->hasAccess( 'state', 'administrate' ) !== true )
throw new UnauthorizedException( 'state', 'administrate' );
$loadedObjectStateGroup = $this->loadObjectStateGroup( $objectStateGroup->id );
$this->repository->beginTransaction();
try
{
$this->objectStateHandler->deleteGroup( $loadedObjectStateGroup->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Creates a new object state in the given group.
*
* Note: in current kernel: If it is the first state all content objects will
* set to this state.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct $objectStateCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
*/
public function createObjectState( APIObjectStateGroup $objectStateGroup, ObjectStateCreateStruct $objectStateCreateStruct )
{
if ( $this->repository->hasAccess( 'state', 'administrate' ) !== true )
throw new UnauthorizedException( 'state', 'administrate' );
$inputStruct = $this->buildCreateInputStruct(
$objectStateCreateStruct->identifier,
$objectStateCreateStruct->defaultLanguageCode,
$objectStateCreateStruct->names,
$objectStateCreateStruct->descriptions
);
try
{
$this->objectStateHandler->loadByIdentifier( $inputStruct->identifier, $objectStateGroup->id );
throw new InvalidArgumentException(
"objectStateCreateStruct",
"Object state with provided identifier already exists in provided object state group"
);
}
catch ( APINotFoundException $e )
{
// Do nothing
}
$this->repository->beginTransaction();
try
{
$spiObjectState = $this->objectStateHandler->create( $objectStateGroup->id, $inputStruct );
if ( is_int( $objectStateCreateStruct->priority ) )
{
$this->objectStateHandler->setPriority(
$spiObjectState->id,
$objectStateCreateStruct->priority
);
// Reload the object state to have the updated priority,
// considering that priorities are always incremental within a group
$spiObjectState = $this->objectStateHandler->load( $spiObjectState->id );
}
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainObjectStateObject( $spiObjectState );
}
/**
* Loads an object state
*
* @param mixed $stateId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the state was not found
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
*/
public function loadObjectState( $stateId )
{
$spiObjectState = $this->objectStateHandler->load( $stateId );
return $this->buildDomainObjectStateObject( $spiObjectState );
}
/**
* Updates an object state
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdateStruct
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
*/
public function updateObjectState( APIObjectState $objectState, ObjectStateUpdateStruct $objectStateUpdateStruct )
{
if ( $this->repository->hasAccess( 'state', 'administrate' ) !== true )
throw new UnauthorizedException( 'state', 'administrate' );
$loadedObjectState = $this->loadObjectState( $objectState->id );
$inputStruct = $this->buildObjectStateUpdateInputStruct(
$loadedObjectState,
$objectStateUpdateStruct->identifier,
$objectStateUpdateStruct->defaultLanguageCode,
$objectStateUpdateStruct->names,
$objectStateUpdateStruct->descriptions
);
if ( $objectStateUpdateStruct->identifier !== null )
{
try
{
$existingObjectState = $this->objectStateHandler->loadByIdentifier(
$inputStruct->identifier,
$loadedObjectState->getObjectStateGroup()->id
);
if ( $existingObjectState->id != $loadedObjectState->id )
{
throw new InvalidArgumentException(
"objectStateUpdateStruct",
"Object state with provided identifier already exists in provided object state group"
);
}
}
catch ( APINotFoundException $e )
{
// Do nothing
}
}
$this->repository->beginTransaction();
try
{
$spiObjectState = $this->objectStateHandler->update(
$loadedObjectState->id,
$inputStruct
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainObjectStateObject( $spiObjectState );
}
/**
* Changes the priority of the state
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change priority on an object state
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
* @param int $priority
*/
public function setPriorityOfObjectState( APIObjectState $objectState, $priority )
{
if ( !is_int( $priority ) )
throw new InvalidArgumentValue( "priority", $priority );
if ( $this->repository->hasAccess( 'state', 'administrate' ) !== true )
throw new UnauthorizedException( 'state', 'administrate' );
$loadedObjectState = $this->loadObjectState( $objectState->id );
$this->repository->beginTransaction();
try
{
$this->objectStateHandler->setPriority(
$loadedObjectState->id,
$priority
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Deletes a object state. The state of the content objects is reset to the
* first object state in the group.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
*/
public function deleteObjectState( APIObjectState $objectState )
{
if ( $this->repository->hasAccess( 'state', 'administrate' ) !== true )
throw new UnauthorizedException( 'state', 'administrate' );
$loadedObjectState = $this->loadObjectState( $objectState->id );
$this->repository->beginTransaction();
try
{
$this->objectStateHandler->delete( $loadedObjectState->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Sets the object-state of a state group to $state for the given content.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state does not belong to the given group
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change the object state
*
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
*/
public function setContentState( ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup, APIObjectState $objectState )
{
if ( $this->repository->canUser( 'state', 'assign', $contentInfo, $objectState ) !== true )
throw new UnauthorizedException( 'state', 'assign' );
$loadedObjectState = $this->loadObjectState( $objectState->id );
if ( $loadedObjectState->getObjectStateGroup()->id != $objectStateGroup->id )
throw new InvalidArgumentException( "objectState", "Object state does not belong to the given group" );
$this->repository->beginTransaction();
try
{
$this->objectStateHandler->setContentState(
$contentInfo->id,
$objectStateGroup->id,
$loadedObjectState->id
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Gets the object-state of object identified by $contentId.
*
* The $state is the id of the state within one group.
*
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
*/
public function getContentState( ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup )
{
$spiObjectState = $this->objectStateHandler->getContentState(
$contentInfo->id,
$objectStateGroup->id
);
return $this->buildDomainObjectStateObject( $spiObjectState );
}
/**
* Returns the number of objects which are in this state
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
*
* @return int
*/
public function getContentCount( APIObjectState $objectState )
{
return $this->objectStateHandler->getContentCount(
$objectState->id
);
}
/**
* Instantiates a new Object State Group Create Struct and sets $identified in it.
*
* @param string $identifier
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct
*/
public function newObjectStateGroupCreateStruct( $identifier )
{
$objectStateGroupCreateStruct = new ObjectStateGroupCreateStruct();
$objectStateGroupCreateStruct->identifier = $identifier;
return $objectStateGroupCreateStruct;
}
/**
* Instantiates a new Object State Group Update Struct.
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct
*/
public function newObjectStateGroupUpdateStruct()
{
return new ObjectStateGroupUpdateStruct();
}
/**
* Instantiates a new Object State Create Struct and sets $identifier in it.
*
* @param string $identifier
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct
*/
public function newObjectStateCreateStruct( $identifier )
{
$objectStateCreateStruct = new ObjectStateCreateStruct();
$objectStateCreateStruct->identifier = $identifier;
return $objectStateCreateStruct;
}
/**
* Instantiates a new Object State Update Struct.
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct
*/
public function newObjectStateUpdateStruct()
{
return new ObjectStateUpdateStruct();
}
/**
* Converts the object state SPI value object to API value object
*
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState $spiObjectState
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState
*/
protected function buildDomainObjectStateObject( SPIObjectState $spiObjectState, APIObjectStateGroup $objectStateGroup = null )
{
$objectStateGroup = $objectStateGroup ?: $this->loadObjectStateGroup( $spiObjectState->groupId );
return new ObjectState(
array(
'id' => $spiObjectState->id,
'identifier' => $spiObjectState->identifier,
'priority' => $spiObjectState->priority,
'defaultLanguageCode' => $spiObjectState->defaultLanguage,
'languageCodes' => $spiObjectState->languageCodes,
'names' => $spiObjectState->name,
'descriptions' => $spiObjectState->description,
'objectStateGroup' => $objectStateGroup
)
);
}
/**
* Converts the object state group SPI value object to API value object
*
* @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $spiObjectStateGroup
*
* @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
*/
protected function buildDomainObjectStateGroupObject( SPIObjectStateGroup $spiObjectStateGroup )
{
return new ObjectStateGroup(
array(
'id' => $spiObjectStateGroup->id,
'identifier' => $spiObjectStateGroup->identifier,
'defaultLanguageCode' => $spiObjectStateGroup->defaultLanguage,
'languageCodes' => $spiObjectStateGroup->languageCodes,
'names' => $spiObjectStateGroup->name,
'descriptions' => $spiObjectStateGroup->description
)
);
}
/**
* Validates input for creating object states/groups and builds the InputStruct object
*
* @param string $identifier
* @param string $defaultLanguageCode
* @param string[] $names
* @param string[] $descriptions
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
*/
protected function buildCreateInputStruct( $identifier, $defaultLanguageCode, $names, $descriptions )
{
if ( !is_string( $identifier ) || empty( $identifier ) )
throw new InvalidArgumentValue( "identifier", $identifier );
if ( !is_string( $defaultLanguageCode ) || empty( $defaultLanguageCode ) )
throw new InvalidArgumentValue( "defaultLanguageCode", $defaultLanguageCode );
if ( !is_array( $names ) || empty( $names ) )
throw new InvalidArgumentValue( "names", $names );
if ( !isset( $names[$defaultLanguageCode] ) )
throw new InvalidArgumentValue( "names", $names );
foreach ( $names as $languageCode => $name )
{
try
{
$this->repository->getContentLanguageService()->loadLanguage( $languageCode );
}
catch ( NotFoundException $e )
{
throw new InvalidArgumentValue( "names", $names );
}
if ( !is_string( $name ) || empty( $name ) )
throw new InvalidArgumentValue( "names", $names );
}
if ( $descriptions !== null && !is_array( $descriptions ) )
throw new InvalidArgumentValue( "descriptions", $descriptions );
$descriptions = $descriptions !== null ? $descriptions : array();
$inputStruct = new InputStruct();
$inputStruct->identifier = $identifier;
$inputStruct->defaultLanguage = $defaultLanguageCode;
$inputStruct->name = $names;
$inputStruct->description = array();
foreach ( $names as $languageCode => $name )
{
if ( isset( $descriptions[$languageCode] ) && !empty( $descriptions[$languageCode] ) )
$inputStruct->description[$languageCode] = $descriptions[$languageCode];
else
$inputStruct->description[$languageCode] = "";
}
return $inputStruct;
}
/**
* Validates input for updating object states and builds the InputStruct object
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState
* @param string $identifier
* @param string $defaultLanguageCode
* @param string[] $names
* @param string[] $descriptions
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
*/
protected function buildObjectStateUpdateInputStruct( APIObjectState $objectState, $identifier, $defaultLanguageCode, $names, $descriptions )
{
$inputStruct = new InputStruct();
if ( $identifier !== null && ( !is_string( $identifier ) || empty( $identifier ) ) )
throw new InvalidArgumentValue( "identifier", $identifier );
$inputStruct->identifier = $identifier !== null ? $identifier : $objectState->identifier;
if ( $defaultLanguageCode !== null && ( !is_string( $defaultLanguageCode ) || empty( $defaultLanguageCode ) ) )
throw new InvalidArgumentValue( "defaultLanguageCode", $defaultLanguageCode );
$inputStruct->defaultLanguage = $defaultLanguageCode !== null ? $defaultLanguageCode : $objectState->defaultLanguageCode;
if ( $names !== null && ( !is_array( $names ) || empty( $names ) ) )
throw new InvalidArgumentValue( "names", $names );
$inputStruct->name = $names !== null ? $names : $objectState->getNames();
if ( !isset( $inputStruct->name[$inputStruct->defaultLanguage] ) )
throw new InvalidArgumentValue( "names", $inputStruct->name );
foreach ( $inputStruct->name as $languageCode => $name )
{
try
{
$this->repository->getContentLanguageService()->loadLanguage( $languageCode );
}
catch ( NotFoundException $e )
{
throw new InvalidArgumentValue( "names", $inputStruct->name );
}
if ( !is_string( $name ) || empty( $name ) )
throw new InvalidArgumentValue( "names", $inputStruct->name );
}
if ( $descriptions !== null && !is_array( $descriptions ) )
throw new InvalidArgumentValue( "descriptions", $descriptions );
$descriptions = $descriptions !== null ? $descriptions : $objectState->getDescriptions();
$descriptions = $descriptions !== null ? $descriptions : array();
$inputStruct->description = array();
foreach ( $inputStruct->name as $languageCode => $name )
{
if ( isset( $descriptions[$languageCode] ) && !empty( $descriptions[$languageCode] ) )
$inputStruct->description[$languageCode] = $descriptions[$languageCode];
else
$inputStruct->description[$languageCode] = "";
}
return $inputStruct;
}
/**
* Validates input for updating object state groups and builds the InputStruct object
*
* @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
* @param string $identifier
* @param string $defaultLanguageCode
* @param string[] $names
* @param string[] $descriptions
*
* @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
*/
protected function buildObjectStateGroupUpdateInputStruct( APIObjectStateGroup $objectStateGroup, $identifier, $defaultLanguageCode, $names, $descriptions )
{
$inputStruct = new InputStruct();
if ( $identifier !== null && ( !is_string( $identifier ) || empty( $identifier ) ) )
throw new InvalidArgumentValue( "identifier", $identifier );
$inputStruct->identifier = $identifier !== null ? $identifier : $objectStateGroup->identifier;
if ( $defaultLanguageCode !== null && ( !is_string( $defaultLanguageCode ) || empty( $defaultLanguageCode ) ) )
throw new InvalidArgumentValue( "defaultLanguageCode", $defaultLanguageCode );
$inputStruct->defaultLanguage = $defaultLanguageCode !== null ? $defaultLanguageCode : $objectStateGroup->defaultLanguageCode;
if ( $names !== null && ( !is_array( $names ) || empty( $names ) ) )
throw new InvalidArgumentValue( "names", $names );
$inputStruct->name = $names !== null ? $names : $objectStateGroup->getNames();
if ( !isset( $inputStruct->name[$inputStruct->defaultLanguage] ) )
throw new InvalidArgumentValue( "names", $inputStruct->name );
foreach ( $inputStruct->name as $languageCode => $name )
{
try
{
$this->repository->getContentLanguageService()->loadLanguage( $languageCode );
}
catch ( NotFoundException $e )
{
throw new InvalidArgumentValue( "names", $inputStruct->name );
}
if ( !is_string( $name ) || empty( $name ) )
throw new InvalidArgumentValue( "names", $inputStruct->name );
}
if ( $descriptions !== null && !is_array( $descriptions ) )
throw new InvalidArgumentValue( "descriptions", $descriptions );
$descriptions = $descriptions !== null ? $descriptions : $objectStateGroup->getDescriptions();
$descriptions = $descriptions !== null ? $descriptions : array();
$inputStruct->description = array();
foreach ( $inputStruct->name as $languageCode => $name )
{
if ( isset( $descriptions[$languageCode] ) && !empty( $descriptions[$languageCode] ) )
$inputStruct->description[$languageCode] = $descriptions[$languageCode];
else
$inputStruct->description[$languageCode] = "";
}
return $inputStruct;
}
}